|
| 1 | +package cliui |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "sort" |
| 7 | + "strconv" |
| 8 | + |
| 9 | + "github.com/jedib0t/go-pretty/v6/table" |
| 10 | + |
| 11 | + "github.com/coder/coder/coderd/database" |
| 12 | + "github.com/coder/coder/codersdk" |
| 13 | +) |
| 14 | + |
| 15 | +type WorkspaceResourcesOptions struct { |
| 16 | + WorkspaceName string |
| 17 | + HideAgentState bool |
| 18 | + HideAccess bool |
| 19 | + Title string |
| 20 | +} |
| 21 | + |
| 22 | +// WorkspaceResources displays the connection status and tree-view of provided resources. |
| 23 | +// ┌────────────────────────────────────────────────────────────────────────────┐ |
| 24 | +// │ RESOURCE STATUS ACCESS │ |
| 25 | +// ├────────────────────────────────────────────────────────────────────────────┤ |
| 26 | +// │ google_compute_disk.root persistent │ |
| 27 | +// ├────────────────────────────────────────────────────────────────────────────┤ |
| 28 | +// │ google_compute_instance.dev ephemeral │ |
| 29 | +// │ └─ dev (linux, amd64) ⦾ connecting [10s] coder ssh dev.dev │ |
| 30 | +// ├────────────────────────────────────────────────────────────────────────────┤ |
| 31 | +// │ kubernetes_pod.dev ephemeral │ |
| 32 | +// │ ├─ go (linux, amd64) ⦿ connected coder ssh dev.go │ |
| 33 | +// │ └─ postgres (linux, amd64) ⦾ disconnected [4s] coder ssh dev.postgres │ |
| 34 | +// └────────────────────────────────────────────────────────────────────────────┘ |
| 35 | +func WorkspaceResources(writer io.Writer, resources []codersdk.WorkspaceResource, options WorkspaceResourcesOptions) error { |
| 36 | + // Sort resources by type for consistent output. |
| 37 | + sort.Slice(resources, func(i, j int) bool { |
| 38 | + return resources[i].Type < resources[j].Type |
| 39 | + }) |
| 40 | + |
| 41 | + // Address on stop indexes whether a resource still exists when in the stopped transition. |
| 42 | + addressOnStop := map[string]codersdk.WorkspaceResource{} |
| 43 | + for _, resource := range resources { |
| 44 | + if resource.Transition != database.WorkspaceTransitionStop { |
| 45 | + continue |
| 46 | + } |
| 47 | + addressOnStop[resource.Address] = resource |
| 48 | + } |
| 49 | + // Displayed stores whether a resource has already been shown. |
| 50 | + // Resources can be stored with numerous states, which we |
| 51 | + // process prior to display. |
| 52 | + displayed := map[string]struct{}{} |
| 53 | + |
| 54 | + tableWriter := table.NewWriter() |
| 55 | + if options.Title != "" { |
| 56 | + tableWriter.SetTitle(options.Title) |
| 57 | + } |
| 58 | + tableWriter.SetStyle(table.StyleLight) |
| 59 | + tableWriter.Style().Options.SeparateColumns = false |
| 60 | + row := table.Row{"Resource", "Status"} |
| 61 | + if !options.HideAccess { |
| 62 | + row = append(row, "Access") |
| 63 | + } |
| 64 | + tableWriter.AppendHeader(row) |
| 65 | + |
| 66 | + totalAgents := 0 |
| 67 | + for _, resource := range resources { |
| 68 | + totalAgents += len(resource.Agents) |
| 69 | + } |
| 70 | + |
| 71 | + for _, resource := range resources { |
| 72 | + if resource.Type == "random_string" { |
| 73 | + // Hide resources that aren't substantial to a user! |
| 74 | + // This is an unfortunate case, and we should allow |
| 75 | + // callers to hide resources eventually. |
| 76 | + continue |
| 77 | + } |
| 78 | + if _, shown := displayed[resource.Address]; shown { |
| 79 | + // The same resource can have multiple transitions. |
| 80 | + continue |
| 81 | + } |
| 82 | + displayed[resource.Address] = struct{}{} |
| 83 | + |
| 84 | + // Sort agents by name for consistent output. |
| 85 | + sort.Slice(resource.Agents, func(i, j int) bool { |
| 86 | + return resource.Agents[i].Name < resource.Agents[j].Name |
| 87 | + }) |
| 88 | + _, existsOnStop := addressOnStop[resource.Address] |
| 89 | + resourceState := "ephemeral" |
| 90 | + if existsOnStop { |
| 91 | + resourceState = "persistent" |
| 92 | + } |
| 93 | + // Display a line for the resource. |
| 94 | + tableWriter.AppendRow(table.Row{ |
| 95 | + Styles.Bold.Render(resource.Type + "." + resource.Name), |
| 96 | + Styles.Placeholder.Render(resourceState), |
| 97 | + "", |
| 98 | + }) |
| 99 | + // Display all agents associated with the resource. |
| 100 | + for index, agent := range resource.Agents { |
| 101 | + sshCommand := "coder ssh " + options.WorkspaceName |
| 102 | + if totalAgents > 1 { |
| 103 | + sshCommand += "." + agent.Name |
| 104 | + } |
| 105 | + sshCommand = Styles.Code.Render(sshCommand) |
| 106 | + var agentStatus string |
| 107 | + if !options.HideAgentState { |
| 108 | + switch agent.Status { |
| 109 | + case codersdk.WorkspaceAgentConnecting: |
| 110 | + since := database.Now().Sub(agent.CreatedAt) |
| 111 | + agentStatus = Styles.Warn.Render("⦾ connecting") + " " + |
| 112 | + Styles.Placeholder.Render("["+strconv.Itoa(int(since.Seconds()))+"s]") |
| 113 | + case codersdk.WorkspaceAgentDisconnected: |
| 114 | + since := database.Now().Sub(*agent.DisconnectedAt) |
| 115 | + agentStatus = Styles.Error.Render("⦾ disconnected") + " " + |
| 116 | + Styles.Placeholder.Render("["+strconv.Itoa(int(since.Seconds()))+"s]") |
| 117 | + case codersdk.WorkspaceAgentConnected: |
| 118 | + agentStatus = Styles.Keyword.Render("⦿ connected") |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + pipe := "├" |
| 123 | + if index == len(resource.Agents)-1 { |
| 124 | + pipe = "└" |
| 125 | + } |
| 126 | + row := table.Row{ |
| 127 | + // These tree from a resource! |
| 128 | + fmt.Sprintf("%s─ %s (%s, %s)", pipe, agent.Name, agent.OperatingSystem, agent.Architecture), |
| 129 | + agentStatus, |
| 130 | + } |
| 131 | + if !options.HideAccess { |
| 132 | + row = append(row, sshCommand) |
| 133 | + } |
| 134 | + tableWriter.AppendRow(row) |
| 135 | + } |
| 136 | + tableWriter.AppendSeparator() |
| 137 | + } |
| 138 | + _, err := fmt.Fprintln(writer, tableWriter.Render()) |
| 139 | + return err |
| 140 | +} |
0 commit comments