@@ -3,6 +3,7 @@ package main
33import (
44 "fmt"
55 "os"
6+ "os/exec"
67 "sort"
78 "text/tabwriter"
89
@@ -28,6 +29,34 @@ func main() {
2829 cmdList .Flags ().BoolVarP (& listVerbose , "verbose" , "v" , false , "Verbose output" )
2930 rootCmd .AddCommand (cmdList )
3031
32+ cmdExec := & cobra.Command {
33+ Use : "exec DEVCONTAINER_NAME COMMAND" ,
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+ ValidArgsFunction : func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
40+ // only completing the first arg (devcontainer name)
41+ if len (args ) != 0 {
42+ return nil , cobra .ShellCompDirectiveNoFileComp
43+ }
44+ devcontainers , err := devcontainers .ListDevcontainers ()
45+ if err != nil {
46+ fmt .Printf ("Error: %v" , err )
47+ os .Exit (1 )
48+ }
49+ names := []string {}
50+ for _ , devcontainer := range devcontainers {
51+ names = append (names , devcontainer .DevcontainerName )
52+ names = append (names , devcontainer .ContainerName )
53+ }
54+ sort .Strings (names )
55+ return names , cobra .ShellCompDirectiveNoFileComp
56+ },
57+ }
58+ rootCmd .AddCommand (cmdExec )
59+
3160 cmdCompletion := & cobra.Command {
3261 Use : "completion" ,
3362 Short : "Generates bash completion scripts" ,
@@ -78,9 +107,7 @@ func runListCommand(cmd *cobra.Command, args []string, listIncludeContainerNames
78107 names := []string {}
79108 for _ , devcontainer := range devcontainers {
80109 names = append (names , devcontainer .DevcontainerName )
81- }
82- if listIncludeContainerNames {
83- for _ , devcontainer := range devcontainers {
110+ if listIncludeContainerNames {
84111 names = append (names , devcontainer .ContainerName )
85112 }
86113 }
@@ -89,3 +116,47 @@ func runListCommand(cmd *cobra.Command, args []string, listIncludeContainerNames
89116 fmt .Println (name )
90117 }
91118}
119+
120+ func runExecCommand (cmd * cobra.Command , args []string ) {
121+ // TODO argument validation!
122+
123+ fmt .Printf ("TODO - exec: %v\n " , args )
124+
125+ devcontainerName := args [0 ]
126+ devcontainers , err := devcontainers .ListDevcontainers ()
127+ if err != nil {
128+ fmt .Printf ("Error: %v" , err )
129+ os .Exit (1 )
130+ }
131+
132+ containerID := ""
133+ for _ , devcontainer := range devcontainers {
134+ if devcontainer .ContainerName == devcontainerName || devcontainer .DevcontainerName == devcontainerName {
135+ containerID = devcontainer .ContainerID
136+ break
137+ }
138+ }
139+ if containerID == "" {
140+ cmd .Usage ()
141+ if err != nil {
142+ fmt .Printf ("Error: %v" , err )
143+ }
144+ os .Exit (1 )
145+ }
146+
147+ dockerArgs := []string {"exec" , "-it" , containerID }
148+ dockerArgs = append (dockerArgs , args [1 :]... )
149+
150+ dockerCmd := exec .Command ("docker" , dockerArgs ... )
151+ dockerCmd .Stdin = os .Stdin
152+ dockerCmd .Stdout = os .Stdout
153+
154+ err = dockerCmd .Start ()
155+ if err != nil {
156+ fmt .Printf ("Exec: start error: %s\n " , err )
157+ }
158+ err = dockerCmd .Wait ()
159+ if err != nil {
160+ fmt .Printf ("Exec: wait error: %s\n " , err )
161+ }
162+ }
0 commit comments