Skip to content

cmd/syncthing: Don't fail early on api setup error (fixes 7558) - #7591

Merged
AudriusButkevicius merged 6 commits into
syncthing:mainfrom
imsodin:cmdsyncthing/cli/api-error
Apr 25, 2021
Merged

AudriusButkevicius merged 6 commits into
syncthing:mainfrom
imsodin:cmdsyncthing/cli/api-error

Conversation

@imsodin

@imsodin imsodin commented Apr 22, 2021

Copy link
Copy Markdown
Member

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 errorAPIClient type that returns the error from setup when on all of it's methods.

@AudriusButkevicius

AudriusButkevicius commented Apr 22, 2021 •

Copy link
Copy Markdown
Member

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 cli config devices --help still works etc, just returns some single garbage folder and single garbage device which works for help purposes, and then when it gets to handling the result of the command, just aborts because it knows it used a placeholder config.

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.

@imsodin

imsodin commented Apr 24, 2021

Copy link
Copy Markdown
Member Author

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 --help. I tried to check for the help flag in afterConfig, but cli.Context.IsSet("help") always returns false and I didn't manage to get that information in any way.

Comment thread cmd/syncthing/cli/utils.go Outdated
func saveToFile(url string) cli.ActionFunc {
return func(c *cli.Context) error {
client := c.App.Metadata["client"].(*APIClient)
client := c.App.Metadata["client"].(APIClient)

@AudriusButkevicius AudriusButkevicius Apr 24, 2021 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is now never set, hence should fail?
We only set clientFactory oppose to client

@AudriusButkevicius

AudriusButkevicius commented Apr 24, 2021 •

Copy link
Copy Markdown
Member

I think help might be a global flag, so you might need to check GlobalIsSet, or manually crawl up for ctx = ctx; ctx.Parent() != nil; ctx = ctx.Parent() {} etc.

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
}

@imsodin

imsodin commented Apr 24, 2021

Copy link
Copy Markdown
Member Author

I tried all of that (also with cli.HelpFlag.Name()) - I don't have any interest in debugging why it doesn't work. I had a quick look at the code and it's convoluted - printing the error together with the help doesn't seem that bad to me.

}
if reflect.DeepEqual(h.cfg, h.original) {
return nil
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@AudriusButkevicius
AudriusButkevicius merged commit ef4b8a2 into syncthing:main Apr 25, 2021
@imsodin
imsodin deleted the cmdsyncthing/cli/api-error branch April 25, 2021 19:57
calmh pushed a commit to calmh/syncthing that referenced this pull request Apr 26, 2021
…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
@calmh calmh added this to the v1.16.0 milestone Apr 26, 2021
calmh added a commit that referenced this pull request Apr 27, 2021
* release:
  lib/model: Handle invalid needed items on send-only (ref #7476) (#7596)
  gui: Handle empty path in validation (ref #7379) (#7595)
  cmd/syncthing: Don't fail early on api setup error (fixes 7558) (#7591)
greatroar pushed a commit to greatroar/syncthing that referenced this pull request May 8, 2021
…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
@calmh calmh added the frozen-due-to-age Issues closed and untouched for a long time, together with being locked for discussion label Aug 11, 2022
@syncthing syncthing locked and limited conversation to collaborators Aug 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

frozen-due-to-age Issues closed and untouched for a long time, together with being locked for discussion

Projects

None yet

Development

Successfully merging this pull request may close these issues.

syncthing cli --help doesn't work without a valid home path with cert.pem

3 participants