Skip to content

Commit 015762d

Browse files
authored
Merge pull request #6 from stuartleeks/sl/templates
Add support for working with templates
2 parents aca7cac + 8458c99 commit 015762d

File tree

8 files changed

+445
-1
lines changed

8 files changed

+445
-1
lines changed

cmd/devcontainer/config.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"os"
7+
8+
"github.com/spf13/cobra"
9+
"github.com/stuartleeks/devcontainer-cli/internal/pkg/config"
10+
)
11+
12+
func createConfigCommand() *cobra.Command {
13+
cmd := &cobra.Command{
14+
Use: "config",
15+
}
16+
cmd.AddCommand(createConfigShowCommand())
17+
cmd.AddCommand(createConfigWriteCommand())
18+
return cmd
19+
}
20+
func createConfigShowCommand() *cobra.Command {
21+
cmd := &cobra.Command{
22+
Use: "show",
23+
Short: "show the current config",
24+
Long: "load the current config and print it out",
25+
Run: func(cmd *cobra.Command, args []string) {
26+
c := config.GetAll()
27+
jsonConfig, err := json.MarshalIndent(c, "", " ")
28+
if err != nil {
29+
fmt.Printf("Error converting to JSON: %s\n", err)
30+
os.Exit(1)
31+
}
32+
fmt.Println(string(jsonConfig))
33+
},
34+
}
35+
return cmd
36+
}
37+
func createConfigWriteCommand() *cobra.Command {
38+
cmd := &cobra.Command{
39+
Use: "write",
40+
Short: "write config",
41+
Long: "Write out the config file to ~/.devcontainer-cli/devcontainer-cli.json",
42+
Run: func(cmd *cobra.Command, args []string) {
43+
if err := config.SaveConfig(); err != nil {
44+
fmt.Printf("Error saving config: %s\n", err)
45+
} else {
46+
fmt.Println("Config saved")
47+
}
48+
},
49+
}
50+
return cmd
51+
52+
}

cmd/devcontainer/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ func main() {
1010

1111
rootCmd.AddCommand(createListCommand())
1212
rootCmd.AddCommand(createExecCommand())
13+
rootCmd.AddCommand(createTemplateCommand())
1314
rootCmd.AddCommand(createCompleteCommand(rootCmd))
15+
rootCmd.AddCommand(createConfigCommand())
1416

1517
rootCmd.Execute()
1618
}

cmd/devcontainer/template.go

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"os"
7+
"sort"
8+
9+
"github.com/spf13/cobra"
10+
"github.com/stuartleeks/devcontainer-cli/internal/pkg/devcontainers"
11+
ioutil2 "github.com/stuartleeks/devcontainer-cli/internal/pkg/ioutil"
12+
)
13+
14+
func createTemplateCommand() *cobra.Command {
15+
cmd := &cobra.Command{
16+
Use: "template",
17+
Short: "work with templates",
18+
Long: "Use subcommands to work with devcontainer templates",
19+
}
20+
cmd.AddCommand(createTemplateListCommand())
21+
cmd.AddCommand(createTemplateAddCommand())
22+
cmd.AddCommand(createTemplateAddLinkCommand())
23+
return cmd
24+
}
25+
26+
func createTemplateListCommand() *cobra.Command {
27+
28+
cmd := &cobra.Command{
29+
Use: "list",
30+
Short: "list templates",
31+
Long: "List devcontainer templates",
32+
Run: func(cmd *cobra.Command, args []string) {
33+
34+
templates, err := devcontainers.GetTemplates()
35+
if err != nil {
36+
fmt.Println(err)
37+
os.Exit(1)
38+
}
39+
40+
for _, template := range templates {
41+
fmt.Println(template.Name)
42+
}
43+
},
44+
}
45+
return cmd
46+
}
47+
48+
func createTemplateAddCommand() *cobra.Command {
49+
cmd := &cobra.Command{
50+
Use: "add TEMPLATE_NAME",
51+
Short: "add devcontainer from template",
52+
Long: "Add a devcontainer definition to the current folder using the specified template",
53+
Run: func(cmd *cobra.Command, args []string) {
54+
55+
if len(args) != 1 {
56+
cmd.Usage()
57+
os.Exit(1)
58+
}
59+
name := args[0]
60+
61+
template, err := devcontainers.GetTemplateByName(name)
62+
if err != nil {
63+
fmt.Println(err)
64+
os.Exit(1)
65+
}
66+
if template == nil {
67+
fmt.Printf("Template '%s' not found\n", name)
68+
}
69+
70+
info, err := os.Stat("./.devcontainer")
71+
if info != nil && err == nil {
72+
fmt.Println("Current folder already contains a .devcontainer folder - exiting")
73+
os.Exit(1)
74+
}
75+
76+
currentDirectory, err := os.Getwd()
77+
if err != nil {
78+
fmt.Printf("Error reading current directory: %s\n", err)
79+
}
80+
if err = ioutil2.CopyFolder(template.Path, currentDirectory+"/.devcontainer"); err != nil {
81+
fmt.Printf("Error copying folder: %s\n", err)
82+
os.Exit(1)
83+
}
84+
},
85+
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
86+
// only completing the first arg (template name)
87+
if len(args) != 0 {
88+
return nil, cobra.ShellCompDirectiveNoFileComp
89+
}
90+
templates, err := devcontainers.GetTemplates()
91+
if err != nil {
92+
fmt.Printf("Error: %v", err)
93+
os.Exit(1)
94+
}
95+
names := []string{}
96+
for _, template := range templates {
97+
names = append(names, template.Name)
98+
}
99+
sort.Strings(names)
100+
return names, cobra.ShellCompDirectiveNoFileComp
101+
},
102+
}
103+
return cmd
104+
}
105+
106+
func createTemplateAddLinkCommand() *cobra.Command {
107+
cmd := &cobra.Command{
108+
Use: "add-link TEMPLATE_NAME",
109+
Short: "add-link devcontainer from template",
110+
Long: "Symlink a devcontainer definition to the current folder using the specified template",
111+
Run: func(cmd *cobra.Command, args []string) {
112+
113+
if len(args) != 1 {
114+
cmd.Usage()
115+
os.Exit(1)
116+
}
117+
name := args[0]
118+
119+
template, err := devcontainers.GetTemplateByName(name)
120+
if err != nil {
121+
fmt.Println(err)
122+
os.Exit(1)
123+
}
124+
if template == nil {
125+
fmt.Printf("Template '%s' not found\n", name)
126+
}
127+
128+
info, err := os.Stat("./.devcontainer")
129+
if info != nil && err == nil {
130+
fmt.Println("Current folder already contains a .devcontainer folder - exiting")
131+
os.Exit(1)
132+
}
133+
134+
currentDirectory, err := os.Getwd()
135+
if err != nil {
136+
fmt.Printf("Error reading current directory: %s\n", err)
137+
}
138+
if err = ioutil2.LinkFolder(template.Path, currentDirectory+"/.devcontainer"); err != nil {
139+
fmt.Printf("Error linking folder: %s\n", err)
140+
os.Exit(1)
141+
}
142+
143+
content := []byte("*\n")
144+
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)
147+
}
148+
},
149+
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
150+
// only completing the first arg (template name)
151+
if len(args) != 0 {
152+
return nil, cobra.ShellCompDirectiveNoFileComp
153+
}
154+
templates, err := devcontainers.GetTemplates()
155+
if err != nil {
156+
fmt.Printf("Error: %v", err)
157+
os.Exit(1)
158+
}
159+
names := []string{}
160+
for _, template := range templates {
161+
names = append(names, template.Name)
162+
}
163+
sort.Strings(names)
164+
return names, cobra.ShellCompDirectiveNoFileComp
165+
},
166+
}
167+
return cmd
168+
}

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ module github.com/stuartleeks/devcontainer-cli
22

33
go 1.14
44

5-
require github.com/spf13/cobra v1.0.0
5+
require (
6+
github.com/spf13/cobra v1.0.0
7+
github.com/spf13/viper v1.4.0
8+
)

go.sum

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
2+
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
23
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
34
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
45
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
@@ -14,9 +15,11 @@ github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3Ee
1415
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
1516
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
1617
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
18+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1719
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1820
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
1921
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
22+
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
2023
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
2124
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
2225
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
@@ -36,6 +39,7 @@ github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoA
3639
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
3740
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
3841
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
42+
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
3943
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
4044
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
4145
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
@@ -45,17 +49,23 @@ github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvW
4549
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
4650
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
4751
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
52+
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
4853
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
4954
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
55+
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
5056
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
57+
github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY=
5158
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
5259
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
5360
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
61+
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
5462
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
5563
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
5664
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
65+
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
5766
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
5867
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
68+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5969
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
6070
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
6171
github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso=
@@ -72,15 +82,20 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeV
7282
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
7383
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
7484
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
85+
github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI=
7586
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
87+
github.com/spf13/cast v1.3.0 h1:oget//CVOEoFewqQxwr0Ej5yjygnqGkvggSE/gB35Q8=
7688
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
7789
github.com/spf13/cobra v1.0.0 h1:6m/oheQuQ13N9ks4hubMG6BnvwOeaJrqSPLahSnczz8=
7890
github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE=
91+
github.com/spf13/jwalterweatherman v1.0.0 h1:XHEdyB+EcvlqZamSM4ZOMGlc93t6AcsBEu9Gc1vn7yk=
7992
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
8093
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
8194
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
95+
github.com/spf13/viper v1.4.0 h1:yXHLWeravcrgGyFSyCgdYpXQ9dR9c/WED3pg1RhxqEU=
8296
github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE=
8397
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
98+
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
8499
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
85100
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
86101
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
@@ -107,7 +122,9 @@ golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5h
107122
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
108123
golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
109124
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
125+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
110126
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
127+
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
111128
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
112129
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
113130
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
@@ -119,9 +136,11 @@ google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZi
119136
google.golang.org/grpc v1.21.0/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM=
120137
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
121138
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
139+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
122140
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
123141
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
124142
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
125143
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
144+
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
126145
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
127146
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

internal/pkg/config/config.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package config
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/spf13/viper"
8+
)
9+
10+
var initialised bool = false
11+
12+
// EnsureInitialised reads the config. Will quit if config is invalid
13+
func EnsureInitialised() {
14+
if !initialised {
15+
viper.SetConfigName("devcontainer-cli")
16+
viper.SetConfigType("json")
17+
18+
viper.AddConfigPath(getConfigPath())
19+
20+
viper.SetDefault("templatePaths", []string{})
21+
22+
// TODO - allow env var for config
23+
if err := viper.ReadInConfig(); err != nil {
24+
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
25+
// Config file not found; ignore error if desired
26+
} else {
27+
fmt.Printf("Error loading config file: %s\n", err)
28+
os.Exit(1)
29+
}
30+
}
31+
initialised = true
32+
}
33+
}
34+
func getConfigPath() string {
35+
if os.Getenv("HOME") != "" {
36+
return "$HOME/.devcontainer-cli/"
37+
}
38+
// if HOME not set, assume Windows and use USERPROFILE env var
39+
return "$USERPROFILE/.devcontainer-cli/"
40+
}
41+
42+
func GetTemplateFolders() []string {
43+
EnsureInitialised()
44+
return viper.GetStringSlice("templatePaths")
45+
}
46+
func GetAll() map[string]interface{} {
47+
EnsureInitialised()
48+
return viper.AllSettings()
49+
}
50+
51+
func SaveConfig() error {
52+
EnsureInitialised()
53+
configPath := getConfigPath()
54+
configPath = os.ExpandEnv(configPath)
55+
if err := os.MkdirAll(configPath, 0755); err != nil {
56+
return err
57+
}
58+
return viper.WriteConfigAs("/home/stuart/.devcontainer-cli/devcontainer-cli.json")
59+
}

0 commit comments

Comments
 (0)