Skip to content

Commit

Permalink
tools: do not write tools key accidently
Browse files Browse the repository at this point in the history
  • Loading branch information
ImSingee committed Jan 4, 2024
1 parent c7aef71 commit ec2577c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
12 changes: 7 additions & 5 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func getKittyConfig(dir string) (string, map[string]gson.JSON, error) {
return "", nil, nil
}

func PatchKittyConfig(dir string, patch func(map[string]gson.JSON) error) error {
func PatchKittyConfig(dir string, patch func(map[string]gson.JSON) (save bool, err error)) error {
filename, c, err := getKittyConfig(dir)
if err != nil {
return err
Expand All @@ -84,14 +84,16 @@ func PatchKittyConfig(dir string, patch func(map[string]gson.JSON) error) error
c = make(map[string]gson.JSON)
}

err = patch(c)
save, err := patch(c)
if err != nil {
return err
}

err = saveKittyConfig(filename, c)
if err != nil {
return err
if save {
err = saveKittyConfig(filename, c)
if err != nil {
return err
}
}

return nil
Expand Down
9 changes: 7 additions & 2 deletions internal/tools/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,14 @@ func (o *installOptions) install() error {
}

func (o *installOptions) writeToolsInfo(tools map[string]string) error {
return config.PatchKittyConfig("", func(c map[string]gson.JSON) error {
return config.PatchKittyConfig("", func(c map[string]gson.JSON) (save bool, err error) {
// if c["tools"] not exist and tools is empty, do nothing
if _, toolsKeyExist := c["tools"]; !toolsKeyExist && len(tools) == 0 {
return false, nil
}

c["tools"] = gson.New(tools)
return nil
return true, nil
})
}

Expand Down

0 comments on commit ec2577c

Please sign in to comment.