snapshot of https://github.com/cortezaproject/corteza.git (2024.9.x, 2026-09-18; blobs > 131072B omitted)
Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
This commit is contained in:
228
server/system/commands/settings.go
Normal file
228
server/system/commands/settings.go
Normal file
@@ -0,0 +1,228 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/cortezaproject/corteza/server/pkg/auth"
|
||||
"github.com/cortezaproject/corteza/server/system/types"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/cortezaproject/corteza/server/pkg/cli"
|
||||
"github.com/cortezaproject/corteza/server/system/service"
|
||||
)
|
||||
|
||||
func Settings(ctx context.Context, app serviceInitializer) *cobra.Command {
|
||||
var (
|
||||
cmd = &cobra.Command{
|
||||
Use: "settings",
|
||||
Short: "Settings management",
|
||||
}
|
||||
|
||||
restartWarning = func(cmd *cobra.Command, args []string) {
|
||||
fmt.Print("\nYour server needs to be restarted in order to apply changes\n")
|
||||
}
|
||||
)
|
||||
|
||||
list := &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List all",
|
||||
PreRunE: commandPreRunInitService(app),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
ctx = auth.SetIdentityToContext(ctx, auth.ServiceUser())
|
||||
|
||||
prefix := cmd.Flags().Lookup("prefix").Value.String()
|
||||
if kv, err := service.DefaultSettings.FindByPrefix(ctx, prefix); err != nil {
|
||||
cli.HandleError(err)
|
||||
} else {
|
||||
var maxlen int
|
||||
for _, v := range kv {
|
||||
if l := len(v.Name); l > maxlen {
|
||||
maxlen = l
|
||||
}
|
||||
}
|
||||
|
||||
for _, v := range kv {
|
||||
cmd.Printf("%s%s\t%v\n", v.Name, strings.Repeat(" ", maxlen-len(v.Name)), v.Value)
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
list.Flags().String("prefix", "", "SettingsFilter settings by prefix")
|
||||
|
||||
get := &cobra.Command{
|
||||
Use: "get [key to get, ...]",
|
||||
|
||||
Short: "Get value (raw JSON) for a specific key",
|
||||
Args: cobra.ExactArgs(1),
|
||||
PreRunE: commandPreRunInitService(app),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
ctx = auth.SetIdentityToContext(ctx, auth.ServiceUser())
|
||||
|
||||
if v, err := service.DefaultSettings.Get(ctx, args[0], 0); err != nil {
|
||||
cli.HandleError(err)
|
||||
} else if v != nil {
|
||||
cmd.Printf("%v\n", v.Value)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
set := &cobra.Command{
|
||||
Use: "set [key to set] [value]",
|
||||
Short: "Set value (raw JSON or string) for a specific key",
|
||||
Args: cobra.ExactArgs(2),
|
||||
PreRunE: commandPreRunInitService(app),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
ctx = auth.SetIdentityToContext(ctx, auth.ServiceUser())
|
||||
|
||||
value := args[1]
|
||||
|
||||
v := &types.SettingValue{
|
||||
Name: args[0],
|
||||
}
|
||||
|
||||
if cmd.Flags().Lookup("as-string").Changed {
|
||||
cli.HandleError(v.SetSetting(value))
|
||||
} else {
|
||||
err := v.SetRawSetting(value)
|
||||
if _, is := err.(*json.SyntaxError); is {
|
||||
// Quote the raw value and re-parse
|
||||
err = v.SetRawSetting(`"` + value + `"`)
|
||||
}
|
||||
|
||||
cli.HandleError(err)
|
||||
}
|
||||
|
||||
cli.HandleError(service.DefaultSettings.Set(ctx, v))
|
||||
},
|
||||
PostRun: restartWarning,
|
||||
}
|
||||
|
||||
set.Flags().BoolP("as-string", "s", false, "Treat input value as string (to avoid wrapping in quotes)")
|
||||
|
||||
imp := &cobra.Command{
|
||||
Use: "import [file]",
|
||||
Short: "Import settings as JSON from stdin or file",
|
||||
Args: cobra.MaximumNArgs(1),
|
||||
PreRunE: commandPreRunInitService(app),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
ctx = auth.SetIdentityToContext(ctx, auth.ServiceUser())
|
||||
|
||||
var (
|
||||
fh *os.File
|
||||
err error
|
||||
)
|
||||
|
||||
if len(args) > 0 {
|
||||
fh, err = os.Open(args[0])
|
||||
cli.HandleError(err)
|
||||
} else {
|
||||
fh = os.Stdin
|
||||
}
|
||||
|
||||
var (
|
||||
decoder = json.NewDecoder(fh)
|
||||
input = map[string]interface{}{}
|
||||
vv types.SettingValueSet
|
||||
)
|
||||
|
||||
cli.HandleError(decoder.Decode(&input))
|
||||
|
||||
for k, v := range input {
|
||||
val := &types.SettingValue{Name: k}
|
||||
|
||||
cli.HandleError(val.SetSetting(v))
|
||||
vv = append(vv, val)
|
||||
}
|
||||
|
||||
if len(vv) > 0 {
|
||||
cli.HandleError(service.DefaultSettings.BulkSet(ctx, vv))
|
||||
}
|
||||
},
|
||||
PostRun: restartWarning,
|
||||
}
|
||||
|
||||
exp := &cobra.Command{
|
||||
Use: "export [file]",
|
||||
Short: "Export settings as JSON to stdout or file",
|
||||
Args: cobra.MaximumNArgs(1),
|
||||
PreRunE: commandPreRunInitService(app),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
ctx = auth.SetIdentityToContext(ctx, auth.ServiceUser())
|
||||
|
||||
var (
|
||||
fh *os.File
|
||||
err error
|
||||
)
|
||||
|
||||
if len(args) > 0 {
|
||||
fh, err = os.Create(args[0])
|
||||
cli.HandleError(err)
|
||||
} else {
|
||||
fh = os.Stdin
|
||||
}
|
||||
|
||||
var (
|
||||
encoder = json.NewEncoder(fh)
|
||||
)
|
||||
|
||||
encoder.SetIndent("", " ")
|
||||
|
||||
if vv, err := service.DefaultSettings.FindByPrefix(ctx); err != nil {
|
||||
cli.HandleError(err)
|
||||
} else {
|
||||
cli.HandleError(encoder.Encode(vv.KV()))
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
del := &cobra.Command{
|
||||
Use: "delete [keys, ...]",
|
||||
Short: "Set value (raw JSON) for a specific key (or by prefix)",
|
||||
Args: cobra.MinimumNArgs(0),
|
||||
PreRunE: commandPreRunInitService(app),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
ctx = auth.SetIdentityToContext(ctx, auth.ServiceUser())
|
||||
|
||||
var (
|
||||
names = []string{}
|
||||
)
|
||||
|
||||
if prefix := cmd.Flags().Lookup("prefix").Value.String(); len(prefix) > 0 {
|
||||
if vv, err := service.DefaultSettings.FindByPrefix(ctx); err != nil {
|
||||
cli.HandleError(err)
|
||||
} else {
|
||||
_ = vv.Walk(func(v *types.SettingValue) error {
|
||||
names = append(names, v.Name)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
} else if len(args) > 0 {
|
||||
names = args
|
||||
}
|
||||
|
||||
for a := 0; a < len(names); a++ {
|
||||
cli.HandleError(service.DefaultSettings.Delete(ctx, names[a], 0))
|
||||
}
|
||||
},
|
||||
PostRun: restartWarning,
|
||||
}
|
||||
|
||||
del.Flags().String("prefix", "", "SettingsFilter settings by prefix")
|
||||
|
||||
cmd.AddCommand(
|
||||
list,
|
||||
get,
|
||||
set,
|
||||
del,
|
||||
imp,
|
||||
exp,
|
||||
)
|
||||
|
||||
return cmd
|
||||
}
|
||||
Reference in New Issue
Block a user