Skip to content

Commit 0a4c0ae

Browse files
authored
Merge pull request #117 from jsuchome/delete-current-cs
Remove Current Codest from config file when it was delete
2 parents 89b2d57 + 6eafd84 commit 0a4c0ae

File tree

3 files changed

+56
-7
lines changed

3 files changed

+56
-7
lines changed

pkg/cli/codeset/delete.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ import (
44
"context"
55
"fmt"
66

7+
"github.com/pkg/errors"
8+
79
codesetc "github.com/fuseml/fuseml-core/gen/http/codeset/client"
810
"github.com/fuseml/fuseml-core/pkg/cli/client"
911
"github.com/fuseml/fuseml-core/pkg/cli/common"
1012
"github.com/spf13/cobra"
13+
"github.com/spf13/viper"
1114
)
1215

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

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

70+
if viper.GetString("CurrentCodeset") == o.Name {
71+
if err := common.DeleteKeyAndWriteConfigFile("CurrentCodeset"); err != nil {
72+
return errors.Wrap(err, "Error writing config file")
73+
}
74+
fmt.Println("No current codeset configured")
75+
}
76+
6777
return nil
6878
}

pkg/cli/codeset/get.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package codeset
22

33
import (
44
"context"
5+
"fmt"
56
"os"
67

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

65+
fmt.Printf("Fetching Codeset %s, under project %s:\n", o.Name, o.Project)
66+
6467
response, err := o.CodesetClient.Get()(context.Background(), request)
6568
if err != nil {
6669
return err

pkg/cli/common/util.go

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package common
22

33
import (
4+
"bytes"
5+
"encoding/json"
46
"fmt"
57
"io/ioutil"
68
"os"
@@ -83,34 +85,68 @@ func LoadFileIntoVar(filePath string, destContent *string) error {
8385
return nil
8486
}
8587

86-
// WriteConfigFile writes new content of the config file.
87-
// If the file does not exist, it is created at default location
88-
// TODO temporary solution until upstream https://github.com/spf13/viper/issues/433 is fixed
89-
func WriteConfigFile() error {
90-
cf := viper.ConfigFileUsed()
88+
// return the file handle of a config file
89+
// if it does not exist yet, creates a new one at default location
90+
func getCurrentOrNewConfigFile() (string, error) {
9191

92+
cf := viper.ConfigFileUsed()
9293
if cf == "" {
9394
fullname := ConfigFileName + "." + ConfigFileType
9495
if dirname, err := os.UserHomeDir(); err == nil {
9596
cf = filepath.Join(dirname, ConfigHomeSubdir, ConfigFuseMLSubdir, fullname)
9697
}
9798
if cf == "" {
98-
return errors.New("Failed to acquire config directory name")
99+
return "", errors.New("Failed to acquire config directory name")
99100
}
100101
configDirPath := filepath.Dir(cf)
101102
if err := os.MkdirAll(configDirPath, os.ModePerm); err != nil {
102-
return err
103+
return "", err
103104
}
104105

105106
fmt.Printf("FuseML configuration file created at %s\n", cf)
106107
}
108+
return cf, nil
109+
}
110+
111+
// WriteConfigFile writes new content of the config file.
112+
// If the file does not exist, it is created at default location
113+
// TODO temporary solution until upstream https://github.com/spf13/viper/issues/433 is fixed
114+
func WriteConfigFile() error {
115+
cf, err := getCurrentOrNewConfigFile()
116+
if err != nil {
117+
return err
118+
}
107119

108120
if err := viper.WriteConfigAs(cf); err != nil {
109121
return err
110122
}
111123
return nil
112124
}
113125

126+
// Writes the current content of file and also deletes given key from it
127+
// As viper does not support this operation directly, here's a workaround
128+
// taken from https://github.com/spf13/viper/issues/632
129+
func DeleteKeyAndWriteConfigFile(key string) error {
130+
cf, err := getCurrentOrNewConfigFile()
131+
if err != nil {
132+
return err
133+
}
134+
135+
configMap := viper.AllSettings()
136+
137+
delete(configMap, strings.ToLower(key))
138+
encodedConfig, _ := json.MarshalIndent(configMap, "", " ")
139+
140+
err = viper.ReadConfig(bytes.NewReader(encodedConfig))
141+
if err != nil {
142+
return err
143+
}
144+
if err := viper.WriteConfigAs(cf); err != nil {
145+
return err
146+
}
147+
return nil
148+
}
149+
114150
// ValidateEnumArgument is used to validate command line arguments that can take a limited set of values
115151
func ValidateEnumArgument(argName, argValue string, values []string) error {
116152
if !util.StringInSlice(argValue, values) {

0 commit comments

Comments
 (0)