diff --git a/cmd/root.go b/cmd/root.go index 8ad0fef299..56a371a75a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -12,6 +12,7 @@ import ( "github.com/flyteorg/flytectl/cmd/register" "github.com/flyteorg/flytectl/cmd/update" "github.com/flyteorg/flytectl/cmd/version" + f "github.com/flyteorg/flytectl/pkg/filesystemutils" "github.com/flyteorg/flytectl/pkg/printer" stdConfig "github.com/flyteorg/flytestdlib/config" "github.com/flyteorg/flytestdlib/config/viper" @@ -26,6 +27,11 @@ var ( configAccessor = viper.NewAccessor(stdConfig.Options{StrictMode: true}) ) +const ( + configFileDir = ".flyte" + configFileName = "config.yaml" +) + func newRootCmd() *cobra.Command { rootCmd := &cobra.Command{ PersistentPreRunE: initConfig, @@ -36,7 +42,7 @@ func newRootCmd() *cobra.Command { } rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", - "config file (default is $HOME/config.yaml)") + "config file (default is $HOME/.flyte/config.yaml)") configAccessor.InitializePflags(rootCmd.PersistentFlags()) @@ -63,7 +69,7 @@ func newRootCmd() *cobra.Command { func initConfig(_ *cobra.Command, _ []string) error { configAccessor = viper.NewAccessor(stdConfig.Options{ StrictMode: true, - SearchPaths: []string{cfgFile}, + SearchPaths: []string{cfgFile, f.FilePathJoin(f.UserHomeDir(), configFileDir, configFileName)}, }) err := configAccessor.UpdateConfig(context.TODO()) diff --git a/docs/source/index.rst b/docs/source/index.rst index 44d061489d..a3a3d61cc1 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -19,6 +19,12 @@ Configure ========= Flytectl allows configuring using a YAML file or pass every configuration value on command-line. The follow configuration is useful to setup. +Place this in $HOME/.flyte directory with name config.yaml. +This file is searched in +1] $HOME/.flyte +2] currDir from where you run flytectl +3] /etc/flyte/config +4] You can pass it commandline using --config Basic Configuration -------------------- diff --git a/pkg/filesystemutils/file_system_utils.go b/pkg/filesystemutils/file_system_utils.go new file mode 100644 index 0000000000..2f0b756c90 --- /dev/null +++ b/pkg/filesystemutils/file_system_utils.go @@ -0,0 +1,22 @@ +package filesystemutils + +import ( + "os" + "path/filepath" +) + +var osUserHomDirFunc = os.UserHomeDir +var filePathJoinFunc = filepath.Join + +// UserHomeDir Returns the users home directory or on error returns the current dir +func UserHomeDir() string { + if homeDir, err := osUserHomDirFunc(); err == nil { + return homeDir + } + return "." +} + +// FilePathJoin Returns the file path obtained by joining various path elements. +func FilePathJoin(elems ...string) string { + return filePathJoinFunc(elems...) +} diff --git a/pkg/filesystemutils/flile_system_utils_test.go b/pkg/filesystemutils/flile_system_utils_test.go new file mode 100644 index 0000000000..9698d1709d --- /dev/null +++ b/pkg/filesystemutils/flile_system_utils_test.go @@ -0,0 +1,42 @@ +package filesystemutils + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" +) + +var ( + homeDirVal = "/home/user" + homeDirErr error +) + +func FakeUserHomeDir() (string, error) { + return homeDirVal, homeDirErr +} + +func TestUserHomeDir(t *testing.T) { + t.Run("User home dir", func(t *testing.T) { + osUserHomDirFunc = FakeUserHomeDir + homeDir := UserHomeDir() + assert.Equal(t, homeDirVal, homeDir) + }) + t.Run("User home dir fail", func(t *testing.T) { + homeDirErr = fmt.Errorf("failed to get users home directory") + homeDirVal = "." + osUserHomDirFunc = FakeUserHomeDir + homeDir := UserHomeDir() + assert.Equal(t, ".", homeDir) + // Reset + homeDirErr = nil + homeDirVal = "/home/user" + }) +} + +func TestFilePathJoin(t *testing.T) { + t.Run("File path join", func(t *testing.T) { + homeDir := FilePathJoin("/", "home", "user") + assert.Equal(t, "/home/user", homeDir) + }) +}