From ec2577ce5967dbe5be221be872106ed803f91cda Mon Sep 17 00:00:00 2001 From: Singee Date: Thu, 4 Jan 2024 14:16:22 +0800 Subject: [PATCH] tools: do not write tools key accidently --- internal/config/config.go | 12 +++++++----- internal/tools/install.go | 9 +++++++-- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index ab3eada..9834e3f 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -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 @@ -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 diff --git a/internal/tools/install.go b/internal/tools/install.go index f8eb487..23c7cf8 100644 --- a/internal/tools/install.go +++ b/internal/tools/install.go @@ -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 }) }