Skip to content

Commit

Permalink
Fix broken defaults and broken flags (#314)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ForestJohnson authored Nov 24, 2021
1 parent 3caae37 commit ab316d2
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 87 deletions.
20 changes: 10 additions & 10 deletions cmd/gotosocial/admincommands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)
},
},
{
Expand All @@ -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)
},
},
{
Expand All @@ -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)
},
},
{
Expand All @@ -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)
},
},
{
Expand All @@ -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)
},
},
{
Expand All @@ -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)
},
},
{
Expand All @@ -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)
},
},
},
Expand All @@ -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)
},
},
{
Expand All @@ -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)
},
},
},
Expand Down
8 changes: 4 additions & 4 deletions cmd/gotosocial/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
5 changes: 3 additions & 2 deletions cmd/gotosocial/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
35 changes: 33 additions & 2 deletions cmd/gotosocial/runaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/gotosocial/servercommands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
},
},
},
Expand Down
4 changes: 2 additions & 2 deletions cmd/gotosocial/testrigcommands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion internal/api/client/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type AuthTestSuite struct {

// SetupSuite sets some variables on the suite that we can use as consts (more or less) throughout
func (suite *AuthTestSuite) SetupSuite() {
c := config.Empty()
c := config.Default()
// we're running on localhost without https so set the protocol to http
c.Protocol = "http"
// just for testing
Expand Down
Loading

0 comments on commit ab316d2

Please sign in to comment.