Skip to content

Commit

Permalink
Merge branch 'feat/1.2.1/dashboard' into test
Browse files Browse the repository at this point in the history
  • Loading branch information
LinkinStars committed Nov 29, 2023
2 parents 5bc5f3f + 107316d commit 8ddaba8
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 3 deletions.
38 changes: 37 additions & 1 deletion cmd/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)
}
}
Expand Down Expand Up @@ -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.
Expand Down
84 changes: 84 additions & 0 deletions internal/cli/config.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion internal/entity/user_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type User struct {
Status int `xorm:"not null default 1 INT(11) status"`
AuthorityGroup int `xorm:"not null default 1 INT(11) authority_group"`
DisplayName string `xorm:"not null default '' VARCHAR(30) display_name"`
Avatar string `xorm:"not null default '' VARCHAR(255) avatar"`
Avatar string `xorm:"not null default '' VARCHAR(1024) avatar"`
Mobile string `xorm:"not null VARCHAR(20) mobile"`
Bio string `xorm:"not null TEXT bio"`
BioHTML string `xorm:"not null TEXT bio_html"`
Expand Down
6 changes: 5 additions & 1 deletion internal/migrations/v18.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,9 @@ func addPasswordLoginControl(ctx context.Context, x *xorm.Engine) error {
return fmt.Errorf("update site info failed: %w", err)
}
}
return nil

type User struct {
Avatar string `xorm:"not null default '' VARCHAR(1024) avatar"`
}
return x.Context(ctx).Sync(new(User))
}

0 comments on commit 8ddaba8

Please sign in to comment.