Skip to content

Commit 73eac92

Browse files
authored
Merge pull request #14 from stuartleeks/sl/template-multi-path
Template improvements
2 parents 0aebb2e + 88ddcf6 commit 73eac92

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

cmd/devcontainer/template.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io/ioutil"
66
"os"
77
"sort"
8+
"text/tabwriter"
89

910
"github.com/spf13/cobra"
1011
"github.com/stuartleeks/devcontainer-cli/internal/pkg/devcontainers"
@@ -24,7 +25,7 @@ func createTemplateCommand() *cobra.Command {
2425
}
2526

2627
func createTemplateListCommand() *cobra.Command {
27-
28+
var listVerbose bool
2829
cmd := &cobra.Command{
2930
Use: "list",
3031
Short: "list templates",
@@ -36,12 +37,28 @@ func createTemplateListCommand() *cobra.Command {
3637
return err
3738
}
3839

40+
if listVerbose {
41+
w := new(tabwriter.Writer)
42+
// minwidth, tabwidth, padding, padchar, flags
43+
w.Init(os.Stdout, 8, 8, 0, '\t', 0)
44+
defer w.Flush()
45+
46+
fmt.Fprintf(w, "%s\t%s\n", "TEMPLATE NAME", "PATH")
47+
fmt.Fprintf(w, "%s\t%s\n", "-------------", "----")
48+
49+
for _, template := range templates {
50+
fmt.Fprintf(w, "%s\t%s\n", template.Name, template.Path)
51+
}
52+
return nil
53+
}
54+
3955
for _, template := range templates {
4056
fmt.Println(template.Name)
4157
}
4258
return nil
4359
},
4460
}
61+
cmd.Flags().BoolVarP(&listVerbose, "verbose", "v", false, "Verbose output")
4562
return cmd
4663
}
4764

internal/pkg/devcontainers/template.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io/ioutil"
66
"os"
77
"path/filepath"
8+
"sort"
89

910
"github.com/stuartleeks/devcontainer-cli/internal/pkg/config"
1011
"github.com/stuartleeks/devcontainer-cli/internal/pkg/errors"
@@ -35,6 +36,8 @@ func GetTemplateByName(name string) (*DevcontainerTemplate, error) {
3536
// GetTemplates returns a list of discovered templates
3637
func GetTemplates() ([]DevcontainerTemplate, error) {
3738
templates := []DevcontainerTemplate{}
39+
templateNames := map[string]bool{}
40+
3841
folders := config.GetTemplateFolders()
3942
if len(folders) == 0 {
4043
return []DevcontainerTemplate{}, &errors.StatusError{Message: "No template folders configured - see https://github.com/stuartleeks/devcontainer-cli/#working-with-devcontainer-templates"}
@@ -45,8 +48,14 @@ func GetTemplates() ([]DevcontainerTemplate, error) {
4548
if err != nil {
4649
return []DevcontainerTemplate{}, err
4750
}
48-
templates = append(templates, newTemplates...)
51+
for _, template := range newTemplates {
52+
if !templateNames[template.Name] {
53+
templateNames[template.Name] = true
54+
templates = append(templates, template)
55+
}
56+
}
4957
}
58+
sort.Slice(templates, func(i int, j int) bool { return templates[i].Name < templates[j].Name })
5059
return templates, nil
5160
}
5261

0 commit comments

Comments
 (0)