Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions cmd/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,13 @@ var AddProfileCmd = &cobra.Command{
if fileConfig.DefaultProfile == "" {
fileConfig.DefaultProfile = name
}
config.WriteConfigToFile(fileConfig)

err = config.WriteConfigToFile(fileConfig)
if err != nil {
fmt.Printf("add profile %s failed\n, err: %v\n", StyleBold.Render(name), err)
return err
}
fmt.Printf("Added profile %s\n", StyleBold.Render(name))

return nil
},
Expand All @@ -139,7 +145,11 @@ var RemoveProfileCmd = &cobra.Command{
if len(fileConfig.Profiles) == 0 {
fileConfig.DefaultProfile = ""
}
config.WriteConfigToFile(fileConfig)
err = config.WriteConfigToFile(fileConfig)
if err != nil {
fmt.Printf("delete profile %s failed\n, err: %v\n", StyleBold.Render(name), err)
return err
}
fmt.Printf("Deleted profile %s\n", StyleBold.Render(name))
} else {
fmt.Printf("No profile found with the name: %s", StyleBold.Render(name))
Expand Down
34 changes: 24 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"errors"
"fmt"
"os"
"pb/cmd"
"pb/pkg/config"
Expand Down Expand Up @@ -140,18 +141,31 @@ func main() {
Profiles: map[string]config.Profile{"demo": defaultInitialProfile()},
DefaultProfile: "demo",
}
config.WriteConfigToFile(&conf)
err = config.WriteConfigToFile(&conf)
if err != nil {
fmt.Printf("failed to write to file %v\n", err)
os.Exit(1)
}
} else {
// updates the demo profile for existing users
_, exists := previousConfig.Profiles["demo"]
// Only update the "demo" profile without overwriting other profiles
demoProfile, exists := previousConfig.Profiles["demo"]
if exists {
conf := config.Profile{
URL: "http://demo.parseable.com",
Username: "admin",
Password: "admin",
}
previousConfig.Profiles["demo"] = conf
config.WriteConfigToFile(previousConfig)
// Update fields in the demo profile only
demoProfile.URL = "http://demo.parseable.com"
demoProfile.Username = "admin"
demoProfile.Password = "admin"
previousConfig.Profiles["demo"] = demoProfile
} else {
// Add the "demo" profile if it doesn't exist
previousConfig.Profiles["demo"] = defaultInitialProfile()
previousConfig.DefaultProfile = "demo" // Optional: set as default if needed
}

// Write the updated configuration back to file
err = config.WriteConfigToFile(previousConfig)
if err != nil {
fmt.Printf("failed to write to existing file %v\n", err)
os.Exit(1)
}
}

Expand Down
14 changes: 10 additions & 4 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,18 @@ func WriteConfigToFile(config *Config) error {
func ReadConfigFromFile() (config *Config, err error) {
filePath, err := Path()
if err != nil {
return
return &Config{}, err
}

data, err := os.ReadFile(filePath)
if err != nil {
return
return &Config{}, err
}
toml.Unmarshal(data, &config)
return

err = toml.Unmarshal(data, &config)
if err != nil {
return &Config{}, err
}

return config, nil
}
Loading