Skip to content

Commit

Permalink
Added HOMEDIR/.flyte to search path for config.yaml (flyteorg#59)
Browse files Browse the repository at this point in the history
Signed-off-by: Prafulla Mahindrakar <[email protected]>
  • Loading branch information
pmahindrakar-oss authored and robert-ulbrich-mercedes-benz committed Jul 2, 2024
1 parent 510d483 commit a8ebb50
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 2 deletions.
10 changes: 8 additions & 2 deletions flytectl/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand All @@ -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())

Expand All @@ -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())
Expand Down
6 changes: 6 additions & 0 deletions flytectl/docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <config-file-path>

Basic Configuration
--------------------
Expand Down
22 changes: 22 additions & 0 deletions flytectl/pkg/filesystemutils/file_system_utils.go
Original file line number Diff line number Diff line change
@@ -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...)
}
42 changes: 42 additions & 0 deletions flytectl/pkg/filesystemutils/flile_system_utils_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}

0 comments on commit a8ebb50

Please sign in to comment.