Fix broken defaults and broken flags (#314)

* start with a default config, not an empty config.

* some data structures were present on Empty config but not Default config

* the monkey patched CLIContext is working

* remove print debugging log

* make the behaviour of the flags consistent across all data types

Conflicts:
	internal/config/config.go

* try to fix accidentally broken test
This commit is contained in:
Forest Johnson 2021-11-24 16:01:38 +00:00 committed by GitHub
commit ab316d2250
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 104 additions and 87 deletions

View file

@ -25,7 +25,7 @@ import (
"github.com/urfave/cli/v2"
)
func adminCommands() []*cli.Command {
func adminCommands(allFlags []cli.Flag) []*cli.Command {
return []*cli.Command{
{
Name: "admin",
@ -56,7 +56,7 @@ func adminCommands() []*cli.Command {
},
},
Action: func(c *cli.Context) error {
return runAction(c, account.Create)
return runAction(c, allFlags, account.Create)
},
},
{
@ -70,7 +70,7 @@ func adminCommands() []*cli.Command {
},
},
Action: func(c *cli.Context) error {
return runAction(c, account.Confirm)
return runAction(c, allFlags, account.Confirm)
},
},
{
@ -84,7 +84,7 @@ func adminCommands() []*cli.Command {
},
},
Action: func(c *cli.Context) error {
return runAction(c, account.Promote)
return runAction(c, allFlags, account.Promote)
},
},
{
@ -98,7 +98,7 @@ func adminCommands() []*cli.Command {
},
},
Action: func(c *cli.Context) error {
return runAction(c, account.Demote)
return runAction(c, allFlags, account.Demote)
},
},
{
@ -112,7 +112,7 @@ func adminCommands() []*cli.Command {
},
},
Action: func(c *cli.Context) error {
return runAction(c, account.Disable)
return runAction(c, allFlags, account.Disable)
},
},
{
@ -126,7 +126,7 @@ func adminCommands() []*cli.Command {
},
},
Action: func(c *cli.Context) error {
return runAction(c, account.Suspend)
return runAction(c, allFlags, account.Suspend)
},
},
{
@ -145,7 +145,7 @@ func adminCommands() []*cli.Command {
},
},
Action: func(c *cli.Context) error {
return runAction(c, account.Password)
return runAction(c, allFlags, account.Password)
},
},
},
@ -161,7 +161,7 @@ func adminCommands() []*cli.Command {
},
},
Action: func(c *cli.Context) error {
return runAction(c, trans.Export)
return runAction(c, allFlags, trans.Export)
},
},
{
@ -175,7 +175,7 @@ func adminCommands() []*cli.Command {
},
},
Action: func(c *cli.Context) error {
return runAction(c, trans.Import)
return runAction(c, allFlags, trans.Import)
},
},
},

View file

@ -22,12 +22,12 @@ import (
"github.com/urfave/cli/v2"
)
func getCommands() []*cli.Command {
func getCommands(allFlags []cli.Flag) []*cli.Command {
commands := []*cli.Command{}
commandSets := [][]*cli.Command{
serverCommands(),
adminCommands(),
testrigCommands(),
serverCommands(allFlags),
adminCommands(allFlags),
testrigCommands(allFlags),
}
for _, cs := range commandSets {
commands = append(commands, cs...)

View file

@ -42,11 +42,12 @@ func main() {
v = Version + " " + Commit[:7]
}
flagsSlice := getFlags()
app := &cli.App{
Version: v,
Usage: "a fediverse social media server",
Flags: getFlags(),
Commands: getCommands(),
Flags: flagsSlice,
Commands: getCommands(flagsSlice),
}
if err := app.Run(os.Args); err != nil {

View file

@ -27,17 +27,48 @@ import (
"github.com/urfave/cli/v2"
)
type MonkeyPatchedCLIContext struct {
CLIContext *cli.Context
AllFlags []cli.Flag
}
func (f MonkeyPatchedCLIContext) Bool(k string) bool { return f.CLIContext.Bool(k) }
func (f MonkeyPatchedCLIContext) String(k string) string { return f.CLIContext.String(k) }
func (f MonkeyPatchedCLIContext) StringSlice(k string) []string { return f.CLIContext.StringSlice(k) }
func (f MonkeyPatchedCLIContext) Int(k string) int { return f.CLIContext.Int(k) }
func (f MonkeyPatchedCLIContext) IsSet(k string) bool {
for _, flag := range f.AllFlags {
flagNames := flag.Names()
for _, name := range flagNames {
if name == k {
return flag.IsSet()
}
}
}
return false
}
// runAction builds up the config and logger necessary for any
// gotosocial action, and then executes the action.
func runAction(c *cli.Context, a cliactions.GTSAction) error {
func runAction(c *cli.Context, allFlags []cli.Flag, a cliactions.GTSAction) error {
// create a new *config.Config based on the config path provided...
conf, err := config.FromFile(c.String(config.GetFlagNames().ConfigPath))
if err != nil {
return fmt.Errorf("error creating config: %s", err)
}
// ... and the flags set on the *cli.Context by urfave
if err := conf.ParseCLIFlags(c, c.App.Version); err != nil {
//
// The IsSet function on the cli.Context object `c` here appears to have some issues right now, it always returns false in my tests.
// However we can re-create the behaviour we want by simply referencing the flag objects we created previously
// https://picopublish.sequentialread.com/files/chatlog_2021_11_18.txt
monkeyPatchedCLIContext := MonkeyPatchedCLIContext{
CLIContext: c,
AllFlags: allFlags,
}
if err := conf.ParseCLIFlags(monkeyPatchedCLIContext, c.App.Version); err != nil {
return fmt.Errorf("error parsing config: %s", err)
}

View file

@ -23,7 +23,7 @@ import (
"github.com/urfave/cli/v2"
)
func serverCommands() []*cli.Command {
func serverCommands(allFlags []cli.Flag) []*cli.Command {
return []*cli.Command{
{
Name: "server",
@ -33,7 +33,7 @@ func serverCommands() []*cli.Command {
Name: "start",
Usage: "start the gotosocial server",
Action: func(c *cli.Context) error {
return runAction(c, server.Start)
return runAction(c, allFlags, server.Start)
},
},
},

View file

@ -23,7 +23,7 @@ import (
"github.com/urfave/cli/v2"
)
func testrigCommands() []*cli.Command {
func testrigCommands(allFlags []cli.Flag) []*cli.Command {
return []*cli.Command{
{
Name: "testrig",
@ -33,7 +33,7 @@ func testrigCommands() []*cli.Command {
Name: "start",
Usage: "start the gotosocial testrig",
Action: func(c *cli.Context) error {
return runAction(c, testrig.Start)
return runAction(c, allFlags, testrig.Start)
},
},
},