-
Notifications
You must be signed in to change notification settings - Fork 8
[FEATURE] Add set-env command to manage environment variables #315
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
Open
AmarYasser1
wants to merge
5
commits into
guibranco:main
Choose a base branch
from
AmarYasser1:feature/add-set-env-command
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9de7159
feat: add set-env command to manage environment variables
AmarYasser1 577308e
style: apply CSharpier formatting and fix all code style issues
AmarYasser1 2048eae
style: fix EOF newlines in all files (CSharpier)
AmarYasser1 8b15ace
Merge branch 'main' into feature/add-set-env-command
guibranco 29a0ff7
Merge branch 'main' into feature/add-set-env-command
guibranco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
Src/AiCommitMessage/Options/SetEnvironmentVariableOptions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| using CommandLine; | ||
|
|
||
| namespace AiCommitMessage.Options; | ||
|
|
||
| /// <summary> | ||
| /// Class SetEnvironmentVariableOptions. | ||
| /// </summary> | ||
| [Verb("set-env", HelpText = "Set the Environment Variable.")] | ||
| public class SetEnvironmentVariableOptions | ||
| { | ||
| /// <summary> | ||
| /// Gets or sets the variable. | ||
| /// </summary> | ||
| /// <value>The variable.</value> | ||
| [Value(0, Required = true, HelpText = "The environment variable in VAR_NAME=value format.")] | ||
| public string Variable { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the target. | ||
| /// </summary> | ||
| /// <value>The target.</value> | ||
| [Option( | ||
| 't', | ||
| "target", | ||
| Required = false, | ||
| Default = "User", | ||
| HelpText = "The environment variable target (User or Machine). Default is User." | ||
| )] | ||
| public string Target { get; set; } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
Src/AiCommitMessage/Services/EnvironmentVariableService.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| using AiCommitMessage.Options; | ||
| using AiCommitMessage.Utility; | ||
|
|
||
| namespace AiCommitMessage.Services; | ||
|
|
||
| /// <summary> | ||
| /// Service for managing environment variables. | ||
| /// </summary> | ||
| public static class EnvironmentVariableService | ||
| { | ||
| /// <summary> | ||
| /// Outputs an error message and sets the exit code to 1. | ||
| /// </summary> | ||
| /// <param name="message">The error message to display.</param> | ||
| private static void ErrorLine(string message) | ||
| { | ||
| Output.ErrorLine(message); | ||
| Environment.ExitCode = 1; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Sets an environment variable based on the provided options. | ||
| /// </summary> | ||
| /// <param name="setEnvironmentVariableOptions">The options containing the variable name, value, and target scope.</param> | ||
| /// <remarks> | ||
| /// This method parses the variable string in the format "VAR_NAME=value" and sets the environment variable | ||
| /// to the specified target scope (User or Machine). If no target is specified, it defaults to User scope. | ||
| /// Setting environment variables with Machine scope requires administrator privileges on Windows. | ||
| /// </remarks> | ||
| /// <exception cref="System.Security.SecurityException">Thrown when attempting to set a Machine-level variable without sufficient permissions.</exception> | ||
| /// <exception cref="ArgumentException">Thrown when the variable name contains invalid characters.</exception> | ||
| public static void SetEnvironmentVariable( | ||
| SetEnvironmentVariableOptions setEnvironmentVariableOptions | ||
| ) | ||
| { | ||
| if (string.IsNullOrWhiteSpace(setEnvironmentVariableOptions.Variable)) | ||
| { | ||
| ErrorLine("Variable cannot be null or empty."); | ||
| return; | ||
| } | ||
|
|
||
| var variable = setEnvironmentVariableOptions.Variable.Split('=', 2); | ||
| if (variable.Length != 2) | ||
| { | ||
| ErrorLine("Invalid variable format. Please use the format: VAR_NAME=value"); | ||
| return; | ||
| } | ||
|
|
||
| var variableName = variable[0].Trim(); | ||
| var variableValue = variable[1].Trim(); | ||
| if (string.IsNullOrWhiteSpace(variableName)) | ||
| { | ||
| ErrorLine("Variable name cannot be empty."); | ||
| return; | ||
| } | ||
|
|
||
| EnvironmentVariableTarget envTarget; | ||
| if ( | ||
| string.Equals( | ||
| setEnvironmentVariableOptions.Target, | ||
| "Machine", | ||
| StringComparison.OrdinalIgnoreCase | ||
| ) | ||
| ) | ||
| { | ||
| envTarget = EnvironmentVariableTarget.Machine; | ||
| } | ||
| else if ( | ||
| string.Equals( | ||
| setEnvironmentVariableOptions.Target, | ||
| "User", | ||
| StringComparison.OrdinalIgnoreCase | ||
| ) | ||
| ) | ||
| { | ||
| envTarget = EnvironmentVariableTarget.User; | ||
| } | ||
| else | ||
| { | ||
| ErrorLine( | ||
| $"Invalid target '{setEnvironmentVariableOptions.Target}'. Please use 'User' or 'Machine'." | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| try | ||
| { | ||
| Environment.SetEnvironmentVariable(variableName, variableValue, envTarget); | ||
|
|
||
| Output.InfoLine( | ||
| $"Environment variable '{variableName}' set to '{variableValue}' for {envTarget} scope." | ||
| ); | ||
| } | ||
| catch (System.Security.SecurityException) | ||
| { | ||
| ErrorLine( | ||
| "Permission denied. Setting Machine-level environment variables requires administrator privileges." | ||
| ); | ||
| } | ||
| catch (ArgumentException ex) | ||
| { | ||
| ErrorLine($"Invalid argument: {ex.Message}"); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| ErrorLine($"An error occurred while setting the environment variable: {ex.Message}"); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.