@@ -6,13 +6,15 @@ import (
66 "path/filepath"
77 "strings"
88
9+ "github.com/steady-bytes/draft/tools/dctl/config"
910 "github.com/steady-bytes/draft/tools/dctl/docker"
1011 "github.com/steady-bytes/draft/tools/dctl/output"
1112
1213 "github.com/spf13/cobra"
1314)
1415
1516func Init (cmd * cobra.Command , args []string ) error {
17+ defineServices ()
1618 ctx := cmd .Context ()
1719 dockerCtl , err := docker .NewDockerController ()
1820 if err != nil {
@@ -42,21 +44,21 @@ func Init(cmd *cobra.Command, args []string) error {
4244 home , err := os .UserHomeDir ()
4345 if err != nil {
4446 output .Error (err )
45- os . Exit ( 1 )
47+ continue
4648 }
4749
4850 dirName := filepath .Join (home , ".config" , "dctl" , "infra" )
4951 err = os .Mkdir (dirName , 0666 )
5052 if err != nil && ! os .IsExist (err ) {
5153 output .Error (err )
52- os . Exit ( 1 )
54+ continue
5355 }
5456
5557 fileName := filepath .Join (dirName , fmt .Sprintf ("%s.yaml" , name ))
5658 err = os .WriteFile (fileName , []byte (config .configFile .contents ), 0666 )
5759 if err != nil {
5860 output .Error (err )
59- os . Exit ( 1 )
61+ continue
6062 }
6163 }
6264
@@ -65,17 +67,38 @@ func Init(cmd *cobra.Command, args []string) error {
6567 home , err := os .UserHomeDir ()
6668 if err != nil {
6769 output .Error (err )
68- os . Exit ( 1 )
70+ continue
6971 }
7072
7173 dirName := filepath .Join (home , ".config" , "dctl" , "infra" , name )
7274 err = os .MkdirAll (dirName , 0666 )
7375 if err != nil && ! os .IsExist (err ) {
7476 output .Error (err )
75- os . Exit ( 1 )
77+ continue
7678 }
7779 }
7880 }
7981
8082 return nil
8183}
84+
85+ // defineServices sets the Services variable based off of the user
86+ // input and current context.
87+ func defineServices () {
88+ dctx := config .GetContext ()
89+
90+ // if user didn't define services, use the ones defined in the context
91+ if len (Services ) == 0 {
92+ Services = make ([]string , len (dctx .Infra ))
93+ i := 0
94+ for key := range dctx .Infra {
95+ Services [i ] = key
96+ i ++
97+ }
98+ }
99+
100+ // if neither user nor context defined services, use default
101+ if len (Services ) == 0 {
102+ Services = defaultServices
103+ }
104+ }
0 commit comments