Skip to content

[FEATURE] Add Command to Set Environment Variable  #38

@guibranco

Description

@guibranco

Description:

I would like to request a feature that adds a new command to a .NET tool project, allowing users to set environment variables via the command-line interface (CLI). The command should accept two arguments:

  1. The variable value (required).
  2. An optional target specifying where the variable should be set (User or Machine scope).

The tool should apply the environment variable to the appropriate scope based on the input.

Why is this needed?

This feature would allow users to easily set environment variables through a .NET CLI tool, simplifying environment setup, especially in CI/CD pipelines or automated scripts. Having the ability to specify the target (User or Machine) will give flexibility based on the user's needs.

Suggested Implementation:

  • The command should set the environment variable for the current user or machine depending on the target specified.
  • If no target is specified, the environment variable should be set for the current user by default.

Example Command Usage:

dotnet mytool set-env MY_VARIABLE=my_value --target User

If --target is not provided, it will default to User.

Example Code:

using System;
using Microsoft.Win32;

public class EnvVarSetter
{
    public static void SetEnvironmentVariable(string variable, string value, string target = "User")
    {
        EnvironmentVariableTarget envTarget;

        if (string.Equals(target, "Machine", StringComparison.OrdinalIgnoreCase))
        {
            envTarget = EnvironmentVariableTarget.Machine;
        }
        else
        {
            envTarget = EnvironmentVariableTarget.User;
        }

        Environment.SetEnvironmentVariable(variable, value, envTarget);
        Console.WriteLine($"Environment variable '{variable}' set to '{value}' for {envTarget}.");
    }
}

// Usage example from the CLI
class Program
{
    static void Main(string[] args)
    {
        if (args.Length < 1)
        {
            Console.WriteLine("Usage: set-env VAR_NAME=value [--target User|Machine]");
            return;
        }

        string[] variableParts = args[0].Split('=');
        if (variableParts.Length != 2)
        {
            Console.WriteLine("Invalid format. Please use VAR_NAME=value.");
            return;
        }

        string variableName = variableParts[0];
        string variableValue = variableParts[1];
        string target = args.Length > 1 && args[1].StartsWith("--target") ? args[1].Split('=')[1] : "User";

        EnvVarSetter.SetEnvironmentVariable(variableName, variableValue, target);
    }
}

Additional Context:

  • This feature will only work on platforms where setting environment variables is allowed (e.g., Windows via EnvironmentVariableTarget).
  • The default behavior is to set the environment variable for the current user if no target is provided.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requestgitautoGitAuto label to trigger the app in a issue.good first issueGood for newcomershacktoberfestParticipation in the Hacktoberfest eventhelp wantedExtra attention is needed📝 documentationTasks related to writing or updating documentation🕓 medium effortA task that can be completed in a few hours🛠 WIPWork in progress🧪 testsTasks related to testing

Projects

Status

In Progress

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions