This is an automated email from the ASF dual-hosted git repository.

linkinstar pushed a commit to branch feat/1.2.1/dashboard
in repository https://gitbox.apache.org/repos/asf/incubator-answer.git


The following commit(s) were added to refs/heads/feat/1.2.1/dashboard by this 
push:
     new def4d8cb feat(command): add config command to set default config
def4d8cb is described below

commit def4d8cbb7dd4903a77f567bf91253a48cb4a004
Author: LinkinStars <[email protected]>
AuthorDate: Thu Nov 16 15:55:03 2023 +0800

    feat(command): add config command to set default config
---
 cmd/command.go         | 38 ++++++++++++++++++++++-
 internal/cli/config.go | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 121 insertions(+), 1 deletion(-)

diff --git a/cmd/command.go b/cmd/command.go
index e9b35294..5b3bb700 100644
--- a/cmd/command.go
+++ b/cmd/command.go
@@ -45,6 +45,8 @@ var (
        // This config is used to upgrade the database from a specific version 
manually.
        // If you want to upgrade the database to version 1.1.0, you can use 
`answer upgrade -f v1.1.0`.
        upgradeVersion string
+       // The fields that need to be set to the default value
+       configFields []string
 )
 
 func init() {
@@ -60,7 +62,9 @@ func init() {
 
        upgradeCmd.Flags().StringVarP(&upgradeVersion, "from", "f", "", 
"upgrade from specific version, eg: -f v1.1.0")
 
-       for _, cmd := range []*cobra.Command{initCmd, checkCmd, runCmd, 
dumpCmd, upgradeCmd, buildCmd, pluginCmd} {
+       configCmd.Flags().StringSliceVarP(&configFields, "with", "w", 
[]string{}, "the fields that need to be set to the default value, eg: -w 
allow_password_login")
+
+       for _, cmd := range []*cobra.Command{initCmd, checkCmd, runCmd, 
dumpCmd, upgradeCmd, buildCmd, pluginCmd, configCmd} {
                rootCmd.AddCommand(cmd)
        }
 }
@@ -231,6 +235,38 @@ To run answer, use:
                        })
                },
        }
+
+       // configCmd set some config to default value
+       configCmd = &cobra.Command{
+               Use:   "config",
+               Short: "set some config to default value",
+               Long:  `set some config to default value`,
+               Run: func(_ *cobra.Command, _ []string) {
+                       cli.FormatAllPath(dataDirPath)
+
+                       c, err := conf.ReadConfig(cli.GetConfigFilePath())
+                       if err != nil {
+                               fmt.Println("read config failed: ", err.Error())
+                               return
+                       }
+
+                       field := &cli.ConfigField{}
+                       for _, f := range configFields {
+                               switch f {
+                               case "allow_password_login":
+                                       field.AllowPasswordLogin = true
+                               default:
+                                       fmt.Printf("field %s not support\n", f)
+                               }
+                       }
+                       err = cli.SetDefaultConfig(c.Data.Database, 
c.Data.Cache, field)
+                       if err != nil {
+                               fmt.Println("set default config failed: ", 
err.Error())
+                       } else {
+                               fmt.Println("set default config successfully")
+                       }
+               },
+       }
 )
 
 // Execute adds all child commands to the root command and sets flags 
appropriately.
diff --git a/internal/cli/config.go b/internal/cli/config.go
new file mode 100644
index 00000000..06764e41
--- /dev/null
+++ b/internal/cli/config.go
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package cli
+
+import (
+       "context"
+       "encoding/json"
+       "fmt"
+       "github.com/apache/incubator-answer/internal/base/constant"
+       "github.com/apache/incubator-answer/internal/base/data"
+       "github.com/apache/incubator-answer/internal/entity"
+       "xorm.io/xorm"
+)
+
+type ConfigField struct {
+       AllowPasswordLogin bool `json:"allow_password_login"`
+}
+
+// SetDefaultConfig set default config
+func SetDefaultConfig(dbConf *data.Database, cacheConf *data.CacheConf, field 
*ConfigField) error {
+       db, err := data.NewDB(false, dbConf)
+       if err != nil {
+               return err
+       }
+       defer db.Close()
+
+       cache, cacheCleanup, err := data.NewCache(cacheConf)
+       if err != nil {
+               fmt.Println("new cache failed")
+       }
+       defer func() {
+               if cache != nil {
+                       cache.Flush(context.Background())
+                       cacheCleanup()
+               }
+       }()
+
+       if field.AllowPasswordLogin {
+               return defaultLoginConfig(db)
+       }
+
+       return nil
+}
+
+func defaultLoginConfig(x *xorm.Engine) (err error) {
+       fmt.Println("set default login config")
+
+       loginSiteInfo := &entity.SiteInfo{
+               Type: constant.SiteTypeLogin,
+       }
+       exist, err := x.Get(loginSiteInfo)
+       if err != nil {
+               return fmt.Errorf("get config failed: %w", err)
+       }
+       if exist {
+               var content map[string]any
+               _ = json.Unmarshal([]byte(loginSiteInfo.Content), &content)
+               content["allow_password_login"] = true
+               dataByte, _ := json.Marshal(content)
+               loginSiteInfo.Content = string(dataByte)
+               _, err = 
x.ID(loginSiteInfo.ID).Cols("content").Update(loginSiteInfo)
+               if err != nil {
+                       return fmt.Errorf("update site info failed: %w", err)
+               }
+       }
+       return nil
+}

Reply via email to