From 2ccbda5d959a91933c939fddf6047228c6dd5bd8 Mon Sep 17 00:00:00 2001 From: Alexandru Popenta Date: Tue, 24 Jun 2025 17:48:18 +0300 Subject: [PATCH 1/6] rename env to config-env --- CLI.md.sh | 18 ++++++------- .../base_transactions_controller.py | 2 +- multiversx_sdk_cli/cli.py | 6 ++--- .../{cli_env.py => cli_config_env.py} | 26 +++++++++++-------- multiversx_sdk_cli/cli_contracts.py | 2 +- multiversx_sdk_cli/cli_get.py | 2 +- multiversx_sdk_cli/cli_shared.py | 2 +- multiversx_sdk_cli/cli_wallet.py | 2 +- multiversx_sdk_cli/{env.py => config_env.py} | 0 multiversx_sdk_cli/delegation.py | 2 +- multiversx_sdk_cli/dns.py | 2 +- 11 files changed, 34 insertions(+), 30 deletions(-) rename multiversx_sdk_cli/{cli_env.py => cli_config_env.py} (77%) rename multiversx_sdk_cli/{env.py => config_env.py} (100%) diff --git a/CLI.md.sh b/CLI.md.sh index f7b492bb..09684b6c 100755 --- a/CLI.md.sh +++ b/CLI.md.sh @@ -185,15 +185,15 @@ generate() { command "Governance.GetConfig" "governance get-config" command "Governance.GetDelegatedVoteInfo" "governance get-delegated-vote-info" - group "Environment" "env" - command "Environment.New" "env new" - command "Environment.Set" "env set" - command "Environment.Get" "env get" - command "Environment.Dump" "env dump" - command "Environment.Switch" "env switch" - command "Environment.List" "env list" - command "Environment.Remove" "env remove" - command "Environment.Reset" "env reset" + group "ConfigEnv" "config-env" + command "ConfigEnv.New" "config-env new" + command "ConfigEnv.Set" "config-env set" + command "ConfigEnv.Get" "config-env get" + command "ConfigEnv.Dump" "config-env dump" + command "ConfigEnv.Switch" "config-env switch" + command "ConfigEnv.List" "config-env list" + command "ConfigEnv.Remove" "config-env remove" + command "ConfigEnv.Reset" "config-env reset" group "ConfigWallet" "config-wallet" command "ConfigWallet.New" "config-wallet new" diff --git a/multiversx_sdk_cli/base_transactions_controller.py b/multiversx_sdk_cli/base_transactions_controller.py index 7c8203f8..8db775a6 100644 --- a/multiversx_sdk_cli/base_transactions_controller.py +++ b/multiversx_sdk_cli/base_transactions_controller.py @@ -10,6 +10,7 @@ StringValue, ) +from multiversx_sdk_cli.config_env import get_address_hrp from multiversx_sdk_cli.constants import ( ADDRESS_PREFIX, EXTRA_GAS_LIMIT_FOR_GUARDED_TRANSACTIONS, @@ -21,7 +22,6 @@ TRUE_STR_LOWER, ) from multiversx_sdk_cli.cosign_transaction import cosign_transaction -from multiversx_sdk_cli.env import get_address_hrp from multiversx_sdk_cli.errors import BadUserInput, TransactionSigningError from multiversx_sdk_cli.guardian_relayer_data import GuardianRelayerData from multiversx_sdk_cli.interfaces import IAccount diff --git a/multiversx_sdk_cli/cli.py b/multiversx_sdk_cli/cli.py index 0e2a2639..8ad91fd5 100644 --- a/multiversx_sdk_cli/cli.py +++ b/multiversx_sdk_cli/cli.py @@ -10,13 +10,13 @@ from rich.logging import RichHandler import multiversx_sdk_cli.cli_config +import multiversx_sdk_cli.cli_config_env import multiversx_sdk_cli.cli_config_wallet import multiversx_sdk_cli.cli_contracts import multiversx_sdk_cli.cli_data import multiversx_sdk_cli.cli_delegation import multiversx_sdk_cli.cli_deps import multiversx_sdk_cli.cli_dns -import multiversx_sdk_cli.cli_env import multiversx_sdk_cli.cli_faucet import multiversx_sdk_cli.cli_get import multiversx_sdk_cli.cli_governance @@ -30,8 +30,8 @@ import multiversx_sdk_cli.version from multiversx_sdk_cli import config, errors, utils, ux from multiversx_sdk_cli.cli_shared import set_proxy_from_config_if_not_provided +from multiversx_sdk_cli.config_env import get_address_hrp from multiversx_sdk_cli.constants import LOG_LEVELS, SDK_PATH -from multiversx_sdk_cli.env import get_address_hrp logger = logging.getLogger("cli") @@ -140,7 +140,7 @@ def setup_parser(args: list[str]): commands.append(multiversx_sdk_cli.cli_faucet.setup_parser(args, subparsers)) commands.append(multiversx_sdk_cli.cli_multisig.setup_parser(args, subparsers)) commands.append(multiversx_sdk_cli.cli_governance.setup_parser(args, subparsers)) - commands.append(multiversx_sdk_cli.cli_env.setup_parser(subparsers)) + commands.append(multiversx_sdk_cli.cli_config_env.setup_parser(subparsers)) commands.append(multiversx_sdk_cli.cli_get.setup_parser(subparsers)) parser.epilog = """ diff --git a/multiversx_sdk_cli/cli_env.py b/multiversx_sdk_cli/cli_config_env.py similarity index 77% rename from multiversx_sdk_cli/cli_env.py rename to multiversx_sdk_cli/cli_config_env.py index 4e3614a9..2584bd21 100644 --- a/multiversx_sdk_cli/cli_env.py +++ b/multiversx_sdk_cli/cli_config_env.py @@ -3,7 +3,7 @@ from typing import Any from multiversx_sdk_cli import cli_shared -from multiversx_sdk_cli.env import ( +from multiversx_sdk_cli.config_env import ( create_new_env, delete_env, delete_value, @@ -23,12 +23,12 @@ def setup_parser(subparsers: Any) -> Any: parser = cli_shared.add_group_subparser( - subparsers, "env", "Configure MultiversX CLI to use specific environment values." + subparsers, "config-env", "Configure MultiversX CLI to use specific environment values." ) subparsers = parser.add_subparsers() sub = cli_shared.add_command_subparser( - subparsers, "env", "new", "Creates a new environment and sets it as the active environment." + subparsers, "config-env", "new", "Creates a new environment and sets it as the active environment." ) _add_name_arg(sub) sub.add_argument( @@ -38,16 +38,20 @@ def setup_parser(subparsers: Any) -> Any: ) sub.set_defaults(func=new_env) - sub = cli_shared.add_command_subparser(subparsers, "env", "get", "Gets an env value from the active environment.") + sub = cli_shared.add_command_subparser( + subparsers, "config-env", "get", "Gets an env value from the active environment." + ) _add_name_arg(sub) sub.set_defaults(func=get_env_value) - sub = cli_shared.add_command_subparser(subparsers, "env", "set", "Sets an env value for the active environment.") + sub = cli_shared.add_command_subparser( + subparsers, "config-env", "set", "Sets an env value for the active environment." + ) _add_name_arg(sub) sub.add_argument("value", type=str, help="the new value") sub.set_defaults(func=set_env_value) - sub = cli_shared.add_command_subparser(subparsers, "env", "dump", "Dumps the active environment.") + sub = cli_shared.add_command_subparser(subparsers, "config-env", "dump", "Dumps the active environment.") sub.add_argument( "--default", required=False, @@ -57,21 +61,21 @@ def setup_parser(subparsers: Any) -> Any: sub.set_defaults(func=dump) sub = cli_shared.add_command_subparser( - subparsers, "env", "delete", "Deletes an env value from the active environment." + subparsers, "config-env", "delete", "Deletes an env value from the active environment." ) _add_name_arg(sub) sub.set_defaults(func=delete_env_value) - sub = cli_shared.add_command_subparser(subparsers, "env", "switch", "Switch to a different environment.") + sub = cli_shared.add_command_subparser(subparsers, "config-env", "switch", "Switch to a different environment.") _add_name_arg(sub) sub.set_defaults(func=switch_env) - sub = cli_shared.add_command_subparser(subparsers, "env", "list", "List available environments") + sub = cli_shared.add_command_subparser(subparsers, "config-env", "list", "List available environments") sub.set_defaults(func=list_envs) sub = cli_shared.add_command_subparser( subparsers, - "env", + "config-env", "remove", "Deletes an environment from the env file. Will switch to default env.", ) @@ -80,7 +84,7 @@ def setup_parser(subparsers: Any) -> Any: sub = cli_shared.add_command_subparser( subparsers, - "env", + "config-env", "reset", "Deletes the environment file. Default env will be used.", ) diff --git a/multiversx_sdk_cli/cli_contracts.py b/multiversx_sdk_cli/cli_contracts.py index 97b2515c..c79ee17e 100644 --- a/multiversx_sdk_cli/cli_contracts.py +++ b/multiversx_sdk_cli/cli_contracts.py @@ -25,11 +25,11 @@ ) from multiversx_sdk_cli.cli_output import CLIOutputBuilder from multiversx_sdk_cli.config import get_config_for_network_providers +from multiversx_sdk_cli.config_env import MxpyEnv from multiversx_sdk_cli.constants import NUMBER_OF_SHARDS from multiversx_sdk_cli.contract_verification import trigger_contract_verification from multiversx_sdk_cli.contracts import SmartContract from multiversx_sdk_cli.docker import is_docker_installed, run_docker -from multiversx_sdk_cli.env import MxpyEnv from multiversx_sdk_cli.errors import DockerMissingError from multiversx_sdk_cli.ux import show_warning diff --git a/multiversx_sdk_cli/cli_get.py b/multiversx_sdk_cli/cli_get.py index 8982d641..45bd6d3a 100644 --- a/multiversx_sdk_cli/cli_get.py +++ b/multiversx_sdk_cli/cli_get.py @@ -7,7 +7,7 @@ from multiversx_sdk_cli import cli_shared from multiversx_sdk_cli.config import get_config_for_network_providers -from multiversx_sdk_cli.env import MxpyEnv +from multiversx_sdk_cli.config_env import MxpyEnv from multiversx_sdk_cli.errors import ( ArgumentsNotProvidedError, BadUsage, diff --git a/multiversx_sdk_cli/cli_shared.py b/multiversx_sdk_cli/cli_shared.py index b84a203e..d94d47fb 100644 --- a/multiversx_sdk_cli/cli_shared.py +++ b/multiversx_sdk_cli/cli_shared.py @@ -28,6 +28,7 @@ load_password, load_relayer_password, ) +from multiversx_sdk_cli.config_env import MxpyEnv, get_address_hrp from multiversx_sdk_cli.config_wallet import ( get_active_wallet, read_wallet_config_file, @@ -38,7 +39,6 @@ DEFAULT_TX_VERSION, TCS_SERVICE_ID, ) -from multiversx_sdk_cli.env import MxpyEnv, get_address_hrp from multiversx_sdk_cli.errors import ( AddressConfigFileError, ArgumentsNotProvidedError, diff --git a/multiversx_sdk_cli/cli_wallet.py b/multiversx_sdk_cli/cli_wallet.py index 44bf02ad..488d814f 100644 --- a/multiversx_sdk_cli/cli_wallet.py +++ b/multiversx_sdk_cli/cli_wallet.py @@ -9,8 +9,8 @@ from multiversx_sdk.core.address import get_shard_of_pubkey from multiversx_sdk_cli import cli_shared, utils +from multiversx_sdk_cli.config_env import get_address_hrp from multiversx_sdk_cli.constants import NUMBER_OF_SHARDS -from multiversx_sdk_cli.env import get_address_hrp from multiversx_sdk_cli.errors import ( BadUsage, BadUserInput, diff --git a/multiversx_sdk_cli/env.py b/multiversx_sdk_cli/config_env.py similarity index 100% rename from multiversx_sdk_cli/env.py rename to multiversx_sdk_cli/config_env.py diff --git a/multiversx_sdk_cli/delegation.py b/multiversx_sdk_cli/delegation.py index e87e2862..2a158a59 100644 --- a/multiversx_sdk_cli/delegation.py +++ b/multiversx_sdk_cli/delegation.py @@ -8,7 +8,7 @@ from multiversx_sdk.abi import BigUIntValue, Serializer from multiversx_sdk_cli.base_transactions_controller import BaseTransactionsController -from multiversx_sdk_cli.env import get_address_hrp +from multiversx_sdk_cli.config_env import get_address_hrp from multiversx_sdk_cli.errors import BadUsage from multiversx_sdk_cli.guardian_relayer_data import GuardianRelayerData from multiversx_sdk_cli.interfaces import IAccount diff --git a/multiversx_sdk_cli/dns.py b/multiversx_sdk_cli/dns.py index 8cda4db5..50955e34 100644 --- a/multiversx_sdk_cli/dns.py +++ b/multiversx_sdk_cli/dns.py @@ -14,8 +14,8 @@ validate_chain_id_args, validate_transaction_args, ) +from multiversx_sdk_cli.config_env import get_address_hrp from multiversx_sdk_cli.constants import ADDRESS_ZERO_HEX -from multiversx_sdk_cli.env import get_address_hrp from multiversx_sdk_cli.transactions import TransactionsController MaxNumShards = 256 From 9742630900f0ed5c877bce0491b4b82e8a8310e9 Mon Sep 17 00:00:00 2001 From: Alexandru Popenta Date: Tue, 24 Jun 2025 17:55:08 +0300 Subject: [PATCH 2/6] generate CLI file --- CLI.md | 58 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/CLI.md b/CLI.md index 52c1fa51..9eccefb8 100644 --- a/CLI.md +++ b/CLI.md @@ -23,7 +23,7 @@ See: COMMAND GROUPS: - {config-wallet,contract,tx,validator,ledger,wallet,validator-wallet,deps,config,localnet,data,staking-provider,dns,faucet,multisig,governance,env,get} + {config-wallet,contract,tx,validator,ledger,wallet,validator-wallet,deps,config,localnet,data,staking-provider,dns,faucet,multisig,governance,config-env,get} TOP-LEVEL OPTIONS: -h, --help show this help message and exit @@ -51,7 +51,7 @@ dns Operations related to the Domain Name Service faucet Get xEGLD on Devnet or Testnet multisig Deploy and interact with the Multisig Smart Contract governance Propose, vote and interact with the governance contract. -env Configure MultiversX CLI to use specific environment values. +config-env Configure MultiversX CLI to use specific environment values. get Get info from the network. ``` @@ -5632,12 +5632,12 @@ options: --proxy PROXY 🔗 the URL of the proxy ``` -## Group **Environment** +## Group **ConfigEnv** ``` -$ mxpy env --help -usage: mxpy env COMMAND [-h] ... +$ mxpy config-env --help +usage: mxpy config-env COMMAND [-h] ... Configure MultiversX CLI to use specific environment values. @@ -5661,12 +5661,12 @@ remove Deletes an environment from the env file. Will sw reset Deletes the environment file. Default env will be used. ``` -### Environment.New +### ConfigEnv.New ``` -$ mxpy env new --help -usage: mxpy env new [-h] ... +$ mxpy config-env new --help +usage: mxpy config-env new [-h] ... Creates a new environment and sets it as the active environment. @@ -5678,12 +5678,12 @@ options: --template TEMPLATE an environment from which to create the new environment ``` -### Environment.Set +### ConfigEnv.Set ``` -$ mxpy env set --help -usage: mxpy env set [-h] ... +$ mxpy config-env set --help +usage: mxpy config-env set [-h] ... Sets an env value for the active environment. @@ -5695,12 +5695,12 @@ options: -h, --help show this help message and exit ``` -### Environment.Get +### ConfigEnv.Get ``` -$ mxpy env get --help -usage: mxpy env get [-h] ... +$ mxpy config-env get --help +usage: mxpy config-env get [-h] ... Gets an env value from the active environment. @@ -5711,12 +5711,12 @@ options: -h, --help show this help message and exit ``` -### Environment.Dump +### ConfigEnv.Dump ``` -$ mxpy env dump --help -usage: mxpy env dump [-h] ... +$ mxpy config-env dump --help +usage: mxpy config-env dump [-h] ... Dumps the active environment. @@ -5725,12 +5725,12 @@ options: --default dumps the default environment instead of the active one. ``` -### Environment.Switch +### ConfigEnv.Switch ``` -$ mxpy env switch --help -usage: mxpy env switch [-h] ... +$ mxpy config-env switch --help +usage: mxpy config-env switch [-h] ... Switch to a different environment. @@ -5741,12 +5741,12 @@ options: -h, --help show this help message and exit ``` -### Environment.List +### ConfigEnv.List ``` -$ mxpy env list --help -usage: mxpy env list [-h] ... +$ mxpy config-env list --help +usage: mxpy config-env list [-h] ... List available environments @@ -5754,12 +5754,12 @@ options: -h, --help show this help message and exit ``` -### Environment.Remove +### ConfigEnv.Remove ``` -$ mxpy env remove --help -usage: mxpy env remove [-h] ... +$ mxpy config-env remove --help +usage: mxpy config-env remove [-h] ... Deletes an environment from the env file. Will switch to default env. @@ -5770,12 +5770,12 @@ options: -h, --help show this help message and exit ``` -### Environment.Reset +### ConfigEnv.Reset ``` -$ mxpy env reset --help -usage: mxpy env reset [-h] ... +$ mxpy config-env reset --help +usage: mxpy config-env reset [-h] ... Deletes the environment file. Default env will be used. From 61818679c51b6cd156a538a379cec2599336fe00 Mon Sep 17 00:00:00 2001 From: Alexandru Popenta Date: Wed, 25 Jun 2025 11:17:00 +0300 Subject: [PATCH 3/6] refactoring config-env --- CLI.md | 28 +++++------ multiversx_sdk_cli/cli_config_env.py | 31 +++++++----- multiversx_sdk_cli/config_env.py | 75 +++++++++++++++++++++------- 3 files changed, 90 insertions(+), 44 deletions(-) diff --git a/CLI.md b/CLI.md index 9eccefb8..68457d5b 100644 --- a/CLI.md +++ b/CLI.md @@ -5651,13 +5651,13 @@ OPTIONS: COMMANDS summary ---------------- new Creates a new environment and sets it as the active environment. -get Gets an env value from the active environment. -set Sets an env value for the active environment. +get Gets an env value from the specified environment. +set Sets an env value for the specified environment. dump Dumps the active environment. -delete Deletes an env value from the active environment. +delete Deletes an env value from the specified environment. switch Switch to a different environment. list List available environments -remove Deletes an environment from the env file. Will switch to default env. +remove Deletes an environment from the env file. Use `mxpy config-env switch` to move to another env. reset Deletes the environment file. Default env will be used. ``` @@ -5671,7 +5671,7 @@ usage: mxpy config-env new [-h] ... Creates a new environment and sets it as the active environment. positional arguments: - name the name of the configuration entry + name the name of the new environment options: -h, --help show this help message and exit @@ -5685,7 +5685,7 @@ options: $ mxpy config-env set --help usage: mxpy config-env set [-h] ... -Sets an env value for the active environment. +Sets an env value for the specified environment. positional arguments: name the name of the configuration entry @@ -5693,6 +5693,7 @@ positional arguments: options: -h, --help show this help message and exit + --env ENV the name of the environment to operate on ``` ### ConfigEnv.Get @@ -5702,13 +5703,14 @@ options: $ mxpy config-env get --help usage: mxpy config-env get [-h] ... -Gets an env value from the active environment. +Gets an env value from the specified environment. positional arguments: name the name of the configuration entry options: -h, --help show this help message and exit + --env ENV the name of the environment to operate on ``` ### ConfigEnv.Dump @@ -5734,11 +5736,9 @@ usage: mxpy config-env switch [-h] ... Switch to a different environment. -positional arguments: - name the name of the configuration entry - options: -h, --help show this help message and exit + --env ENV the name of the environment to operate on ``` ### ConfigEnv.List @@ -5761,13 +5761,11 @@ options: $ mxpy config-env remove --help usage: mxpy config-env remove [-h] ... -Deletes an environment from the env file. Will switch to default env. - -positional arguments: - environment The environment to remove from env file. +Deletes an environment from the env file. Use `mxpy config-env switch` to move to another env. options: - -h, --help show this help message and exit + -h, --help show this help message and exit + --env ENV the name of the environment to operate on ``` ### ConfigEnv.Reset diff --git a/multiversx_sdk_cli/cli_config_env.py b/multiversx_sdk_cli/cli_config_env.py index 2584bd21..e893aa13 100644 --- a/multiversx_sdk_cli/cli_config_env.py +++ b/multiversx_sdk_cli/cli_config_env.py @@ -30,7 +30,7 @@ def setup_parser(subparsers: Any) -> Any: sub = cli_shared.add_command_subparser( subparsers, "config-env", "new", "Creates a new environment and sets it as the active environment." ) - _add_name_arg(sub) + sub.add_argument("name", type=str, help="the name of the new environment") sub.add_argument( "--template", required=False, @@ -39,16 +39,18 @@ def setup_parser(subparsers: Any) -> Any: sub.set_defaults(func=new_env) sub = cli_shared.add_command_subparser( - subparsers, "config-env", "get", "Gets an env value from the active environment." + subparsers, "config-env", "get", "Gets an env value from the specified environment." ) _add_name_arg(sub) + _add_env_arg(sub) sub.set_defaults(func=get_env_value) sub = cli_shared.add_command_subparser( - subparsers, "config-env", "set", "Sets an env value for the active environment." + subparsers, "config-env", "set", "Sets an env value for the specified environment." ) _add_name_arg(sub) sub.add_argument("value", type=str, help="the new value") + _add_env_arg(sub) sub.set_defaults(func=set_env_value) sub = cli_shared.add_command_subparser(subparsers, "config-env", "dump", "Dumps the active environment.") @@ -61,13 +63,14 @@ def setup_parser(subparsers: Any) -> Any: sub.set_defaults(func=dump) sub = cli_shared.add_command_subparser( - subparsers, "config-env", "delete", "Deletes an env value from the active environment." + subparsers, "config-env", "delete", "Deletes an env value from the specified environment." ) _add_name_arg(sub) + _add_env_arg(sub) sub.set_defaults(func=delete_env_value) sub = cli_shared.add_command_subparser(subparsers, "config-env", "switch", "Switch to a different environment.") - _add_name_arg(sub) + _add_env_arg(sub) sub.set_defaults(func=switch_env) sub = cli_shared.add_command_subparser(subparsers, "config-env", "list", "List available environments") @@ -77,9 +80,9 @@ def setup_parser(subparsers: Any) -> Any: subparsers, "config-env", "remove", - "Deletes an environment from the env file. Will switch to default env.", + "Deletes an environment from the env file. Use `mxpy config-env switch` to move to another env.", ) - sub.add_argument("environment", type=str, help="The environment to remove from env file.") + _add_env_arg(sub) sub.set_defaults(func=remove_env_entry) sub = cli_shared.add_command_subparser( @@ -98,6 +101,10 @@ def _add_name_arg(sub: Any): sub.add_argument("name", type=str, help="the name of the configuration entry") +def _add_env_arg(sub: Any): + sub.add_argument("--env", required=True, type=str, help="the name of the environment to operate on") + + def dump(args: Any): if args.default: dump_out_json(get_defaults()) @@ -106,16 +113,16 @@ def dump(args: Any): def get_env_value(args: Any): - value = get_value(args.name) + value = get_value(args.name, args.env) print(value) def set_env_value(args: Any): - set_value(args.name, args.value) + set_value(args.name, args.value, args.env) def delete_env_value(args: Any): - delete_value(args.name) + delete_value(args.name, args.env) def new_env(args: Any): @@ -124,7 +131,7 @@ def new_env(args: Any): def switch_env(args: Any): - set_active(args.name) + set_active(args.env) dump_out_json(get_active_env()) @@ -139,7 +146,7 @@ def remove_env_entry(args: Any): logger.info("Environment file not found. Aborting...") return - delete_env(args.environment) + delete_env(args.env) def delete_env_file(args: Any): diff --git a/multiversx_sdk_cli/config_env.py b/multiversx_sdk_cli/config_env.py index 40f003a5..8629a9ca 100644 --- a/multiversx_sdk_cli/config_env.py +++ b/multiversx_sdk_cli/config_env.py @@ -46,22 +46,47 @@ def get_defaults() -> dict[str, str]: @cache def get_address_hrp() -> str: - return get_value("default_address_hrp") + """ + Returns the HRP for the active environment. + If not set, it returns the default value. + """ + data = read_env_file() + active_env_name: str = data.get("active", "default") + return get_value("default_address_hrp", active_env_name) @cache def get_proxy_url() -> str: - return get_value("proxy_url") + """ + Returns the proxy URL for the active environment. + If not set, it returns an empty string. + """ + data = read_env_file() + active_env_name: str = data.get("active", "default") + return get_value("proxy_url", active_env_name) @cache def get_explorer_url() -> str: - return get_value("explorer_url") + """ + Returns the explorer URL for the active environment. + If not set, it returns an empty string. + """ + data = read_env_file() + active_env_name: str = data.get("active", "default") + return get_value("explorer_url", active_env_name) @cache def get_confirmation_setting() -> bool: - confirmation_value = get_value("ask_confirmation") + """ + Returns the confirmation setting for the active environment. + If not set, it defaults to False. + """ + data = read_env_file() + active_env_name: str = data.get("active", "default") + + confirmation_value = get_value("ask_confirmation", active_env_name) if confirmation_value.lower() in ["true", "yes", "1"]: return True elif confirmation_value.lower() in ["false", "no", "0"]: @@ -71,11 +96,17 @@ def get_confirmation_setting() -> bool: @cache -def get_value(name: str) -> str: +def get_value(name: str, env_name: str) -> str: _guard_valid_name(name) - data = get_active_env() + data = read_env_file() + + envs = data.get("environments", {}) + env = envs.get(env_name, None) + if env is None: + raise UnknownEnvironmentError(env_name) + default_value = get_defaults()[name] - value = data.get(name, default_value) + value = env.get(name, default_value) assert isinstance(value, str) return value @@ -109,13 +140,18 @@ def resolve_env_path() -> Path: return GLOBAL_ENV_PATH -def set_value(name: str, value: Any): +def set_value(name: str, value: str, env_name: str): _guard_valid_name(name) data = read_env_file() - active_env = data.get("active", "default") - data.setdefault("environments", {}) - data["environments"].setdefault(active_env, {}) - data["environments"][active_env][name] = value + + envs = data.get("environments", {}) + env = envs.get(env_name, None) + if env is None: + raise UnknownEnvironmentError(env_name) + + env[name] = value + envs[env_name] = env + data["environments"] = envs write_file(data) @@ -124,14 +160,19 @@ def write_file(data: dict[str, Any]): write_json_file(str(env_path), data) -def delete_value(name: str): +def delete_value(name: str, env_name: str): """Deletes a key-value pair of the active env.""" _guard_valid_env_deletion(name) data = read_env_file() - active_env = data.get("active", "default") - data.setdefault("environments", {}) - data["environments"].setdefault(active_env, {}) - del data["environments"][active_env][name] + + envs = data.get("environments", {}) + env = envs.get(env_name, None) + if env is None: + raise UnknownEnvironmentError(env_name) + + del env[name] + envs[env_name] = env + data["environments"] = envs write_file(data) From fce7475116b56dc192b1dd1c1ea3af4f7f8641d2 Mon Sep 17 00:00:00 2001 From: Alexandru Popenta Date: Thu, 26 Jun 2025 10:41:41 +0300 Subject: [PATCH 4/6] rename logger --- multiversx_sdk_cli/cli_config_env.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/multiversx_sdk_cli/cli_config_env.py b/multiversx_sdk_cli/cli_config_env.py index e893aa13..2c4282d8 100644 --- a/multiversx_sdk_cli/cli_config_env.py +++ b/multiversx_sdk_cli/cli_config_env.py @@ -18,7 +18,7 @@ from multiversx_sdk_cli.utils import dump_out_json from multiversx_sdk_cli.ux import confirm_continuation -logger = logging.getLogger("cli.env") +logger = logging.getLogger("cli.config_env") def setup_parser(subparsers: Any) -> Any: @@ -80,7 +80,7 @@ def setup_parser(subparsers: Any) -> Any: subparsers, "config-env", "remove", - "Deletes an environment from the env file. Use `mxpy config-env switch` to move to another env.", + "Deletes an environment from the env file. Use `mxpy config-env switch` to switch to another env.", ) _add_env_arg(sub) sub.set_defaults(func=remove_env_entry) From 74a6c1ba06427276c2b769734ff5a1355d3f4ecc Mon Sep 17 00:00:00 2001 From: Alexandru Popenta Date: Thu, 26 Jun 2025 12:04:04 +0300 Subject: [PATCH 5/6] fix for when no env is configured --- multiversx_sdk_cli/config_env.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/multiversx_sdk_cli/config_env.py b/multiversx_sdk_cli/config_env.py index 8629a9ca..bdbb239d 100644 --- a/multiversx_sdk_cli/config_env.py +++ b/multiversx_sdk_cli/config_env.py @@ -52,6 +52,9 @@ def get_address_hrp() -> str: """ data = read_env_file() active_env_name: str = data.get("active", "default") + + if active_env_name == "default": + return get_defaults()["default_address_hrp"] return get_value("default_address_hrp", active_env_name) @@ -63,6 +66,9 @@ def get_proxy_url() -> str: """ data = read_env_file() active_env_name: str = data.get("active", "default") + + if active_env_name == "default": + return get_defaults()["proxy_url"] return get_value("proxy_url", active_env_name) @@ -74,6 +80,9 @@ def get_explorer_url() -> str: """ data = read_env_file() active_env_name: str = data.get("active", "default") + + if active_env_name == "default": + return get_defaults()["explorer_url"] return get_value("explorer_url", active_env_name) @@ -86,6 +95,9 @@ def get_confirmation_setting() -> bool: data = read_env_file() active_env_name: str = data.get("active", "default") + if active_env_name == "default": + return get_defaults()["ask_confirmation"].lower() in ["true", "yes", "1"] + confirmation_value = get_value("ask_confirmation", active_env_name) if confirmation_value.lower() in ["true", "yes", "1"]: return True From 16ca822e89786908fb66c9f7ebb8d30f09f50e7b Mon Sep 17 00:00:00 2001 From: Alexandru Popenta Date: Thu, 26 Jun 2025 16:55:47 +0300 Subject: [PATCH 6/6] refactoring --- multiversx_sdk_cli/config_env.py | 39 ++++++++++++-------------------- 1 file changed, 14 insertions(+), 25 deletions(-) diff --git a/multiversx_sdk_cli/config_env.py b/multiversx_sdk_cli/config_env.py index bdbb239d..8bf5d1ac 100644 --- a/multiversx_sdk_cli/config_env.py +++ b/multiversx_sdk_cli/config_env.py @@ -44,18 +44,23 @@ def get_defaults() -> dict[str, str]: } +def _get_env_value(key: str) -> str: + """Returns the value of a key for the active environment.""" + data = read_env_file() + active_env_name: str = data.get("active", "default") + + if active_env_name == "default": + return get_defaults()[key] + return get_value(key, active_env_name) + + @cache def get_address_hrp() -> str: """ Returns the HRP for the active environment. If not set, it returns the default value. """ - data = read_env_file() - active_env_name: str = data.get("active", "default") - - if active_env_name == "default": - return get_defaults()["default_address_hrp"] - return get_value("default_address_hrp", active_env_name) + return _get_env_value("default_address_hrp") @cache @@ -64,12 +69,7 @@ def get_proxy_url() -> str: Returns the proxy URL for the active environment. If not set, it returns an empty string. """ - data = read_env_file() - active_env_name: str = data.get("active", "default") - - if active_env_name == "default": - return get_defaults()["proxy_url"] - return get_value("proxy_url", active_env_name) + return _get_env_value("proxy_url") @cache @@ -78,12 +78,7 @@ def get_explorer_url() -> str: Returns the explorer URL for the active environment. If not set, it returns an empty string. """ - data = read_env_file() - active_env_name: str = data.get("active", "default") - - if active_env_name == "default": - return get_defaults()["explorer_url"] - return get_value("explorer_url", active_env_name) + return _get_env_value("explorer_url") @cache @@ -92,13 +87,7 @@ def get_confirmation_setting() -> bool: Returns the confirmation setting for the active environment. If not set, it defaults to False. """ - data = read_env_file() - active_env_name: str = data.get("active", "default") - - if active_env_name == "default": - return get_defaults()["ask_confirmation"].lower() in ["true", "yes", "1"] - - confirmation_value = get_value("ask_confirmation", active_env_name) + confirmation_value = _get_env_value("ask_confirmation") if confirmation_value.lower() in ["true", "yes", "1"]: return True elif confirmation_value.lower() in ["false", "no", "0"]: