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 Environment Variable directive parsing #965

Merged
merged 5 commits into from
Jul 10, 2020

Conversation

fredrikhr
Copy link
Contributor

TL;DR

  • Adds a new extension method UseEnvironmentVariableDirective() to CommandLineBuilder enabling to control process-level environment variables for the invocation pipeline from command-line directives.
    The new directive is: env.

Details

As part of the discussion in #951 the need for an environment variable directive could be benefitial for invocations as well.

This PR adds a parser that intercepts the env directive and extracts a = separated key-value-pair from the directive value.

Added to UseDefaults()

Since the ability to easily control process-level environment variables is quite useful, I propose that we should add the environment variable directive to the UseDefaults() extension method on CommandLineBuilder.

This directive is especially useful in shells (most notably on Windows) where specifying variables during process invocation is not possible without modifying the environment variables of the hosting shell.

Behaviour

The value specified in the env directive is expected to contain a = separated key-value-pair.

  1. The value of the directive is split into two parts at the first ocurrence of =.
    The first component is considered to be the environment variable to set, the second component is the new value for the variable.
  2. Both the variable name and the value are trimmed, stripping leading and trailing whitespace.
  3. System.Envrionment.SetEnvironmentVariable(string variable, string value) is invoked, setting the variable at process-level, overriding the existing value (if any).

Invalid Directives

An env-directive is ignored if:

  • The value of the directive (the text following the : in the directive argument) is an empty or white-space only string.
  • The value of the directive does not contain at least one = character.
  • The string preceeding the first = in the directive value is an empty or white-space only string.

Limitations

This directive cannot modify the behaviour of the .NET Runtime that is controlled by environment variables. When the env-directive is parsed, the .NET Runtime Host has already booted the runtime and envaluated the environment variables at process launch.

Environment variables that are controlled by the .NET Generic Host can be affected by this directive, provided that the Generic Host is started after the invocation pipeline has parsed env-directives, or after the pipeline has finished.

Invocation Pipeline precedence

The middle to parse the env-directives is added immediately following the ExceptionHandler middleware to ensure that environment variables are parsed as early in the pipeline as possible. Envrionment.SetEnvironmentVariable might throw an exception for weird combinatinations for variable names, and these exception should be handled and printed properly using the exception-handling middleware (if present in the pipeline).

fredrikhr added a commit to fredrikhr/dotnet-command-line-api that referenced this pull request Jul 6, 2020
@jonsequitur jonsequitur added the enhancement New feature or request label Jul 6, 2020
@KathleenDollard
Copy link
Contributor

@fredrikhr Thanks for this!

@jonsequitur jonsequitur merged commit 8565a27 into dotnet:master Jul 10, 2020
@jonsequitur
Copy link
Contributor

Thanks @fredrikhr!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants