Skip to content

Commit

Permalink
Merge pull request #117 from jsuchome/delete-current-cs
Browse files Browse the repository at this point in the history
Remove Current Codest from config file when it was delete
  • Loading branch information
jsuchome authored Nov 18, 2021
2 parents 89b2d57 + 6eafd84 commit 0a4c0ae
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
10 changes: 10 additions & 0 deletions pkg/cli/codeset/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
"context"
"fmt"

"github.com/pkg/errors"

codesetc "github.com/fuseml/fuseml-core/gen/http/codeset/client"
"github.com/fuseml/fuseml-core/pkg/cli/client"
"github.com/fuseml/fuseml-core/pkg/cli/common"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// DeleteOptions holds the options for 'codeset delete' sub command
Expand Down Expand Up @@ -64,5 +67,12 @@ func (o *DeleteOptions) run() error {

fmt.Printf("Codeset %s successfully deleted\n", o.Name)

if viper.GetString("CurrentCodeset") == o.Name {
if err := common.DeleteKeyAndWriteConfigFile("CurrentCodeset"); err != nil {
return errors.Wrap(err, "Error writing config file")
}
fmt.Println("No current codeset configured")
}

return nil
}
3 changes: 3 additions & 0 deletions pkg/cli/codeset/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package codeset

import (
"context"
"fmt"
"os"

codesetc "github.com/fuseml/fuseml-core/gen/http/codeset/client"
Expand Down Expand Up @@ -61,6 +62,8 @@ func (o *GetOptions) run() error {
return err
}

fmt.Printf("Fetching Codeset %s, under project %s:\n", o.Name, o.Project)

response, err := o.CodesetClient.Get()(context.Background(), request)
if err != nil {
return err
Expand Down
50 changes: 43 additions & 7 deletions pkg/cli/common/util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package common

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -83,34 +85,68 @@ 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 location
// TODO temporary solution until upstream https://github.com/spf13/viper/issues/433 is fixed
func WriteConfigFile() error {
cf := viper.ConfigFileUsed()
// return the file handle of a config file
// if it does not exist yet, creates a new one at default location
func getCurrentOrNewConfigFile() (string, error) {

cf := viper.ConfigFileUsed()
if cf == "" {
fullname := ConfigFileName + "." + ConfigFileType
if dirname, err := os.UserHomeDir(); err == nil {
cf = filepath.Join(dirname, ConfigHomeSubdir, ConfigFuseMLSubdir, fullname)
}
if cf == "" {
return errors.New("Failed to acquire config directory name")
return "", errors.New("Failed to acquire config directory name")
}
configDirPath := filepath.Dir(cf)
if err := os.MkdirAll(configDirPath, os.ModePerm); err != nil {
return err
return "", err
}

fmt.Printf("FuseML configuration file created at %s\n", cf)
}
return cf, nil
}

// WriteConfigFile writes new content of the config file.
// 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, err := getCurrentOrNewConfigFile()
if err != nil {
return err
}

if err := viper.WriteConfigAs(cf); err != nil {
return err
}
return nil
}

// Writes the current content of file and also deletes given key from it
// As viper does not support this operation directly, here's a workaround
// taken from https://github.com/spf13/viper/issues/632
func DeleteKeyAndWriteConfigFile(key string) error {
cf, err := getCurrentOrNewConfigFile()
if err != nil {
return err
}

configMap := viper.AllSettings()

delete(configMap, strings.ToLower(key))
encodedConfig, _ := json.MarshalIndent(configMap, "", " ")

err = viper.ReadConfig(bytes.NewReader(encodedConfig))
if err != nil {
return err
}
if err := viper.WriteConfigAs(cf); err != nil {
return err
}
return nil
}

// ValidateEnumArgument is used to validate command line arguments that can take a limited set of values
func ValidateEnumArgument(argName, argValue string, values []string) error {
if !util.StringInSlice(argValue, values) {
Expand Down

0 comments on commit 0a4c0ae

Please sign in to comment.