-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cmd): add settings server command
- Loading branch information
1 parent
f38848d
commit 0bfce9c
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |