-
Notifications
You must be signed in to change notification settings - Fork 4
PR for Issues #7 #8
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
base: master
Are you sure you want to change the base?
Changes from 6 commits
5ebe11f
a0d1b67
5378d9f
56b60fc
10581ea
afaf54c
0943780
2d07732
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,16 +8,11 @@ | |
|
|
||
| @click.command() | ||
| @click.argument("name", required=False) | ||
| @click.option("-v", "--value", required=False, default=None, | ||
| help="Variable value") | ||
| @click.option("-u", "--user", is_flag=True, required=False, | ||
| help="Specifies if configure user environment") | ||
| @click.option("-a", "--append", is_flag=True, required=False, | ||
| help="Appends to/Creates environment variable") | ||
| @click.option("-d", "--delete", is_flag=True, required=False, | ||
| help="Deletes environment variable") | ||
| @click.option("-l", "--list-all", is_flag=True, required=False, | ||
| help="Lists all environment variables") | ||
| @click.option("-v", "--value", required=False, default=None, help="Variable value") | ||
| @click.option("-u", "--user", is_flag=True, required=False, help="Specifies if configure user environment") | ||
| @click.option("-a", "--append", is_flag=True, required=False, help="Appends to/Creates environment variable") | ||
| @click.option("-d", "--delete", is_flag=True, required=False, help="Deletes environment variable") | ||
| @click.option("-l", "--list-all", is_flag=True, required=False, help="Lists all environment variables") | ||
| def click_command(name, value, user, append, delete, list_all): | ||
| """ | ||
| Utility to set/get/modify/delete windows environment variables via registry | ||
|
|
@@ -34,6 +29,7 @@ def click_command(name, value, user, append, delete, list_all): | |
| """ | ||
| setenv(name, value, user, append, delete, list_all) | ||
|
|
||
|
|
||
| def setenv(name="", value=None, user=False, append=False, delete=False, list_all=False, suppress_echo=False): | ||
| if list_all: | ||
| result = list_all_variables(user) | ||
|
|
@@ -52,7 +48,7 @@ def setenv(name="", value=None, user=False, append=False, delete=False, list_all | |
|
|
||
| if append: | ||
| if value is not None: | ||
| result = append_variable(name, value, user) | ||
| result = append_variable(name, value, user, suppress_echo) | ||
| else: | ||
| if not suppress_echo: | ||
| click.echo("No value is provided in append mode", err=True) | ||
|
|
@@ -63,7 +59,7 @@ def setenv(name="", value=None, user=False, append=False, delete=False, list_all | |
| elif delete: | ||
| result = delete_variable(name, user) | ||
| else: | ||
| result = get_variable(name, user) | ||
| result = get_variable(name, user, suppress_echo) | ||
|
|
||
| if not suppress_echo: | ||
| click.echo(result) | ||
|
|
@@ -83,11 +79,11 @@ def set_variable(name, value, user): | |
| return False | ||
|
|
||
|
|
||
| def append_variable(name, value, user): | ||
| def append_variable(name, value, user, suppress_echo): | ||
| """ | ||
| Creates/appends environment variable | ||
| """ | ||
| new_val = get_variable(name=name, user=user) | ||
| new_val = get_variable(name, user, suppress_echo) | ||
| if new_val: | ||
| new_val += ";" + value | ||
| else: | ||
|
|
@@ -97,7 +93,7 @@ def append_variable(name, value, user): | |
| return result | ||
|
|
||
|
|
||
| def get_variable(name, user): | ||
| def get_variable(name, user, suppress_echo): | ||
| """ | ||
| Gets the value of environment variable | ||
| """ | ||
|
|
@@ -107,8 +103,10 @@ def get_variable(name, user): | |
| value, regtype = winreg.QueryValueEx(key, name) | ||
| return value | ||
| except WindowsError: | ||
| click.echo("Environment Variable '{}' does not exist".format(name)) | ||
| return "" | ||
| if not suppress_echo: | ||
| click.echo("Environment Variable '{}' does not exist".format(name)) | ||
| raise KeyError | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have some concerns how this will work in CLI implementation. It will be a long unreadable traceback. I think we need to raise a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also unittests fail with current implementation. Need to fix it as well |
||
|
|
||
|
|
||
| def delete_variable(name, user): | ||
| """ | ||
|
|
@@ -122,6 +120,7 @@ def delete_variable(name, user): | |
| except WindowsError: | ||
| return False | ||
|
|
||
|
|
||
| def list_all_variables(user): | ||
| hkey = user_hkey if user else system_hkey | ||
| all_vars = {} | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -24,6 +24,7 @@ def test_02_system_environment(self): | |||||
| def run_checks(self, user): | ||||||
| # check that variable does not exist before start | ||||||
| user_list = ["--user"] if user else [] | ||||||
|
|
||||||
|
||||||
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| # |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.