@@ -29,17 +29,17 @@ func createTemplateListCommand() *cobra.Command {
2929 Use : "list" ,
3030 Short : "list templates" ,
3131 Long : "List devcontainer templates" ,
32- Run : func (cmd * cobra.Command , args []string ) {
32+ RunE : func (cmd * cobra.Command , args []string ) error {
3333
3434 templates , err := devcontainers .GetTemplates ()
3535 if err != nil {
36- fmt .Println (err )
37- os .Exit (1 )
36+ return err
3837 }
3938
4039 for _ , template := range templates {
4140 fmt .Println (template .Name )
4241 }
42+ return nil
4343 },
4444 }
4545 return cmd
@@ -50,37 +50,34 @@ func createTemplateAddCommand() *cobra.Command {
5050 Use : "add TEMPLATE_NAME" ,
5151 Short : "add devcontainer from template" ,
5252 Long : "Add a devcontainer definition to the current folder using the specified template" ,
53- Run : func (cmd * cobra.Command , args []string ) {
53+ RunE : func (cmd * cobra.Command , args []string ) error {
5454
5555 if len (args ) != 1 {
56- cmd .Usage ()
57- os .Exit (1 )
56+ return cmd .Usage ()
5857 }
5958 name := args [0 ]
6059
6160 template , err := devcontainers .GetTemplateByName (name )
6261 if err != nil {
63- fmt .Println (err )
64- os .Exit (1 )
62+ return err
6563 }
6664 if template == nil {
6765 fmt .Printf ("Template '%s' not found\n " , name )
6866 }
6967
7068 info , err := os .Stat ("./.devcontainer" )
7169 if info != nil && err == nil {
72- fmt .Println ("Current folder already contains a .devcontainer folder - exiting" )
73- os .Exit (1 )
70+ return fmt .Errorf ("Current folder already contains a .devcontainer folder - exiting" )
7471 }
7572
7673 currentDirectory , err := os .Getwd ()
7774 if err != nil {
78- fmt .Printf ("Error reading current directory: %s\n " , err )
75+ return fmt .Errorf ("Error reading current directory: %s\n " , err )
7976 }
8077 if err = ioutil2 .CopyFolder (template .Path , currentDirectory + "/.devcontainer" ); err != nil {
81- fmt .Printf ("Error copying folder: %s\n " , err )
82- os .Exit (1 )
78+ return fmt .Errorf ("Error copying folder: %s\n " , err )
8379 }
80+ return err
8481 },
8582 ValidArgsFunction : func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
8683 // only completing the first arg (template name)
@@ -108,43 +105,39 @@ func createTemplateAddLinkCommand() *cobra.Command {
108105 Use : "add-link TEMPLATE_NAME" ,
109106 Short : "add-link devcontainer from template" ,
110107 Long : "Symlink a devcontainer definition to the current folder using the specified template" ,
111- Run : func (cmd * cobra.Command , args []string ) {
108+ RunE : func (cmd * cobra.Command , args []string ) error {
112109
113110 if len (args ) != 1 {
114- cmd .Usage ()
115- os .Exit (1 )
111+ return cmd .Usage ()
116112 }
117113 name := args [0 ]
118114
119115 template , err := devcontainers .GetTemplateByName (name )
120116 if err != nil {
121- fmt .Println (err )
122- os .Exit (1 )
117+ return err
123118 }
124119 if template == nil {
125- fmt .Printf ("Template '%s' not found\n " , name )
120+ return fmt .Errorf ("Template '%s' not found\n " , name )
126121 }
127122
128123 info , err := os .Stat ("./.devcontainer" )
129124 if info != nil && err == nil {
130- fmt .Println ("Current folder already contains a .devcontainer folder - exiting" )
131- os .Exit (1 )
125+ return fmt .Errorf ("Current folder already contains a .devcontainer folder - exiting" )
132126 }
133127
134128 currentDirectory , err := os .Getwd ()
135129 if err != nil {
136- fmt .Printf ("Error reading current directory: %s\n " , err )
130+ return fmt .Errorf ("Error reading current directory: %s\n " , err )
137131 }
138132 if err = ioutil2 .LinkFolder (template .Path , currentDirectory + "/.devcontainer" ); err != nil {
139- fmt .Printf ("Error linking folder: %s\n " , err )
140- os .Exit (1 )
133+ return fmt .Errorf ("Error linking folder: %s\n " , err )
141134 }
142135
143136 content := []byte ("*\n " )
144137 if err := ioutil .WriteFile (currentDirectory + "/.devcontainer/.gitignore" , content , 0644 ); err != nil { // -rw-r--r--
145- fmt .Printf ("Error writing .gitignore: %s\n " , err )
146- os .Exit (1 )
138+ return fmt .Errorf ("Error writing .gitignore: %s\n " , err )
147139 }
140+ return err
148141 },
149142 ValidArgsFunction : func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
150143 // only completing the first arg (template name)
0 commit comments