Skip to content

Commit c963c03

Browse files
mustard-mhroboquat
authored andcommitted
[gp-cli] move cmd files
1 parent c9e363c commit c963c03

File tree

8 files changed

+278
-278
lines changed

8 files changed

+278
-278
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2+
// Licensed under the GNU Affero General Public License (AGPL).
3+
// See License-AGPL.txt in the project root for license information.
4+
5+
package cmd
6+
7+
import (
8+
"context"
9+
"fmt"
10+
"os"
11+
"sort"
12+
"time"
13+
14+
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor"
15+
"github.com/gitpod-io/gitpod/supervisor/api"
16+
log "github.com/sirupsen/logrus"
17+
"github.com/spf13/cobra"
18+
19+
"github.com/olekukonko/tablewriter"
20+
)
21+
22+
var listPortsCmd = &cobra.Command{
23+
Use: "list",
24+
Short: "Lists the workspace ports and their states.",
25+
Run: func(*cobra.Command, []string) {
26+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
27+
defer cancel()
28+
29+
conn := supervisor.Dial()
30+
client := api.NewStatusServiceClient(conn)
31+
32+
ports, portsListError := supervisor.GetPortsList(ctx, client)
33+
34+
if portsListError != nil {
35+
log.WithError(portsListError).Error("Could not get the ports list.")
36+
return
37+
}
38+
39+
if len(ports) == 0 {
40+
fmt.Println("No ports detected.")
41+
return
42+
}
43+
44+
sort.Slice(ports, func(i, j int) bool {
45+
return int(ports[i].LocalPort) < int(ports[j].LocalPort)
46+
})
47+
48+
table := tablewriter.NewWriter(os.Stdout)
49+
table.SetHeader([]string{"Port", "Status", "URL", "Name & Description"})
50+
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
51+
table.SetCenterSeparator("|")
52+
53+
for _, port := range ports {
54+
status := "not served"
55+
statusColor := tablewriter.FgHiBlackColor
56+
if port.Exposed == nil && port.Tunneled == nil {
57+
if port.AutoExposure == api.PortAutoExposure_failed {
58+
status = "failed to expose"
59+
statusColor = tablewriter.FgRedColor
60+
} else {
61+
status = "detecting..."
62+
statusColor = tablewriter.FgYellowColor
63+
}
64+
} else if port.Served {
65+
status = "open (" + port.Exposed.Visibility.String() + ")"
66+
if port.Exposed.Visibility == api.PortVisibility_public {
67+
statusColor = tablewriter.FgHiGreenColor
68+
} else {
69+
statusColor = tablewriter.FgHiCyanColor
70+
}
71+
}
72+
73+
nameAndDescription := port.Name
74+
if len(port.Description) > 0 {
75+
if len(nameAndDescription) > 0 {
76+
nameAndDescription = fmt.Sprint(nameAndDescription, ": ", port.Description)
77+
} else {
78+
nameAndDescription = port.Description
79+
}
80+
}
81+
82+
table.Rich(
83+
[]string{fmt.Sprint(port.LocalPort), status, port.Exposed.Url, nameAndDescription},
84+
[]tablewriter.Colors{{}, {statusColor}, {}, {}},
85+
)
86+
}
87+
88+
table.Render()
89+
},
90+
}
91+
92+
func init() {
93+
portsCmd.AddCommand(listPortsCmd)
94+
}

components/gitpod-cli/cmd/ports.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package cmd
66

77
import (
8-
"github.com/gitpod-io/gitpod/gitpod-cli/cmd/ports"
98
"github.com/spf13/cobra"
109
)
1110

@@ -19,13 +18,6 @@ var portsCmd = &cobra.Command{
1918
},
2019
}
2120

22-
var listPortsCmd = &cobra.Command{
23-
Use: "list",
24-
Short: "Lists the workspace ports and their states.",
25-
Run: ports.ListPortsCmd,
26-
}
27-
2821
func init() {
2922
rootCmd.AddCommand(portsCmd)
30-
portsCmd.AddCommand(listPortsCmd)
3123
}

components/gitpod-cli/cmd/ports/ports-list.go

Lines changed: 0 additions & 86 deletions
This file was deleted.
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2+
// Licensed under the GNU Affero General Public License (AGPL).
3+
// See License-AGPL.txt in the project root for license information.
4+
5+
package cmd
6+
7+
import (
8+
"context"
9+
"fmt"
10+
"os"
11+
"time"
12+
13+
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor"
14+
"github.com/gitpod-io/gitpod/supervisor/api"
15+
"github.com/manifoldco/promptui"
16+
"github.com/spf13/cobra"
17+
"google.golang.org/grpc/codes"
18+
"google.golang.org/grpc/status"
19+
)
20+
21+
var attachTaskCmdOpts struct {
22+
Interactive bool
23+
ForceResize bool
24+
}
25+
26+
// attachTaskCmd represents the attach task command
27+
var attachTaskCmd = &cobra.Command{
28+
Use: "attach <id>",
29+
Short: "Attach to a workspace task",
30+
Args: cobra.MaximumNArgs(1),
31+
Run: func(cmd *cobra.Command, args []string) {
32+
var terminalAlias string
33+
34+
conn := supervisor.Dial()
35+
36+
if len(args) > 0 {
37+
terminalAlias = args[0]
38+
} else {
39+
statusClient := api.NewStatusServiceClient(conn)
40+
stateToFilter := api.TaskState(api.TaskState_value["running"])
41+
42+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
43+
defer cancel()
44+
45+
tasks := supervisor.GetTasksListByState(ctx, statusClient, stateToFilter)
46+
47+
if len(tasks) == 0 {
48+
fmt.Println("There are no running tasks")
49+
return
50+
}
51+
52+
var taskNames []string
53+
var taskIndex int
54+
55+
if len(tasks) == 1 {
56+
taskIndex = 0
57+
} else {
58+
59+
for _, task := range tasks {
60+
taskNames = append(taskNames, task.Presentation.Name)
61+
}
62+
63+
prompt := promptui.Select{
64+
Label: "What task do you want attach to?",
65+
Items: taskNames,
66+
Templates: &promptui.SelectTemplates{
67+
Selected: "Attaching to task: {{ . }}",
68+
},
69+
}
70+
71+
selectedIndex, selectedValue, err := prompt.Run()
72+
73+
if selectedValue == "" {
74+
return
75+
}
76+
77+
if err != nil {
78+
panic(err)
79+
}
80+
81+
taskIndex = selectedIndex
82+
}
83+
84+
terminalAlias = tasks[taskIndex].Terminal
85+
}
86+
87+
terminalClient := api.NewTerminalServiceClient(conn)
88+
89+
terminal, err := terminalClient.Get(context.Background(), &api.GetTerminalRequest{Alias: terminalAlias})
90+
if err != nil {
91+
if e, ok := status.FromError(err); ok {
92+
switch e.Code() {
93+
case codes.NotFound:
94+
fmt.Println("Terminal is inactive:", terminalAlias)
95+
default:
96+
fmt.Println(e.Code(), e.Message())
97+
}
98+
return
99+
} else {
100+
panic(err)
101+
}
102+
}
103+
ppid := int64(os.Getppid())
104+
105+
if ppid == terminal.Pid {
106+
fmt.Println("You are already in terminal:", terminalAlias)
107+
return
108+
}
109+
110+
interactive, _ := cmd.Flags().GetBool("interactive")
111+
forceResize, _ := cmd.Flags().GetBool("force-resize")
112+
113+
supervisor.AttachToTerminal(context.Background(), terminalClient, terminalAlias, supervisor.AttachToTerminalOpts{
114+
ForceResize: forceResize,
115+
Interactive: interactive,
116+
})
117+
},
118+
}
119+
120+
func init() {
121+
tasksCmd.AddCommand(attachTaskCmd)
122+
123+
attachTaskCmd.Flags().BoolVarP(&attachTaskCmdOpts.Interactive, "interactive", "i", true, "assume control over the terminal")
124+
attachTaskCmd.Flags().BoolVarP(&attachTaskCmdOpts.ForceResize, "force-resize", "r", true, "force this terminal's size irregardless of other clients")
125+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2+
// Licensed under the GNU Affero General Public License (AGPL).
3+
// See License-AGPL.txt in the project root for license information.
4+
5+
package cmd
6+
7+
import (
8+
"context"
9+
"fmt"
10+
"os"
11+
"time"
12+
13+
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor"
14+
"github.com/gitpod-io/gitpod/supervisor/api"
15+
"github.com/spf13/cobra"
16+
17+
"github.com/olekukonko/tablewriter"
18+
)
19+
20+
// listTasksCmd represents the tasks list command
21+
var listTasksCmd = &cobra.Command{
22+
Use: "list",
23+
Short: "Lists the workspace tasks and their state",
24+
Run: func(cmd *cobra.Command, args []string) {
25+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
26+
defer cancel()
27+
28+
conn := supervisor.Dial()
29+
client := api.NewStatusServiceClient(conn)
30+
31+
tasks := supervisor.GetTasksList(ctx, client)
32+
33+
if len(tasks) == 0 {
34+
fmt.Println("No tasks detected")
35+
return
36+
}
37+
38+
table := tablewriter.NewWriter(os.Stdout)
39+
table.SetHeader([]string{"Terminal ID", "Name", "State"})
40+
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
41+
table.SetCenterSeparator("|")
42+
43+
mapStatusToColor := map[api.TaskState]int{
44+
0: tablewriter.FgHiGreenColor,
45+
1: tablewriter.FgHiGreenColor,
46+
2: tablewriter.FgHiBlackColor,
47+
}
48+
49+
for _, task := range tasks {
50+
table.Rich([]string{task.Terminal, task.Presentation.Name, task.State.String()}, []tablewriter.Colors{{}, {}, {mapStatusToColor[task.State]}})
51+
}
52+
53+
table.Render()
54+
},
55+
}
56+
57+
func init() {
58+
tasksCmd.AddCommand(listTasksCmd)
59+
}

0 commit comments

Comments
 (0)