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

[FEATURE] Add Command to Set Environment Variable #38

Open
guibranco opened this issue Sep 21, 2024 · 13 comments
Open

[FEATURE] Add Command to Set Environment Variable #38

guibranco opened this issue Sep 21, 2024 · 13 comments
Assignees
Labels
📝 documentation Tasks related to writing or updating documentation enhancement New feature or request gitauto GitAuto label to trigger the app in a issue. good first issue Good for newcomers hacktoberfest Participation in the Hacktoberfest event help wanted Extra attention is needed 🕓 medium effort A task that can be completed in a few hours 🧪 tests Tasks related to testing

Comments

@guibranco
Copy link
Owner

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.
@guibranco guibranco added 📝 documentation Tasks related to writing or updating documentation enhancement New feature or request help wanted Extra attention is needed good first issue Good for newcomers hacktoberfest Participation in the Hacktoberfest event 🕓 medium effort A task that can be completed in a few hours 🧪 tests Tasks related to testing labels Sep 21, 2024
@gstraccini gstraccini bot added 🛠 WIP Work in progress and removed 🛠 WIP Work in progress labels Oct 8, 2024
@Tharanishwaran
Copy link

@guibranco can i work on this ?

@guibranco
Copy link
Owner Author

Hi @Tharanishwaran ,

Thanks for picking this up! 🙌
You have been assigned the issue.

If you have any questions or need clarification along the way, feel free to ask.

Good luck and happy coding! 😊

Repository owner deleted a comment from gitauto-ai bot Oct 20, 2024
Repository owner deleted a comment from gitauto-ai bot Oct 20, 2024
@Tharanishwaran
Copy link

Hi @guibranco ,
Thank you for assigning me! I'll start working on it and update you soon

@guibranco guibranco added the 🛠 WIP Work in progress label Oct 22, 2024
@guibranco
Copy link
Owner Author

Hi @Tharanishwaran 👋,

I hope all is well! I wanted to check in on the progress of this issue, as we haven’t received any updates or a pull request from you in over two weeks ⏳.

Are you still working on this? If you encounter any blockers or need help, please feel free to reach out—we're here to support you 💪. If there’s no progress or response within the next week, we may need to unassign you from this issue to ensure it can be addressed on time 🔄.

Thank you for your attention to this, and I look forward to hearing from you soon! 😊

Best,

@Tharanishwaran
Copy link

Hi @guibranco,
I apologize for the delay in communication. Due to current commitments, I won't be able to work on this issue. Please feel free to assign it to someone else who can give it the attention it deserves.
Thank you for the opportunity and understanding.

@guibranco
Copy link
Owner Author

Thanks for letting me know @Tharanishwaran!

@gitauto-ai gitauto-ai bot added the gitauto GitAuto label to trigger the app in a issue. label Nov 12, 2024
Copy link
Contributor

gitauto-ai bot commented Nov 12, 2024

Hey, I'm a bit lost here! Not sure which file I should be fixing. Could you give me a bit more to go on? Maybe add some details to the issue or drop a comment with some extra hints? Thanks!

Have feedback or need help?
Feel free to email [email protected].

@guibranco guibranco removed the gitauto GitAuto label to trigger the app in a issue. label Nov 15, 2024
@gstraccini gstraccini bot removed the 🛠 WIP Work in progress label Nov 15, 2024
@gitauto-ai gitauto-ai bot added the gitauto GitAuto label to trigger the app in a issue. label Nov 18, 2024
Copy link
Contributor

gitauto-ai bot commented Nov 18, 2024

@guibranco Pull request completed! Check it out here #122 🚀

Note: I automatically create a pull request for an unassigned and open issue in order from oldest to newest once a day at 00:00 UTC, as long as you have remaining automation usage. Should you have any questions or wish to change settings or limits, please feel free to contact [email protected] or invite us to Slack Connect.

@gstraccini gstraccini bot added the 🛠 WIP Work in progress label Nov 18, 2024
@gstraccini gstraccini bot removed the 🛠 WIP Work in progress label Dec 12, 2024
@bc2026
Copy link

bc2026 commented Dec 21, 2024

Hey, do you need someone to work on this issue?

@guibranco
Copy link
Owner Author

Hi @bc2026 ,

Yes, sure, if you feel comfortable picking this up, I will assign it to you! 😊
If you have any questions or need clarification along the way, feel free to ask.

@nicholasptran
Copy link

I can take this

@guibranco
Copy link
Owner Author

Hi @nicholasptran👋,

Thanks for picking this up! 🙌
You have been assigned the issue.

If you have any questions or need clarification along the way, feel free to ask.

Good luck and happy coding! 😊

@nicholasptran
Copy link

@guibranco
Status update. I can start working on this next weekend.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📝 documentation Tasks related to writing or updating documentation enhancement New feature or request gitauto GitAuto label to trigger the app in a issue. good first issue Good for newcomers hacktoberfest Participation in the Hacktoberfest event help wanted Extra attention is needed 🕓 medium effort A task that can be completed in a few hours 🧪 tests Tasks related to testing
Projects
Status: No status
4 participants