@@ -2,10 +2,12 @@ package main
22
33import (
44 "fmt"
5- "io/ioutil"
65 "os"
6+ "sort"
77
88 "github.com/spf13/cobra"
9+ "github.com/stuartleeks/devcontainer-cli/internal/pkg/devcontainers"
10+ ioutil2 "github.com/stuartleeks/devcontainer-cli/internal/pkg/ioutil"
911)
1012
1113func createTemplateCommand () * cobra.Command {
@@ -15,38 +17,85 @@ func createTemplateCommand() *cobra.Command {
1517 Long : "Use subcommands to work with devcontainer templates" ,
1618 }
1719 cmd .AddCommand (createTemplateListCommand ())
20+ cmd .AddCommand (createTemplateAddCommand ())
1821 return cmd
1922}
2023
2124func createTemplateListCommand () * cobra.Command {
22- isDevcontainerFolder := func (parentPath string , fi os.FileInfo ) bool {
23- if ! fi .IsDir () {
24- return false
25- }
26- devcontainerJsonPath := fmt .Sprintf ("%s/%s/.devcontainer/devcontainer.json" , parentPath , fi .Name ())
27- devContainerJsonInfo , err := os .Stat (devcontainerJsonPath )
28- return err == nil && ! devContainerJsonInfo .IsDir ()
29- }
25+
3026 cmd := & cobra.Command {
3127 Use : "list" ,
3228 Short : "list templates" ,
3329 Long : "List devcontainer templates" ,
3430 Run : func (cmd * cobra.Command , args []string ) {
35- const containerFolder string = "$HOME/source/vscode-dev-containers/containers" // TODO - make configurable!
3631
37- folder := os .ExpandEnv (containerFolder )
38- c , err := ioutil .ReadDir (folder )
32+ templates , err := devcontainers .GetTemplates ()
33+ if err != nil {
34+ fmt .Println (err )
35+ os .Exit (1 )
36+ }
37+
38+ for _ , template := range templates {
39+ fmt .Println (template .Name )
40+ }
41+ },
42+ }
43+ return cmd
44+ }
45+
46+ func createTemplateAddCommand () * cobra.Command {
47+ cmd := & cobra.Command {
48+ Use : "add TEMPLATE_NAME" ,
49+ Short : "add devcontainer from template" ,
50+ Long : "Add a devcontainer definition to the current folder using the specified template" ,
51+ Run : func (cmd * cobra.Command , args []string ) {
52+
53+ if len (args ) != 1 {
54+ cmd .Usage ()
55+ os .Exit (1 )
56+ }
57+ name := args [0 ]
58+
59+ template , err := devcontainers .GetTemplateByName (name )
3960 if err != nil {
40- fmt .Printf ( "Error reading devcontainer definitions: %s \n " , err )
61+ fmt .Println ( err )
4162 os .Exit (1 )
4263 }
64+ if template == nil {
65+ fmt .Printf ("Template '%s' not found\n " , name )
66+ }
4367
44- for _ , entry := range c {
45- if isDevcontainerFolder ( folder , entry ) {
46- fmt .Println (entry . Name () )
47- }
68+ info , err := os . Stat ( "./.devcontainer" )
69+ if info != nil && err == nil {
70+ fmt .Println ("Current folder already contains a .devcontainer folder - exiting" )
71+ os . Exit ( 1 )
4872 }
4973
74+ currentDirectory , err := os .Getwd ()
75+ if err != nil {
76+ fmt .Printf ("Error reading current directory: %s\n " , err )
77+ }
78+ if err = ioutil2 .CopyFolder (template .Path , currentDirectory + "/.devcontainer" ); err != nil {
79+ fmt .Printf ("Error copying folder: %s\n " , err )
80+ os .Exit (1 )
81+ }
82+ },
83+ ValidArgsFunction : func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
84+ // only completing the first arg (template name)
85+ if len (args ) != 0 {
86+ return nil , cobra .ShellCompDirectiveNoFileComp
87+ }
88+ templates , err := devcontainers .GetTemplates ()
89+ if err != nil {
90+ fmt .Printf ("Error: %v" , err )
91+ os .Exit (1 )
92+ }
93+ names := []string {}
94+ for _ , template := range templates {
95+ names = append (names , template .Name )
96+ }
97+ sort .Strings (names )
98+ return names , cobra .ShellCompDirectiveNoFileComp
5099 },
51100 }
52101 return cmd
0 commit comments