Skip to content

Commit 6181867

Browse files
committed
refactoring config-env
1 parent 9742630 commit 6181867

3 files changed

Lines changed: 90 additions & 44 deletions

File tree

CLI.md

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5651,13 +5651,13 @@ OPTIONS:
56515651
COMMANDS summary
56525652
----------------
56535653
new Creates a new environment and sets it as the active environment.
5654-
get Gets an env value from the active environment.
5655-
set Sets an env value for the active environment.
5654+
get Gets an env value from the specified environment.
5655+
set Sets an env value for the specified environment.
56565656
dump Dumps the active environment.
5657-
delete Deletes an env value from the active environment.
5657+
delete Deletes an env value from the specified environment.
56585658
switch Switch to a different environment.
56595659
list List available environments
5660-
remove Deletes an environment from the env file. Will switch to default env.
5660+
remove Deletes an environment from the env file. Use `mxpy config-env switch` to move to another env.
56615661
reset Deletes the environment file. Default env will be used.
56625662
56635663
```
@@ -5671,7 +5671,7 @@ usage: mxpy config-env new [-h] ...
56715671
Creates a new environment and sets it as the active environment.
56725672
56735673
positional arguments:
5674-
name the name of the configuration entry
5674+
name the name of the new environment
56755675
56765676
options:
56775677
-h, --help show this help message and exit
@@ -5685,14 +5685,15 @@ options:
56855685
$ mxpy config-env set --help
56865686
usage: mxpy config-env set [-h] ...
56875687
5688-
Sets an env value for the active environment.
5688+
Sets an env value for the specified environment.
56895689
56905690
positional arguments:
56915691
name the name of the configuration entry
56925692
value the new value
56935693
56945694
options:
56955695
-h, --help show this help message and exit
5696+
--env ENV the name of the environment to operate on
56965697
56975698
```
56985699
### ConfigEnv.Get
@@ -5702,13 +5703,14 @@ options:
57025703
$ mxpy config-env get --help
57035704
usage: mxpy config-env get [-h] ...
57045705
5705-
Gets an env value from the active environment.
5706+
Gets an env value from the specified environment.
57065707
57075708
positional arguments:
57085709
name the name of the configuration entry
57095710
57105711
options:
57115712
-h, --help show this help message and exit
5713+
--env ENV the name of the environment to operate on
57125714
57135715
```
57145716
### ConfigEnv.Dump
@@ -5734,11 +5736,9 @@ usage: mxpy config-env switch [-h] ...
57345736
57355737
Switch to a different environment.
57365738
5737-
positional arguments:
5738-
name the name of the configuration entry
5739-
57405739
options:
57415740
-h, --help show this help message and exit
5741+
--env ENV the name of the environment to operate on
57425742
57435743
```
57445744
### ConfigEnv.List
@@ -5761,13 +5761,11 @@ options:
57615761
$ mxpy config-env remove --help
57625762
usage: mxpy config-env remove [-h] ...
57635763
5764-
Deletes an environment from the env file. Will switch to default env.
5765-
5766-
positional arguments:
5767-
environment The environment to remove from env file.
5764+
Deletes an environment from the env file. Use `mxpy config-env switch` to move to another env.
57685765
57695766
options:
5770-
-h, --help show this help message and exit
5767+
-h, --help show this help message and exit
5768+
--env ENV the name of the environment to operate on
57715769
57725770
```
57735771
### ConfigEnv.Reset

multiversx_sdk_cli/cli_config_env.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def setup_parser(subparsers: Any) -> Any:
3030
sub = cli_shared.add_command_subparser(
3131
subparsers, "config-env", "new", "Creates a new environment and sets it as the active environment."
3232
)
33-
_add_name_arg(sub)
33+
sub.add_argument("name", type=str, help="the name of the new environment")
3434
sub.add_argument(
3535
"--template",
3636
required=False,
@@ -39,16 +39,18 @@ def setup_parser(subparsers: Any) -> Any:
3939
sub.set_defaults(func=new_env)
4040

4141
sub = cli_shared.add_command_subparser(
42-
subparsers, "config-env", "get", "Gets an env value from the active environment."
42+
subparsers, "config-env", "get", "Gets an env value from the specified environment."
4343
)
4444
_add_name_arg(sub)
45+
_add_env_arg(sub)
4546
sub.set_defaults(func=get_env_value)
4647

4748
sub = cli_shared.add_command_subparser(
48-
subparsers, "config-env", "set", "Sets an env value for the active environment."
49+
subparsers, "config-env", "set", "Sets an env value for the specified environment."
4950
)
5051
_add_name_arg(sub)
5152
sub.add_argument("value", type=str, help="the new value")
53+
_add_env_arg(sub)
5254
sub.set_defaults(func=set_env_value)
5355

5456
sub = cli_shared.add_command_subparser(subparsers, "config-env", "dump", "Dumps the active environment.")
@@ -61,13 +63,14 @@ def setup_parser(subparsers: Any) -> Any:
6163
sub.set_defaults(func=dump)
6264

6365
sub = cli_shared.add_command_subparser(
64-
subparsers, "config-env", "delete", "Deletes an env value from the active environment."
66+
subparsers, "config-env", "delete", "Deletes an env value from the specified environment."
6567
)
6668
_add_name_arg(sub)
69+
_add_env_arg(sub)
6770
sub.set_defaults(func=delete_env_value)
6871

6972
sub = cli_shared.add_command_subparser(subparsers, "config-env", "switch", "Switch to a different environment.")
70-
_add_name_arg(sub)
73+
_add_env_arg(sub)
7174
sub.set_defaults(func=switch_env)
7275

7376
sub = cli_shared.add_command_subparser(subparsers, "config-env", "list", "List available environments")
@@ -77,9 +80,9 @@ def setup_parser(subparsers: Any) -> Any:
7780
subparsers,
7881
"config-env",
7982
"remove",
80-
"Deletes an environment from the env file. Will switch to default env.",
83+
"Deletes an environment from the env file. Use `mxpy config-env switch` to move to another env.",
8184
)
82-
sub.add_argument("environment", type=str, help="The environment to remove from env file.")
85+
_add_env_arg(sub)
8386
sub.set_defaults(func=remove_env_entry)
8487

8588
sub = cli_shared.add_command_subparser(
@@ -98,6 +101,10 @@ def _add_name_arg(sub: Any):
98101
sub.add_argument("name", type=str, help="the name of the configuration entry")
99102

100103

104+
def _add_env_arg(sub: Any):
105+
sub.add_argument("--env", required=True, type=str, help="the name of the environment to operate on")
106+
107+
101108
def dump(args: Any):
102109
if args.default:
103110
dump_out_json(get_defaults())
@@ -106,16 +113,16 @@ def dump(args: Any):
106113

107114

108115
def get_env_value(args: Any):
109-
value = get_value(args.name)
116+
value = get_value(args.name, args.env)
110117
print(value)
111118

112119

113120
def set_env_value(args: Any):
114-
set_value(args.name, args.value)
121+
set_value(args.name, args.value, args.env)
115122

116123

117124
def delete_env_value(args: Any):
118-
delete_value(args.name)
125+
delete_value(args.name, args.env)
119126

120127

121128
def new_env(args: Any):
@@ -124,7 +131,7 @@ def new_env(args: Any):
124131

125132

126133
def switch_env(args: Any):
127-
set_active(args.name)
134+
set_active(args.env)
128135
dump_out_json(get_active_env())
129136

130137

@@ -139,7 +146,7 @@ def remove_env_entry(args: Any):
139146
logger.info("Environment file not found. Aborting...")
140147
return
141148

142-
delete_env(args.environment)
149+
delete_env(args.env)
143150

144151

145152
def delete_env_file(args: Any):

multiversx_sdk_cli/config_env.py

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,47 @@ def get_defaults() -> dict[str, str]:
4646

4747
@cache
4848
def get_address_hrp() -> str:
49-
return get_value("default_address_hrp")
49+
"""
50+
Returns the HRP for the active environment.
51+
If not set, it returns the default value.
52+
"""
53+
data = read_env_file()
54+
active_env_name: str = data.get("active", "default")
55+
return get_value("default_address_hrp", active_env_name)
5056

5157

5258
@cache
5359
def get_proxy_url() -> str:
54-
return get_value("proxy_url")
60+
"""
61+
Returns the proxy URL for the active environment.
62+
If not set, it returns an empty string.
63+
"""
64+
data = read_env_file()
65+
active_env_name: str = data.get("active", "default")
66+
return get_value("proxy_url", active_env_name)
5567

5668

5769
@cache
5870
def get_explorer_url() -> str:
59-
return get_value("explorer_url")
71+
"""
72+
Returns the explorer URL for the active environment.
73+
If not set, it returns an empty string.
74+
"""
75+
data = read_env_file()
76+
active_env_name: str = data.get("active", "default")
77+
return get_value("explorer_url", active_env_name)
6078

6179

6280
@cache
6381
def get_confirmation_setting() -> bool:
64-
confirmation_value = get_value("ask_confirmation")
82+
"""
83+
Returns the confirmation setting for the active environment.
84+
If not set, it defaults to False.
85+
"""
86+
data = read_env_file()
87+
active_env_name: str = data.get("active", "default")
88+
89+
confirmation_value = get_value("ask_confirmation", active_env_name)
6590
if confirmation_value.lower() in ["true", "yes", "1"]:
6691
return True
6792
elif confirmation_value.lower() in ["false", "no", "0"]:
@@ -71,11 +96,17 @@ def get_confirmation_setting() -> bool:
7196

7297

7398
@cache
74-
def get_value(name: str) -> str:
99+
def get_value(name: str, env_name: str) -> str:
75100
_guard_valid_name(name)
76-
data = get_active_env()
101+
data = read_env_file()
102+
103+
envs = data.get("environments", {})
104+
env = envs.get(env_name, None)
105+
if env is None:
106+
raise UnknownEnvironmentError(env_name)
107+
77108
default_value = get_defaults()[name]
78-
value = data.get(name, default_value)
109+
value = env.get(name, default_value)
79110
assert isinstance(value, str)
80111
return value
81112

@@ -109,13 +140,18 @@ def resolve_env_path() -> Path:
109140
return GLOBAL_ENV_PATH
110141

111142

112-
def set_value(name: str, value: Any):
143+
def set_value(name: str, value: str, env_name: str):
113144
_guard_valid_name(name)
114145
data = read_env_file()
115-
active_env = data.get("active", "default")
116-
data.setdefault("environments", {})
117-
data["environments"].setdefault(active_env, {})
118-
data["environments"][active_env][name] = value
146+
147+
envs = data.get("environments", {})
148+
env = envs.get(env_name, None)
149+
if env is None:
150+
raise UnknownEnvironmentError(env_name)
151+
152+
env[name] = value
153+
envs[env_name] = env
154+
data["environments"] = envs
119155
write_file(data)
120156

121157

@@ -124,14 +160,19 @@ def write_file(data: dict[str, Any]):
124160
write_json_file(str(env_path), data)
125161

126162

127-
def delete_value(name: str):
163+
def delete_value(name: str, env_name: str):
128164
"""Deletes a key-value pair of the active env."""
129165
_guard_valid_env_deletion(name)
130166
data = read_env_file()
131-
active_env = data.get("active", "default")
132-
data.setdefault("environments", {})
133-
data["environments"].setdefault(active_env, {})
134-
del data["environments"][active_env][name]
167+
168+
envs = data.get("environments", {})
169+
env = envs.get(env_name, None)
170+
if env is None:
171+
raise UnknownEnvironmentError(env_name)
172+
173+
del env[name]
174+
envs[env_name] = env
175+
data["environments"] = envs
135176
write_file(data)
136177

137178

0 commit comments

Comments
 (0)