diff --git a/CLI.md b/CLI.md index 96424409..e27952f1 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. @@ -5651,41 +5651,41 @@ 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. ``` -### 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. 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 --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. +Sets an env value for the specified environment. positional arguments: name the name of the configuration entry @@ -5693,30 +5693,32 @@ positional arguments: options: -h, --help show this help message and exit + --env ENV the name of the environment to operate on ``` -### 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. +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 ``` -### 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,28 +5727,26 @@ 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. -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 ``` -### 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,28 +5754,26 @@ 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. - -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 ``` -### 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. diff --git a/CLI.md.sh b/CLI.md.sh index ceb6f205..9c7dc806 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 63% rename from multiversx_sdk_cli/cli_env.py rename to multiversx_sdk_cli/cli_config_env.py index 4e3614a9..2c4282d8 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, @@ -18,19 +18,19 @@ 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: 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("name", type=str, help="the name of the new environment") sub.add_argument( "--template", required=False, @@ -38,16 +38,22 @@ 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 specified environment." + ) _add_name_arg(sub) + _add_env_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 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, "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,30 +63,31 @@ 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 specified environment." ) _add_name_arg(sub) + _add_env_arg(sub) sub.set_defaults(func=delete_env_value) - sub = cli_shared.add_command_subparser(subparsers, "env", "switch", "Switch to a different environment.") - _add_name_arg(sub) + sub = cli_shared.add_command_subparser(subparsers, "config-env", "switch", "Switch to a different environment.") + _add_env_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.", + "Deletes an environment from the env file. Use `mxpy config-env switch` to switch 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( subparsers, - "env", + "config-env", "reset", "Deletes the environment file. Default env will be used.", ) @@ -94,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()) @@ -102,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): @@ -120,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()) @@ -135,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/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 7b4d3c5c..959b8e0e 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 68% rename from multiversx_sdk_cli/env.py rename to multiversx_sdk_cli/config_env.py index 40f003a5..8bf5d1ac 100644 --- a/multiversx_sdk_cli/env.py +++ b/multiversx_sdk_cli/config_env.py @@ -44,24 +44,50 @@ 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: - return get_value("default_address_hrp") + """ + Returns the HRP for the active environment. + If not set, it returns the default value. + """ + return _get_env_value("default_address_hrp") @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. + """ + return _get_env_value("proxy_url") @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. + """ + return _get_env_value("explorer_url") @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. + """ + 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"]: @@ -71,11 +97,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 +141,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 +161,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) 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