Skip to content
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

Connection remove command #1444

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* Currently only supports SQL scripts: `post_deploy: [{sql_script: script.sql}]`
* Added `snow spcs service execute-job` command, which supports creating and executing a job service in the current schema.
* Added `snow app events` command to fetch logs and traces from local and customer app installations
* Added `snow connection remove` command, which removes a connection from the configuration file.

## Fixes and improvements
* Fixed problem with whitespaces in `snow connection add` command
Expand Down
17 changes: 17 additions & 0 deletions src/snowflake/cli/_plugins/connection/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
get_all_connections,
get_connection_dict,
get_default_connection_name,
remove_connection,
set_config_value,
)
from snowflake.cli.api.console import cli_console
Expand Down Expand Up @@ -345,3 +346,19 @@ def set_default(
get_connection_dict(connection_name=name)
set_config_value(section=None, key="default_connection_name", value=name)
return MessageResult(f"Default connection set to: {name}")


@app.command(requires_connection=False)
def remove(
name: str = typer.Argument(
help="Name of the connection, as defined in your `config.toml`",
show_default=False,
),
**options,
):
"""Removes connection from configuration file."""
remove_result = remove_connection(name)
if remove_result:
return MessageResult(f"Successfully removed connection {name}")
else:
raise ClickException(f"Connection {name} not found")
13 changes: 13 additions & 0 deletions src/snowflake/cli/api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ def add_connection(name: str, connection_config: ConnectionConfig):
)


def remove_connection(name: str):
return remove_config_value(CONNECTIONS_SECTION, name)


_DEFAULT_LOGS_CONFIG = {
"save_logs": True,
"path": str(CONFIG_MANAGER.file_path.parent / "logs"),
Expand Down Expand Up @@ -186,6 +190,15 @@ def set_config_value(section: str | None, key: str, value: Any):
conf_file_cache[key] = value


def remove_config_value(section: str, key: str) -> bool:
with _config_file() as conf_file_cache:
if section and conf_file_cache.get(section).get(key):
conf_file_cache[section].pop(key)
return True
else:
return False


def get_logs_config() -> dict:
logs_config = _DEFAULT_LOGS_CONFIG.copy()
if config_section_exists(*LOGS_SECTION_PATH):
Expand Down
Loading
Loading