From 23b4a44ce33bd6b555ad2f063894cc9bfb035a4d Mon Sep 17 00:00:00 2001 From: LinkinStar Date: Fri, 4 Nov 2022 11:30:53 +0800 Subject: [PATCH] feat: rewrite config file when install config --- internal/base/conf/conf.go | 11 +++++++++ internal/cli/install.go | 25 +++------------------ internal/install/install_controller.go | 14 ++++++++---- pkg/writer/writer.go | 31 ++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 26 deletions(-) create mode 100644 pkg/writer/writer.go diff --git a/internal/base/conf/conf.go b/internal/base/conf/conf.go index 55846825..37b091c0 100644 --- a/internal/base/conf/conf.go +++ b/internal/base/conf/conf.go @@ -9,7 +9,9 @@ import ( "github.com/answerdev/answer/internal/cli" "github.com/answerdev/answer/internal/router" "github.com/answerdev/answer/internal/service/service_config" + "github.com/answerdev/answer/pkg/writer" "github.com/segmentfault/pacman/contrib/conf/viper" + "sigs.k8s.io/yaml" ) // AllConfig all config @@ -48,3 +50,12 @@ func ReadConfig(configFilePath string) (c *AllConfig, err error) { } return c, nil } + +// RewriteConfig rewrite config file path +func RewriteConfig(configFilePath string, allConfig *AllConfig) error { + content, err := yaml.Marshal(allConfig) + if err != nil { + return err + } + return writer.ReplaceFile(configFilePath, string(content)) +} diff --git a/internal/cli/install.go b/internal/cli/install.go index eee7b89a..b31abbf2 100644 --- a/internal/cli/install.go +++ b/internal/cli/install.go @@ -1,14 +1,13 @@ package cli import ( - "bufio" "fmt" - "os" "path/filepath" "github.com/answerdev/answer/configs" "github.com/answerdev/answer/i18n" "github.com/answerdev/answer/pkg/dir" + "github.com/answerdev/answer/pkg/writer" ) const ( @@ -50,7 +49,7 @@ func InstallConfigFile(configFilePath string) error { } fmt.Printf("[config-file] create directory success, config file is %s\n", configFilePath) - if err := writerFile(configFilePath, string(configs.Config)); err != nil { + if err := writer.WriteFile(configFilePath, string(configs.Config)); err != nil { fmt.Printf("[config-file] install fail %s\n", err.Error()) return fmt.Errorf("write file failed %s", err) } @@ -87,7 +86,7 @@ func installI18nBundle() { continue } fmt.Printf("[i18n] install %s bundle...\n", item.Name()) - err = writerFile(path, string(content)) + err = writer.WriteFile(path, string(content)) if err != nil { fmt.Printf("[i18n] install %s bundle fail: %s\n", item.Name(), err.Error()) } else { @@ -95,21 +94,3 @@ func installI18nBundle() { } } } - -func writerFile(filePath, content string) error { - file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0o666) - if err != nil { - return err - } - defer func() { - _ = file.Close() - }() - writer := bufio.NewWriter(file) - if _, err := writer.WriteString(content); err != nil { - return err - } - if err := writer.Flush(); err != nil { - return err - } - return nil -} diff --git a/internal/install/install_controller.go b/internal/install/install_controller.go index 42a67671..1b98db82 100644 --- a/internal/install/install_controller.go +++ b/internal/install/install_controller.go @@ -98,8 +98,7 @@ func InitEnvironment(ctx *gin.Context) { return } - err := cli.InstallConfigFile(confPath) - if err != nil { + if err := cli.InstallConfigFile(confPath); err != nil { handler.HandleResponse(ctx, errors.BadRequest(reason.InstallConfigFailed), &InitEnvironmentResp{ Success: false, CreateConfigFailed: true, @@ -112,8 +111,15 @@ func InitEnvironment(ctx *gin.Context) { c, err := conf.ReadConfig(confPath) if err != nil { log.Errorf("read config failed %s", err) - err = errors.BadRequest(reason.ReadConfigFailed) - handler.HandleResponse(ctx, err, nil) + handler.HandleResponse(ctx, errors.BadRequest(reason.ReadConfigFailed), nil) + return + } + c.Data.Database.Driver = req.DbType + c.Data.Database.Connection = req.GetConnection() + + if err := conf.RewriteConfig(confPath, c); err != nil { + log.Errorf("rewrite config failed %s", err) + handler.HandleResponse(ctx, errors.BadRequest(reason.ReadConfigFailed), nil) return } diff --git a/pkg/writer/writer.go b/pkg/writer/writer.go new file mode 100644 index 00000000..d2b6c440 --- /dev/null +++ b/pkg/writer/writer.go @@ -0,0 +1,31 @@ +package writer + +import ( + "bufio" + "os" +) + +// ReplaceFile remove old file and write new file +func ReplaceFile(filePath, content string) error { + _ = os.Remove(filePath) + return WriteFile(filePath, content) +} + +// WriteFile write file to path +func WriteFile(filePath, content string) error { + file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0o666) + if err != nil { + return err + } + defer func() { + _ = file.Close() + }() + writer := bufio.NewWriter(file) + if _, err := writer.WriteString(content); err != nil { + return err + } + if err := writer.Flush(); err != nil { + return err + } + return nil +}