@@ -21,45 +21,7 @@ func main() {
2121 Short : "List devcontainers" ,
2222 Long : "Lists devcontainers that are currently running" ,
2323 Run : func (cmd * cobra.Command , args []string ) {
24- if listIncludeContainerNames && listVerbose {
25- fmt .Println ("Can't use both verbose and include-container-names" )
26- os .Exit (1 )
27- }
28- devcontainers , err := devcontainers .ListDevcontainers ()
29- if err != nil {
30- fmt .Printf ("Error: %v" , err )
31- os .Exit (1 )
32- }
33- if listVerbose {
34- sort .Slice (devcontainers , func (i , j int ) bool { return devcontainers [i ].DevcontainerName < devcontainers [j ].DevcontainerName })
35-
36- w := new (tabwriter.Writer )
37- // minwidth, tabwidth, padding, padchar, flags
38- w .Init (os .Stdout , 8 , 8 , 0 , '\t' , 0 )
39- defer w .Flush ()
40-
41- fmt .Fprintf (w , "\n %s\t %s\t " , "Devcontainer name" , "Container name" )
42- fmt .Fprintf (w , "\n %s\t %s\t " , "----" , "----" )
43-
44- for _ , devcontainer := range devcontainers {
45- fmt .Fprintf (w , "\n %s\t %s\t " , devcontainer .DevcontainerName , devcontainer .ContainerName )
46- }
47- fmt .Fprintln (w )
48- return
49- }
50- names := []string {}
51- for _ , devcontainer := range devcontainers {
52- names = append (names , devcontainer .DevcontainerName )
53- }
54- if listIncludeContainerNames {
55- for _ , devcontainer := range devcontainers {
56- names = append (names , devcontainer .ContainerName )
57- }
58- }
59- sort .Strings (names )
60- for _ , name := range names {
61- fmt .Println (name )
62- }
24+ runListCommand (cmd , args , listIncludeContainerNames , listVerbose )
6325 },
6426 }
6527 cmdList .Flags ().BoolVar (& listIncludeContainerNames , "include-container-names" , false , "Also include container names in the list" )
@@ -68,3 +30,44 @@ func main() {
6830 rootCmd .AddCommand (cmdList )
6931 rootCmd .Execute ()
7032}
33+
34+ func runListCommand (cmd * cobra.Command , args []string , listIncludeContainerNames bool , listVerbose bool ) {
35+ if listIncludeContainerNames && listVerbose {
36+ fmt .Println ("Can't use both verbose and include-container-names" )
37+ os .Exit (1 )
38+ }
39+ devcontainers , err := devcontainers .ListDevcontainers ()
40+ if err != nil {
41+ fmt .Printf ("Error: %v" , err )
42+ os .Exit (1 )
43+ }
44+ if listVerbose {
45+ sort .Slice (devcontainers , func (i , j int ) bool { return devcontainers [i ].DevcontainerName < devcontainers [j ].DevcontainerName })
46+
47+ w := new (tabwriter.Writer )
48+ // minwidth, tabwidth, padding, padchar, flags
49+ w .Init (os .Stdout , 8 , 8 , 0 , '\t' , 0 )
50+ defer w .Flush ()
51+
52+ fmt .Fprintf (w , "%s\t %s\n " , "DEVCONTAINER NAME" , "CONTAINER NAME" )
53+ fmt .Fprintf (w , "%s\t %s\n " , "-----------------" , "--------------" )
54+
55+ for _ , devcontainer := range devcontainers {
56+ fmt .Fprintf (w , "%s\t %s\n " , devcontainer .DevcontainerName , devcontainer .ContainerName )
57+ }
58+ return
59+ }
60+ names := []string {}
61+ for _ , devcontainer := range devcontainers {
62+ names = append (names , devcontainer .DevcontainerName )
63+ }
64+ if listIncludeContainerNames {
65+ for _ , devcontainer := range devcontainers {
66+ names = append (names , devcontainer .ContainerName )
67+ }
68+ }
69+ sort .Strings (names )
70+ for _ , name := range names {
71+ fmt .Println (name )
72+ }
73+ }
0 commit comments