Skip to content

Commit a318a5f

Browse files
authored
cmd: Fix init command on unexisting .ecctl folder (#64)
There was a small mistake in the directory permission mask when the folder `~/.ecctl` does not exist, it was previously creating it without the execution premission which prevents anyone from actually accessing the folder. Additionally it prints the location of the configuration that's been written down at the end of initializing the config. Signed-off-by: Marc Lopez <marc5.12@outlook.com>
1 parent 8dcfa6e commit a318a5f

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

cmd/init.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package cmd
1919

2020
import (
21+
"fmt"
2122
"os"
2223
"path/filepath"
2324
"runtime"
@@ -36,11 +37,11 @@ var initCmd = &cobra.Command{
3637
PreRunE: cobra.MaximumNArgs(0),
3738
RunE: func(cmd *cobra.Command, args []string) error {
3839
fp := strings.Replace(ecctlHomePath, homePrefix, cmdutil.GetHomePath(runtime.GOOS), 1)
39-
if err := os.MkdirAll(fp, 0664); err != nil {
40+
if err := os.MkdirAll(fp, 0775); err != nil {
4041
return err
4142
}
4243

43-
return ecctl.InitConfig(ecctl.InitConfigParams{
44+
err := ecctl.InitConfig(ecctl.InitConfigParams{
4445
Client: defaultClient,
4546
Viper: defaultViper,
4647
Reader: defaultInput,
@@ -49,6 +50,22 @@ var initCmd = &cobra.Command{
4950
PasswordReadFunc: terminal.ReadPassword,
5051
FilePath: filepath.Join(fp, defaultViper.GetString("config")),
5152
})
53+
if err != nil {
54+
return err
55+
}
56+
57+
// Only print this message when viper.ConfigFileUsed() since it means
58+
// the config hasn't been read via viper, thus just recently written
59+
// down in the path.
60+
if cfg := defaultViper.ConfigFileUsed(); cfg == "" {
61+
if err := defaultViper.ReadInConfig(); err != nil {
62+
return err
63+
}
64+
cfg = defaultViper.ConfigFileUsed()
65+
fmt.Fprintln(defaultOutput, "\nConfig written to", cfg)
66+
}
67+
68+
return nil
5269
},
5370
}
5471

0 commit comments

Comments
 (0)