From 8d72808fd14e5f04a5edfc435b79ed37b30657f0 Mon Sep 17 00:00:00 2001 From: Marc Lopez Rubio Date: Fri, 15 Nov 2019 14:54:30 +0800 Subject: [PATCH] init: Ensure homepath is created (#51) Ensures that the path that holds the configuration file which will be created already exists. Resolves elastic/ecctl#46 Signed-off-by: Marc Lopez --- cmd/init.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/cmd/init.go b/cmd/init.go index d31c09cf..25dd071c 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -18,6 +18,7 @@ package cmd import ( + "os" "path/filepath" "runtime" "strings" @@ -35,7 +36,10 @@ var initCmd = &cobra.Command{ PreRunE: cobra.MaximumNArgs(0), RunE: func(cmd *cobra.Command, args []string) error { fp := strings.Replace(ecctlHomePath, homePrefix, cmdutil.GetHomePath(runtime.GOOS), 1) - fp = filepath.Join(fp, defaultViper.GetString("config")) + if err := os.MkdirAll(fp, 0664); err != nil { + return err + } + return ecctl.InitConfig(ecctl.InitConfigParams{ Client: defaultClient, Viper: defaultViper, @@ -43,7 +47,7 @@ var initCmd = &cobra.Command{ Writer: defaultOutput, ErrWriter: defaultError, PasswordReadFunc: terminal.ReadPassword, - FilePath: fp, + FilePath: filepath.Join(fp, defaultViper.GetString("config")), }) }, }