Skip to content

Commit d65f103

Browse files
authored
fix: ensure proper handling for profiles (#62)
Fixes #61
1 parent 14440bc commit d65f103

File tree

3 files changed

+46
-16
lines changed

3 files changed

+46
-16
lines changed

cmd/profile.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,13 @@ var AddProfileCmd = &cobra.Command{
114114
if fileConfig.DefaultProfile == "" {
115115
fileConfig.DefaultProfile = name
116116
}
117-
config.WriteConfigToFile(fileConfig)
117+
118+
err = config.WriteConfigToFile(fileConfig)
119+
if err != nil {
120+
fmt.Printf("add profile %s failed\n, err: %v\n", StyleBold.Render(name), err)
121+
return err
122+
}
123+
fmt.Printf("Added profile %s\n", StyleBold.Render(name))
118124

119125
return nil
120126
},
@@ -139,7 +145,11 @@ var RemoveProfileCmd = &cobra.Command{
139145
if len(fileConfig.Profiles) == 0 {
140146
fileConfig.DefaultProfile = ""
141147
}
142-
config.WriteConfigToFile(fileConfig)
148+
err = config.WriteConfigToFile(fileConfig)
149+
if err != nil {
150+
fmt.Printf("delete profile %s failed\n, err: %v\n", StyleBold.Render(name), err)
151+
return err
152+
}
143153
fmt.Printf("Deleted profile %s\n", StyleBold.Render(name))
144154
} else {
145155
fmt.Printf("No profile found with the name: %s", StyleBold.Render(name))

main.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package main
1818

1919
import (
2020
"errors"
21+
"fmt"
2122
"os"
2223
"pb/cmd"
2324
"pb/pkg/config"
@@ -140,18 +141,31 @@ func main() {
140141
Profiles: map[string]config.Profile{"demo": defaultInitialProfile()},
141142
DefaultProfile: "demo",
142143
}
143-
config.WriteConfigToFile(&conf)
144+
err = config.WriteConfigToFile(&conf)
145+
if err != nil {
146+
fmt.Printf("failed to write to file %v\n", err)
147+
os.Exit(1)
148+
}
144149
} else {
145-
// updates the demo profile for existing users
146-
_, exists := previousConfig.Profiles["demo"]
150+
// Only update the "demo" profile without overwriting other profiles
151+
demoProfile, exists := previousConfig.Profiles["demo"]
147152
if exists {
148-
conf := config.Profile{
149-
URL: "http://demo.parseable.com",
150-
Username: "admin",
151-
Password: "admin",
152-
}
153-
previousConfig.Profiles["demo"] = conf
154-
config.WriteConfigToFile(previousConfig)
153+
// Update fields in the demo profile only
154+
demoProfile.URL = "http://demo.parseable.com"
155+
demoProfile.Username = "admin"
156+
demoProfile.Password = "admin"
157+
previousConfig.Profiles["demo"] = demoProfile
158+
} else {
159+
// Add the "demo" profile if it doesn't exist
160+
previousConfig.Profiles["demo"] = defaultInitialProfile()
161+
previousConfig.DefaultProfile = "demo" // Optional: set as default if needed
162+
}
163+
164+
// Write the updated configuration back to file
165+
err = config.WriteConfigToFile(previousConfig)
166+
if err != nil {
167+
fmt.Printf("failed to write to existing file %v\n", err)
168+
os.Exit(1)
155169
}
156170
}
157171

pkg/config/config.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,18 @@ func WriteConfigToFile(config *Config) error {
9090
func ReadConfigFromFile() (config *Config, err error) {
9191
filePath, err := Path()
9292
if err != nil {
93-
return
93+
return &Config{}, err
9494
}
95+
9596
data, err := os.ReadFile(filePath)
9697
if err != nil {
97-
return
98+
return &Config{}, err
9899
}
99-
toml.Unmarshal(data, &config)
100-
return
100+
101+
err = toml.Unmarshal(data, &config)
102+
if err != nil {
103+
return &Config{}, err
104+
}
105+
106+
return config, nil
101107
}

0 commit comments

Comments
 (0)