|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "fmt" |
5 | | - "os" |
6 | | - "os/exec" |
7 | | - "sort" |
8 | | - "text/tabwriter" |
9 | | - |
10 | 4 | "github.com/spf13/cobra" |
11 | | - "github.com/stuartleeks/devcontainer-cli/internal/pkg/devcontainers" |
12 | 5 | ) |
13 | 6 |
|
14 | 7 | func main() { |
15 | | - var listIncludeContainerNames bool |
16 | | - var listVerbose bool |
17 | 8 |
|
18 | 9 | rootCmd := &cobra.Command{Use: "devcontainer"} |
19 | 10 |
|
20 | | - cmdList := &cobra.Command{ |
21 | | - Use: "list", |
22 | | - Short: "List devcontainers", |
23 | | - Long: "Lists devcontainers that are currently running", |
24 | | - Run: func(cmd *cobra.Command, args []string) { |
25 | | - runListCommand(cmd, args, listIncludeContainerNames, listVerbose) |
26 | | - }, |
27 | | - } |
28 | | - cmdList.Flags().BoolVar(&listIncludeContainerNames, "include-container-names", false, "Also include container names in the list") |
29 | | - cmdList.Flags().BoolVarP(&listVerbose, "verbose", "v", false, "Verbose output") |
30 | | - rootCmd.AddCommand(cmdList) |
31 | | - |
32 | | - cmdExec := &cobra.Command{ |
33 | | - Use: "exec DEVCONTAINER_NAME COMMAND [args...]", |
34 | | - Short: "Execute a command in a devcontainer", |
35 | | - Long: "Execute a command in a devcontainer, similar to `docker exec`.", |
36 | | - Run: func(cmd *cobra.Command, args []string) { |
37 | | - runExecCommand(cmd, args) |
38 | | - }, |
39 | | - Args: cobra.ArbitraryArgs, |
40 | | - DisableFlagParsing: true, |
41 | | - DisableFlagsInUseLine: true, |
42 | | - ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
43 | | - // only completing the first arg (devcontainer name) |
44 | | - if len(args) != 0 { |
45 | | - return nil, cobra.ShellCompDirectiveNoFileComp |
46 | | - } |
47 | | - devcontainers, err := devcontainers.ListDevcontainers() |
48 | | - if err != nil { |
49 | | - fmt.Printf("Error: %v", err) |
50 | | - os.Exit(1) |
51 | | - } |
52 | | - names := []string{} |
53 | | - for _, devcontainer := range devcontainers { |
54 | | - names = append(names, devcontainer.DevcontainerName) |
55 | | - names = append(names, devcontainer.ContainerName) |
56 | | - } |
57 | | - sort.Strings(names) |
58 | | - return names, cobra.ShellCompDirectiveNoFileComp |
59 | | - }, |
60 | | - } |
61 | | - rootCmd.AddCommand(cmdExec) |
62 | | - |
63 | | - cmdCompletion := &cobra.Command{ |
64 | | - Use: "completion", |
65 | | - Short: "Generates bash completion scripts", |
66 | | - Long: `To load completion run |
67 | | - |
68 | | - . <(devcontainer completion) |
69 | | - |
70 | | - To configure your bash shell to load completions for each session add to your bashrc |
71 | | - |
72 | | - # ~/.bashrc or ~/.profile |
73 | | - . <(devcontainer completion) |
74 | | - `, |
75 | | - Run: func(cmd *cobra.Command, args []string) { |
76 | | - rootCmd.GenBashCompletion(os.Stdout) |
77 | | - }, |
78 | | - } |
79 | | - rootCmd.AddCommand(cmdCompletion) |
| 11 | + rootCmd.AddCommand(createListCommand()) |
| 12 | + rootCmd.AddCommand(createExecCommand()) |
| 13 | + rootCmd.AddCommand(createCompleteCommand(rootCmd)) |
80 | 14 |
|
81 | 15 | rootCmd.Execute() |
82 | 16 | } |
83 | | - |
84 | | -func runListCommand(cmd *cobra.Command, args []string, listIncludeContainerNames bool, listVerbose bool) { |
85 | | - if listIncludeContainerNames && listVerbose { |
86 | | - fmt.Println("Can't use both verbose and include-container-names") |
87 | | - os.Exit(1) |
88 | | - } |
89 | | - devcontainers, err := devcontainers.ListDevcontainers() |
90 | | - if err != nil { |
91 | | - fmt.Printf("Error: %v", err) |
92 | | - os.Exit(1) |
93 | | - } |
94 | | - if listVerbose { |
95 | | - sort.Slice(devcontainers, func(i, j int) bool { return devcontainers[i].DevcontainerName < devcontainers[j].DevcontainerName }) |
96 | | - |
97 | | - w := new(tabwriter.Writer) |
98 | | - // minwidth, tabwidth, padding, padchar, flags |
99 | | - w.Init(os.Stdout, 8, 8, 0, '\t', 0) |
100 | | - defer w.Flush() |
101 | | - |
102 | | - fmt.Fprintf(w, "%s\t%s\n", "DEVCONTAINER NAME", "CONTAINER NAME") |
103 | | - fmt.Fprintf(w, "%s\t%s\n", "-----------------", "--------------") |
104 | | - |
105 | | - for _, devcontainer := range devcontainers { |
106 | | - fmt.Fprintf(w, "%s\t%s\n", devcontainer.DevcontainerName, devcontainer.ContainerName) |
107 | | - } |
108 | | - return |
109 | | - } |
110 | | - names := []string{} |
111 | | - for _, devcontainer := range devcontainers { |
112 | | - names = append(names, devcontainer.DevcontainerName) |
113 | | - if listIncludeContainerNames { |
114 | | - names = append(names, devcontainer.ContainerName) |
115 | | - } |
116 | | - } |
117 | | - sort.Strings(names) |
118 | | - for _, name := range names { |
119 | | - fmt.Println(name) |
120 | | - } |
121 | | -} |
122 | | - |
123 | | -func runExecCommand(cmd *cobra.Command, args []string) { |
124 | | - // TODO argument validation! |
125 | | - |
126 | | - if len(args) < 2 { |
127 | | - cmd.Usage() |
128 | | - os.Exit(1) |
129 | | - } |
130 | | - |
131 | | - devcontainerName := args[0] |
132 | | - devcontainers, err := devcontainers.ListDevcontainers() |
133 | | - if err != nil { |
134 | | - fmt.Printf("Error: %v", err) |
135 | | - os.Exit(1) |
136 | | - } |
137 | | - |
138 | | - containerID := "" |
139 | | - for _, devcontainer := range devcontainers { |
140 | | - if devcontainer.ContainerName == devcontainerName || devcontainer.DevcontainerName == devcontainerName { |
141 | | - containerID = devcontainer.ContainerID |
142 | | - break |
143 | | - } |
144 | | - } |
145 | | - if containerID == "" { |
146 | | - cmd.Usage() |
147 | | - if err != nil { |
148 | | - fmt.Printf("Error: %v", err) |
149 | | - } |
150 | | - os.Exit(1) |
151 | | - } |
152 | | - |
153 | | - dockerArgs := []string{"exec", "-it", containerID} |
154 | | - dockerArgs = append(dockerArgs, args[1:]...) |
155 | | - |
156 | | - dockerCmd := exec.Command("docker", dockerArgs...) |
157 | | - dockerCmd.Stdin = os.Stdin |
158 | | - dockerCmd.Stdout = os.Stdout |
159 | | - |
160 | | - err = dockerCmd.Start() |
161 | | - if err != nil { |
162 | | - fmt.Printf("Exec: start error: %s\n", err) |
163 | | - } |
164 | | - err = dockerCmd.Wait() |
165 | | - if err != nil { |
166 | | - fmt.Printf("Exec: wait error: %s\n", err) |
167 | | - } |
168 | | -} |
0 commit comments