Skip to content
This repository has been archived by the owner on Nov 20, 2023. It is now read-only.

Added functionality to specify default options for commands #736

Merged
merged 4 commits into from
Nov 17, 2020
Merged

Added functionality to specify default options for commands #736

merged 4 commits into from
Nov 17, 2020

Conversation

qpooqp
Copy link
Contributor

@qpooqp qpooqp commented Oct 30, 2020

This is functionality which allows users to specify default options for commands in environment variables and this default options will be automatically applied.

More description with some examples is in #353.

src/tye/Program.DefaultOptionsMiddleware.cs Outdated Show resolved Hide resolved

private static void DefaultOptionsMiddleware(InvocationContext context)
{
if (context.ParseResult.CommandResult.Parent != context.ParseResult.RootCommandResult)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this a common check for these types of middleware? May be worth a comment why you check that.

Copy link
Contributor Author

@qpooqp qpooqp Nov 3, 2020

Choose a reason for hiding this comment

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

Well, not exactly.
I'm checking if the currently executing command is second level (probably not official name but i'm not sure how to name it) command. That means that the command is subcommand of root.
For example:

  • Input tye run is described like this
    • tye is root command
    • run is second level command
  • Input tye build
    • tye is root command
    • build is second level command

This check is not necessary at the moment. I was just trying to be safe for the future.
If there will be added third level command and the name of the command will be same as one of the second level (for example tye build run - run is third level and also second level (from tye run) command), then it may add unintended options. So i rather disable it for the moment.

If you know that there will not be third level commands, then i can remove this chcek.
Othervise i will put some comment there.

return;
}

var originalParseResult = context.ParseResult;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think an example here in comments would be useful. Took me a while to understand what was going on here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added some comments to the code, but here if brief description.

  • Line 24 - Get value from env-var for current command - if nothing found then exit
  • Line 31 - Get all applied options in original input
  • Line 32 - Recreate original input
  • L 34 - Check if option --no-default is applied in original options - if yes, then exit
    Example: tye run --no-default - this will not apply default options
  • L 39 - Parse default options to validate them
  • L 40 - Get valid default options
  • L 41 - Filter only the default options which are not already applied in original input
  • L 43 - Set new parse result (original input plus default options)

private const string DefaultOptionsEnvVarPrefix = "TYE_";
private const string DefaultOptionsEnvVarPostfix = "_ARGS";

private static void DefaultOptionsMiddleware(InvocationContext context)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it feasible to add a test for this middleware? Is there a mock invocation context you could have to parse?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that i can test it. Will add some tests.

@qpooqp
Copy link
Contributor Author

qpooqp commented Nov 3, 2020

@jkotalik Can i have a question about the file src/Microsoft.Tye.Core/StandardOptions.cs?

The file is in project Microsoft.Tye.Core, but it is used only in project tye. There are only options for the command line.
Shouldnt it be rather in project tye?

image

@jkotalik
Copy link
Contributor

StandardOptions

Go ahead an try moving it, see if anything breaks 😄 . I'm fine to move it.


namespace Microsoft.Tye.UnitTests
{
public class DefaultOptionsMiddlewareTests
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice tests!

Copy link
Contributor

@jkotalik jkotalik left a comment

Choose a reason for hiding this comment

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

LGTM besides a small nit to move file locations. Thanks!

@jkotalik jkotalik merged commit 474dae7 into dotnet:master Nov 17, 2020
@jkotalik
Copy link
Contributor

Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants