Skip to content

Commit

Permalink
Make sure config file exists before attempting to write it
Browse files Browse the repository at this point in the history
This is a workaround for spf13/viper#433;
once it is fixed, viper.WriteConfig should be able to create the config
file if it does not exist.
  • Loading branch information
jsuchome committed Jun 17, 2021
1 parent f456385 commit 5c5d8a5
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pkg/cli/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func initializeConfig(cmd *cobra.Command) error {
viper.SetConfigName(common.ConfigFileName)

// Set the config format and extension
viper.SetConfigType("yaml")
viper.SetConfigType(common.ConfigFileType)

// Set paths where viper should look for the config file.
if dirname, err := os.Getwd(); err == nil {
Expand Down
8 changes: 5 additions & 3 deletions pkg/cli/codeset/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package codeset
import (
"fmt"

"github.com/fuseml/fuseml-core/pkg/cli/common"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/fuseml/fuseml-core/pkg/cli/common"
)

// SetOptions holds the options for 'codeset set' sub command
Expand Down Expand Up @@ -57,8 +59,8 @@ func (o *SetOptions) run() error {

viper.Set("CurrentCodeset", o.Name)

if err := viper.WriteConfig(); err != nil {
return err
if err := common.WriteConfigFile(); err != nil {
return errors.Wrap(err, "Error writing config file")
}

fmt.Printf("Current codeset set to %s.\n", o.Name)
Expand Down
2 changes: 2 additions & 0 deletions pkg/cli/common/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import "fmt"
const (
// ConfigFileName is the name of the FuseML configuration file (without extension)
ConfigFileName = "config"
// ConfigFileType is the type of default config file
ConfigFileType = "yaml"
// ConfigHomeSubdir is the subdirectory where the FuseML configuration files is located
ConfigHomeSubdir = ".fuseml"
// DefaultHTTPTimeout is the default HTTP timeout value
Expand Down
27 changes: 27 additions & 0 deletions pkg/cli/common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/spf13/viper"
)

// CheckErr prints a user friendly error to STDERR and exits with a non-zero
Expand Down Expand Up @@ -70,3 +73,27 @@ func LoadFileIntoVar(filePath string, destContent *string) error {

return nil
}

// WriteConfigFile writes new content of the config file.
// If the file does not exist, it is created at default locatio.
// If the file does not exist, it is created at default location
// TODO temporary solution until upstream https://github.com/spf13/viper/issues/433 is fixed
func WriteConfigFile() error {
cf := viper.ConfigFileUsed()

if cf == "" {
fullname := ConfigFileName + "." + ConfigFileType
if dirname, err := os.Getwd(); err == nil {
cf = filepath.Join(dirname, ConfigHomeSubdir, fullname)
}
if dirname, err := os.UserHomeDir(); err == nil {
cf = filepath.Join(dirname, ConfigHomeSubdir, fullname)
}
fmt.Printf("New config file to be created at %s\n", cf)
}

if err := viper.WriteConfigAs(cf); err != nil {
return err
}
return nil
}
4 changes: 2 additions & 2 deletions pkg/cli/project/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func (o *SetOptions) run() error {

viper.Set("CurrentProject", o.Name)

if err := viper.WriteConfig(); err != nil {
return err
if err := common.WriteConfigFile(); err != nil {
return errors.Wrap(err, "Error writing config file")
}

fmt.Printf("Current project set to %s.\n", o.Name)
Expand Down

0 comments on commit 5c5d8a5

Please sign in to comment.