guibranco / dotnet-aicommitmessage

🧠 🧰 This tool generates AI-powered commit messages via Git hooks, automating meaningful message suggestions from OpenAI and others to improve commit quality and efficiency
https://guibranco.github.io/dotnet-aicommitmessage/
MIT License
1 stars 0 forks source link

[FEATURE] Add Command to Set Environment Variable #38

Open guibranco opened 1 hour ago

guibranco commented 1 hour ago

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:

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:

gitauto-ai[bot] commented 1 hour ago

Click the checkbox below to generate a PR!

@guibranco, You have 5 requests left in this cycle which refreshes on 2024-10-21 10:07:38+00:00. If you have any questions or concerns, please contact us at info@gitauto.ai.