|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "sort" |
| 7 | + "text/tabwriter" |
| 8 | + |
| 9 | + "github.com/spf13/cobra" |
| 10 | + "github.com/stuartleeks/devcontainer-cli/internal/pkg/devcontainers" |
| 11 | +) |
| 12 | + |
| 13 | +func createSnippetCommand() *cobra.Command { |
| 14 | + cmd := &cobra.Command{ |
| 15 | + Use: "snippet", |
| 16 | + Short: "work with snippets (experimental)", |
| 17 | + Long: "Use subcommands to work with devcontainer snippets (experimental)", |
| 18 | + } |
| 19 | + cmd.AddCommand(createSnippetListCommand()) |
| 20 | + cmd.AddCommand(createSnippetAddCommand()) |
| 21 | + return cmd |
| 22 | +} |
| 23 | + |
| 24 | +func createSnippetListCommand() *cobra.Command { |
| 25 | + var listVerbose bool |
| 26 | + cmd := &cobra.Command{ |
| 27 | + Use: "list", |
| 28 | + Short: "list snippets", |
| 29 | + Long: "List devcontainer snippets", |
| 30 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 31 | + |
| 32 | + snippets, err := devcontainers.GetSnippets() |
| 33 | + if err != nil { |
| 34 | + return err |
| 35 | + } |
| 36 | + |
| 37 | + if listVerbose { |
| 38 | + w := new(tabwriter.Writer) |
| 39 | + // minwidth, tabwidth, padding, padchar, flags |
| 40 | + w.Init(os.Stdout, 8, 8, 0, '\t', 0) |
| 41 | + defer w.Flush() |
| 42 | + |
| 43 | + fmt.Fprintf(w, "%s\t%s\n", "SNIPPET NAME", "PATH") |
| 44 | + fmt.Fprintf(w, "%s\t%s\n", "-------------", "----") |
| 45 | + |
| 46 | + for _, snippet := range snippets { |
| 47 | + fmt.Fprintf(w, "%s\t%s\n", snippet.Name, snippet.Path) |
| 48 | + } |
| 49 | + return nil |
| 50 | + } |
| 51 | + |
| 52 | + for _, snippet := range snippets { |
| 53 | + fmt.Println(snippet.Name) |
| 54 | + } |
| 55 | + return nil |
| 56 | + }, |
| 57 | + } |
| 58 | + cmd.Flags().BoolVarP(&listVerbose, "verbose", "v", false, "Verbose output") |
| 59 | + return cmd |
| 60 | +} |
| 61 | + |
| 62 | +func createSnippetAddCommand() *cobra.Command { |
| 63 | + var devcontainerName string |
| 64 | + cmd := &cobra.Command{ |
| 65 | + Use: "add SNIPPET_NAME", |
| 66 | + Short: "add snippet to devcontainer", |
| 67 | + Long: "Add a snippet to the devcontainer definition for the current folder", |
| 68 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 69 | + |
| 70 | + if len(args) != 1 { |
| 71 | + return cmd.Usage() |
| 72 | + } |
| 73 | + name := args[0] |
| 74 | + |
| 75 | + currentDirectory, err := os.Getwd() |
| 76 | + if err != nil { |
| 77 | + return fmt.Errorf("Error reading current directory: %s\n", err) |
| 78 | + } |
| 79 | + |
| 80 | + err = devcontainers.AddSnippetToDevcontainer(currentDirectory, name) |
| 81 | + if err != nil { |
| 82 | + return fmt.Errorf("Error setting devcontainer name: %s", err) |
| 83 | + } |
| 84 | + |
| 85 | + return nil |
| 86 | + }, |
| 87 | + ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 88 | + // only completing the first arg (template name) |
| 89 | + if len(args) != 0 { |
| 90 | + return nil, cobra.ShellCompDirectiveNoFileComp |
| 91 | + } |
| 92 | + templates, err := devcontainers.GetSnippets() |
| 93 | + if err != nil { |
| 94 | + os.Exit(1) |
| 95 | + } |
| 96 | + names := []string{} |
| 97 | + for _, template := range templates { |
| 98 | + names = append(names, template.Name) |
| 99 | + } |
| 100 | + sort.Strings(names) |
| 101 | + return names, cobra.ShellCompDirectiveNoFileComp |
| 102 | + }, |
| 103 | + } |
| 104 | + cmd.Flags().StringVar(&devcontainerName, "devcontainer-name", "", "Value to set the devcontainer.json name property to (default is folder name)") |
| 105 | + return cmd |
| 106 | +} |
0 commit comments