Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to override app configurations at the command line #1542

Merged
merged 9 commits into from
Nov 17, 2023

Conversation

freakboy3742
Copy link
Member

Adds a -C / --config option that allows a user to override the value of a setting at the command line.

e.g.:

briefcase create -C template=\"https:/example.com/template\"

would generate an app using the values in the pyproject.toml, except for using a different template.

This is primarily useful for internal testing setups, such as generating a test app inside a template repository, or running CI for the Apple support package in CI. However, it could also be useful to pass in other values, such as a version number.

It's likely only of any practical use in the create command, but it's been implemented in all "app facing" commands in case it might be useful.

Fixes #1115.

/cc @rmartin16

PR Checklist:

  • All new features have been tested
  • All new features have been documented
  • I have read the CONTRIBUTING.md file
  • I will abide by the code of conduct

@freakboy3742 freakboy3742 requested a review from mhsmith November 16, 2023 07:23
Copy link
Member

@rmartin16 rmartin16 left a comment

Choose a reason for hiding this comment

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

Well timed; this is meeting my needs of creating specific projects in CI.

briefcase create linux AppImage \
    --config 'template="../../"' \
    --config 'requires=["toga-gtk==0.3.1", "PyGobject<3.46.0"]'

I guess I hadn't considered this before but...apparently attributes can have spaces.

briefcase package -C '"zzz template"={"asdf"=42}'

I gave up trying to get a newline character in to the value for -C....but that shouldn't matter since you can pass multiple values for -C.

👍🏼

Comment on lines 120 to 123
except ValueError:
raise BriefcaseCommandError(
f"Unable to parse configuration override {value}"
)
Copy link
Member

Choose a reason for hiding this comment

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

I think Rich's stacktrace handles this regardless but can you raise from the ValueError?

Suggested change
except ValueError:
raise BriefcaseCommandError(
f"Unable to parse configuration override {value}"
)
except ValueError as e:
raise BriefcaseCommandError(
f"Unable to parse configuration override {value}"
) from e

Copy link
Member

Choose a reason for hiding this comment

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

That should be unnecessary: exceptions raised within an except block are automatically chained unless you say from None.

Copy link
Member Author

Choose a reason for hiding this comment

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

Might as well be explicit.

dest="config_overrides",
action="append",
metavar="KEY=VALUE",
help="Override the value of the app configuration item KEY with VALUE.",
Copy link
Member

Choose a reason for hiding this comment

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

nitpick: no period at the end to match the other options (although, I also shouldn't have added one to -v...)

Suggested change
help="Override the value of the app configuration item KEY with VALUE.",
help="Override the value of the app configuration item KEY with VALUE",

Copy link
Member Author

Choose a reason for hiding this comment

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

Interesting - the very next option defined (--verbosity) did have a period. I've corrected both.

@@ -248,6 +248,7 @@ def __call__(
no_update: bool = False,
test_mode: bool = False,
passthrough: list[str] | None = None,
config_overrides: list[str] | None = None,
Copy link
Member

Choose a reason for hiding this comment

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

needs to be passed to finalize()?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, but not any more with the revised approach that I've implemented.

@@ -68,6 +68,7 @@ def __call__(
update_resources: bool = False,
update_support: bool = False,
test_mode: bool = False,
config_overrides: list[str] | None = None,
Copy link
Member

Choose a reason for hiding this comment

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

needs to be passed to finalize()?

Copy link
Member Author

Choose a reason for hiding this comment

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

Again, Yes, but not any more with the revised approach that I've implemented.

@mhsmith
Copy link
Member

mhsmith commented Nov 16, 2023

@rmartin16: I'm guessing from your comments that you didn't intend to approve this yet?

Comment on lines 117 to 119
for value in config_overrides:
try:
overrides.update(tomllib.loads(value))
Copy link
Member

Choose a reason for hiding this comment

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

I can't see any mention of multi-level config values, like --config 'linux.requires=...', which is valid TOML.

I think this code might actually work with that as long as you only had one of them. But then if you added --config linux.something_else=..., the second linux would overwrite the first instead of merging with it.

We don't necessarily need to support that syntax in this PR, but if we don't support it, we should at least display a useful error when the user tries to use it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good call. Right now, it's probably easier to reject multi-level keys completely. We can always add them later if there's a reasonable use case.

Copy link
Member Author

@freakboy3742 freakboy3742 Nov 17, 2023

Choose a reason for hiding this comment

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

Actually - in the process of looking at this, it occurred to me that this approach avoids validation of any provided value (e.g., you could specify an invalid version number)... and when I went looking to see how I could do validation, I found a much cleaner way to handle the overrides, in a way that both validates and avoids the issue @rmartin16 found with not passing the overrides to some commands.

@rmartin16
Copy link
Member

I meant to....but selecting "comment" instead probably would have been a better choice perhaps. I was mostly trying to communicate "don't feel required to send this back to me with changes".

Copy link
Member

@rmartin16 rmartin16 left a comment

Choose a reason for hiding this comment

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

updating status to comment

@freakboy3742 freakboy3742 requested a review from mhsmith November 17, 2023 04:43
@freakboy3742
Copy link
Member Author

I've updated the PR so that any command-line overrides go through the same validation process as if they were specified in pyproject.toml. This ends up being a lot less invasive on commands, in addition to the validation benefit.

I've also beefed up the validation that is done on the keys, ensuring that deep keys are rejected.

@mhsmith
Copy link
Member

mhsmith commented Nov 17, 2023

@rmartin16: FYI, if you want to cancel an approval, you have to choose "Dismiss review" under "Changes approved" at the bottom of the page.

@freakboy3742 freakboy3742 merged commit fa275e8 into beeware:main Nov 17, 2023
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide command line option to override app settings
3 participants