Skip to content

Commit

Permalink
Merge branch 'feat/1.4.0/config' into test
Browse files Browse the repository at this point in the history
  • Loading branch information
LinkinStars committed Aug 23, 2024
2 parents 82d7445 + b20e656 commit 2cc4783
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .asf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ github:
- q-and-a
- hacktoberfest
features:
wiki: false
wiki: true
issues: true
projects: true
discussions: false
Expand Down
7 changes: 7 additions & 0 deletions cmd/answer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,19 @@
* under the License.
*/

//go:generate go run github.com/swaggo/swag/cmd/swag init -g ./cmd/answer/main.go -d ../../ -o ../../docs

package main

import (
answercmd "github.com/apache/incubator-answer/cmd"
)

// main godoc
// @title "apache answer"
// @description = "apache answer api"
// @version = "v0.0.1"
// @BasePath = "/"
// @securityDefinitions.apikey ApiKeyAuth
// @in header
// @name Authorization
Expand Down
11 changes: 8 additions & 3 deletions cmd/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,17 @@ To run answer, use:
}

field := &cli.ConfigField{}
for _, f := range configFields {
switch f {
fmt.Println(configFields)
if len(configFields) > 0 {
switch configFields[0] {
case "allow_password_login":
field.AllowPasswordLogin = true
case "deactivate_plugin":
if len(configFields) > 1 {
field.DeactivatePluginSlugName = configFields[1]
}
default:
fmt.Printf("field %s not support\n", f)
fmt.Printf("field %s not support\n", configFields[0])
}
}
err = cli.SetDefaultConfig(c.Data.Database, c.Data.Cache, field)
Expand Down
8 changes: 4 additions & 4 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11419,12 +11419,12 @@ const docTemplate = `{

// SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo = &swag.Spec{
Version: "",
Version: "= \"v0.0.1\"",
Host: "",
BasePath: "",
BasePath: "= \"/\"",
Schemes: []string{},
Title: "",
Description: "",
Title: "\"answer\"",
Description: "= \"answer api\"",
InfoInstanceName: "swagger",
SwaggerTemplate: docTemplate,
LeftDelim: "{{",
Expand Down
6 changes: 5 additions & 1 deletion docs/swagger.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
{
"swagger": "2.0",
"info": {
"contact": {}
"description": "= \"answer api\"",
"title": "\"answer\"",
"contact": {},
"version": "= \"v0.0.1\""
},
"basePath": "= \"/\"",
"paths": {
"/": {
"get": {
Expand Down
4 changes: 4 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# specific language governing permissions and limitations
# under the License.

basePath: = "/"
definitions:
constant.NotificationChannelKey:
enum:
Expand Down Expand Up @@ -2899,6 +2900,9 @@ definitions:
type: object
info:
contact: {}
description: = "answer api"
title: '"answer"'
version: = "v0.0.1"
paths:
/:
get:
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ require (
github.com/stretchr/testify v1.8.4
github.com/swaggo/files v1.0.0
github.com/swaggo/gin-swagger v1.5.3
github.com/swaggo/swag v1.16.1
github.com/swaggo/swag v1.16.3
github.com/tidwall/gjson v1.14.4
github.com/yuin/goldmark v1.4.13
golang.org/x/crypto v0.21.0
Expand All @@ -79,6 +79,7 @@ require (
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/containerd/continuity v0.4.2 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/cli v24.0.6+incompatible // indirect
github.com/docker/docker v24.0.6+incompatible // indirect
Expand Down Expand Up @@ -128,6 +129,7 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/afero v1.9.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
Expand All @@ -140,6 +142,7 @@ require (
github.com/tidwall/pretty v1.2.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/urfave/cli/v2 v2.3.0 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7
github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/coreos/pkg v0.0.0-20160727233714-3ac0863d7acf/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
Expand Down Expand Up @@ -615,6 +616,7 @@ github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU=
github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E=
Expand Down Expand Up @@ -692,6 +694,8 @@ github.com/swaggo/gin-swagger v1.5.3/go.mod h1:3XJKSfHjDMB5dBo/0rrTXidPmgLeqsX89
github.com/swaggo/swag v1.8.1/go.mod h1:ugemnJsPZm/kRwFUnzBlbHRd0JY9zE1M4F+uy2pAaPQ=
github.com/swaggo/swag v1.16.1 h1:fTNRhKstPKxcnoKsytm4sahr8FaYzUcT7i1/3nd/fBg=
github.com/swaggo/swag v1.16.1/go.mod h1:9/LMvHycG3NFHfR6LwvikHv5iFvmPADQ359cKikGxto=
github.com/swaggo/swag v1.16.3 h1:PnCYjPCah8FK4I26l2F/KQ4yz3sILcVUN3cTlBFA9Pg=
github.com/swaggo/swag v1.16.3/go.mod h1:DImHIuOFXKpMFAQjcC7FG4m3Dg4+QuUgUzJmKjI/gRk=
github.com/syndtr/goleveldb v1.0.0 h1:fBdIW9lB4Iz0n9khmH8w27SJ3QEJ7+IgjPEwGSZiFdE=
github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ=
github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM=
Expand All @@ -710,7 +714,9 @@ github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95
github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU=
github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/urfave/cli v1.22.1 h1:+mkCCcOFKPnCmVYVcURKps1Xe+3zP90gSYGNfRkjoIY=
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb h1:zGWFAtiMcyryUHoUjUJX0/lt1H2+i2Ka2n+D3DImSNo=
Expand Down
39 changes: 39 additions & 0 deletions internal/cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (

type ConfigField struct {
AllowPasswordLogin bool `json:"allow_password_login"`
// The slug name of plugin that you want to deactivate
DeactivatePluginSlugName string `json:"deactivate_plugin_slug_name"`
}

// SetDefaultConfig set default config
Expand All @@ -55,6 +57,9 @@ func SetDefaultConfig(dbConf *data.Database, cacheConf *data.CacheConf, field *C
if field.AllowPasswordLogin {
return defaultLoginConfig(db)
}
if len(field.DeactivatePluginSlugName) > 0 {
return deactivatePlugin(db, field.DeactivatePluginSlugName)
}

return nil
}
Expand Down Expand Up @@ -82,3 +87,37 @@ func defaultLoginConfig(x *xorm.Engine) (err error) {
}
return nil
}

func deactivatePlugin(x *xorm.Engine, pluginSlugName string) (err error) {
fmt.Printf("try to deactivate plugin: %s\n", pluginSlugName)

item := &entity.Config{Key: constant.PluginStatus}
exist, err := x.Get(item)
if err != nil {
return fmt.Errorf("get config failed: %w", err)
}
if !exist {
return nil
}

pluginStatusMapping := make(map[string]bool)
_ = json.Unmarshal([]byte(item.Value), &pluginStatusMapping)
status, ok := pluginStatusMapping[pluginSlugName]
if !ok {
fmt.Printf("plugin %s not exist\n", pluginSlugName)
return nil
}
if !status {
fmt.Printf("plugin %s already deactivated\n", pluginSlugName)
return nil
}

pluginStatusMapping[pluginSlugName] = false
dataByte, _ := json.Marshal(pluginStatusMapping)
item.Value = string(dataByte)
_, err = x.ID(item.ID).Cols("value").Update(item)
if err != nil {
return fmt.Errorf("update plugin status failed: %w", err)
}
return nil
}
4 changes: 0 additions & 4 deletions internal/router/swagger_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,5 @@ func (a *SwaggerRouter) Register(r *gin.RouterGroup) {

// InitSwaggerDocs init swagger docs
func (a *SwaggerRouter) InitSwaggerDocs() {
docs.SwaggerInfo.Title = "answer"
docs.SwaggerInfo.Description = "answer api"
docs.SwaggerInfo.Version = "v0.0.1"
docs.SwaggerInfo.Host = fmt.Sprintf("%s%s", a.config.Host, a.config.Address)
docs.SwaggerInfo.BasePath = "/"
}

0 comments on commit 2cc4783

Please sign in to comment.