Repository navigation
cmd/syncthing: Don't fail early on api setup error (fixes 7558) - #7591
Conversation
|
This is actually better than anything else I came up with, but some random ideas some of which might stick: I wonder if we could go a step further and generate a dummy config if looking up a proper one fails, to still wire the subcommands, so things like As a side venture, you could also turn getClient into getClientFactory which the commands themselves call (which then constructs the client). I guess this doesn't help in any way as we retrieve the config for the purpose of generating commands of devices available to start with, so this will be called regardless even in case of --help, unless combined with the placeholder config proposal above. So if you do help on commands other than the magic generated ones, you could avoid having to construct the client all together for the purposes of displaying --help. Also, if we fail to call the API to get the config, we could try loading it from disk, so the CLI would still work, but instead of fiddling the api, it would fiddle the file on disk. |
|
Nice ideas. I did all of that except for managing the config on disk when the api is down. Definitely doable (and useful), but I don't want to do it in this PR (just because I don't want to do it right now :) ). It still shows the error when passing |
| func saveToFile(url string) cli.ActionFunc { | ||
| return func(c *cli.Context) error { | ||
| client := c.App.Metadata["client"].(*APIClient) | ||
| client := c.App.Metadata["client"].(APIClient) |
There was a problem hiding this comment.
I think this is now never set, hence should fail?
We only set clientFactory oppose to client
|
I think help might be a global flag, so you might need to check Also found the following in the code: func checkCommandHelp(c *Context, name string) bool {
if c.Bool("h") || c.Bool("help") {
_ = ShowCommandHelp(c, name)
return true
}
return false
}
func checkSubcommandHelp(c *Context) bool {
if c.Bool("h") || c.Bool("help") {
_ = ShowSubcommandHelp(c)
return true
}
return false
} |
|
I tried all of that (also with |
| } | ||
| if reflect.DeepEqual(h.cfg, h.original) { | ||
| return nil | ||
| } |
There was a problem hiding this comment.
If you moved this above the h.err check, --help should not modify the config, in which case we'd end up not returning the error.
There was a problem hiding this comment.
You need a util.FillNil on the default config it seems, but other than that, seems to work for me.
You can check for "--help" in ctx.Args() apparently, but I feel the DeepEquals early termination is probably less hacky.
There was a problem hiding this comment.
That's not ok because it means we don't return the error on an actual (read) operation, but just the default value and success.
There was a problem hiding this comment.
In that case you don't even want to run the command at all.
How about adding a Before handler, if it doesn't have --help or -h in args, and error is set, just abort the command all together.
And then remove the error check in the After command.
There was a problem hiding this comment.
Just came to the same conclusion after posting my last comment. However I kept the error check, because After is called regardless of whether Before returned a non-nil error or not.
…thing#7591) * cmd/syncthing: Don't fail early on api setup error (fixes 7558) * switch to factory pattern * refactor config command to show help on nothing * wip * wip * already abort in before
…thing#7591) * cmd/syncthing: Don't fail early on api setup error (fixes 7558) * switch to factory pattern * refactor config command to show help on nothing * wip * wip * already abort in before
Instead of immediately exiting when encountering an error setting up the API client, the error is stored and returned if the api client is used. That ensures that the error does not come up if no operation involving the API is happening (like just querying
--help).To do so I added an interface for the api client and an
errorAPIClienttype that returns the error from setup when on all of it's methods.