Skip to content

Commit

Permalink
feat(cmd): add settings server command
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed May 2, 2023
1 parent f38848d commit 0bfce9c
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions server/cmd/setting.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package cmd

import (
"fmt"
"strconv"

"github.com/charmbracelet/soft-serve/server/backend"
"github.com/spf13/cobra"
)

func settingCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "setting",
Short: "Manage settings",
}

cmd.AddCommand(
&cobra.Command{
Use: "allow-keyless [true|false]",
Short: "Set or get allow keyless access to repositories",
Args: cobra.RangeArgs(0, 1),
PersistentPreRunE: checkIfAdmin,
RunE: func(cmd *cobra.Command, args []string) error {
cfg, _ := fromContext(cmd)
switch len(args) {
case 0:
cmd.Println(cfg.Backend.AllowKeyless())
case 1:
v, _ := strconv.ParseBool(args[0])
if err := cfg.Backend.SetAllowKeyless(v); err != nil {
return err
}
}

return nil
},
},
)

cmd.AddCommand(
&cobra.Command{
Use: "anon-access [ACCESS_LEVEL]",
Short: "Set or get the default access level for anonymous users",
Args: cobra.RangeArgs(0, 1),
ValidArgs: []string{backend.NoAccess.String(), backend.ReadOnlyAccess.String(), backend.ReadWriteAccess.String(), backend.AdminAccess.String()},
PersistentPreRunE: checkIfAdmin,
RunE: func(cmd *cobra.Command, args []string) error {
cfg, _ := fromContext(cmd)
switch len(args) {
case 0:
cmd.Println(cfg.Backend.AnonAccess())
case 1:
al := backend.ParseAccessLevel(args[0])
if al < 0 {
return fmt.Errorf("invalid access level: %s", args[0])
}
if err := cfg.Backend.SetAnonAccess(al); err != nil {
return err
}
}

return nil
},
},
)

return cmd
}

0 comments on commit 0bfce9c

Please sign in to comment.