Skip to content

Commit

Permalink
feat(cli): add new config subcommands (keephq#999)
Browse files Browse the repository at this point in the history
  • Loading branch information
pehlicd authored Mar 22, 2024
1 parent d5653c0 commit e51408e
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 94 deletions.
56 changes: 56 additions & 0 deletions docs/cli/commands/cli-config-new.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
sidebarTitle: "keep config new"
---

Create new config.

## Usage

```
Usage: keep config new [OPTIONS]...
```

## Options
* `interactive`:
* Type: BOOL
* Default: `True`
* Usage: `--interactive`

Create config interactively.

* `url`:
* Type: STRING
* Default: `http://localhost:8080`
* Usage: `--url`

The URL of the Keep backend server.

* `api-key`:
* Type: STRING
* Default: ``
* Usage: `--api-key`

The api key for authenticating over keep.

* `help`:
* Type: BOOL
* Default: `false`
* Usage: `--help`

Show this message and exit.



## CLI Help

```
Usage: keep config new [OPTIONS]
create new config.
Options:
-u, --url TEXT The url of the keep api
-a, --api-key TEXT The api key for keep
-i, --interactive Interactive mode creating keep config (default True)
--help Show this message and exit.
```
68 changes: 0 additions & 68 deletions docs/cli/commands/cli-config-provider.mdx

This file was deleted.

32 changes: 32 additions & 0 deletions docs/cli/commands/cli-config-show.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
sidebarTitle: "keep config show"
---

Show keep configuration.

## Usage

```
Usage: keep config show [OPTIONS]...
```

## Options
* `help`:
* Type: BOOL
* Default: `false`
* Usage: `--help`

Show this message and exit.



## CLI Help

```
Usage: keep config show [OPTIONS]
show the current config.
Options:
--help Show this message and exit.
```
5 changes: 3 additions & 2 deletions docs/cli/commands/cli-config.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ Usage: keep config [OPTIONS] COMMAND [ARGS]...
```
Usage: keep config [OPTIONS] COMMAND [ARGS]...
Set keep configuration.
Manage the config.
Options:
--help Show this message and exit.
Commands:
provider Set the provider configuration.
new create new config.
show show the current config.
```
72 changes: 48 additions & 24 deletions keep/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
except metadata.PackageNotFoundError:
KEEP_VERSION = os.environ.get("KEEP_VERSION", "unknown")


logging_config = {
"version": 1,
"disable_existing_loggers": False,
Expand Down Expand Up @@ -116,9 +115,9 @@ def set_config(self, keep_config: str):
pass
self.api_key = self.config.get("api_key") or os.getenv("KEEP_API_KEY") or ""
self.keep_api_url = (
self.config.get("keep_api_url")
or os.getenv("KEEP_API_URL")
or Info.KEEP_MANAGED_API_URL
self.config.get("keep_api_url")
or os.getenv("KEEP_API_URL")
or Info.KEEP_MANAGED_API_URL
)
self.random_user_id = self.config.get("random_user_id")
# if we don't have a random user id, we create one and keep it on the config file
Expand All @@ -132,10 +131,10 @@ def set_config(self, keep_config: str):

# if we auth, we don't need to check for api key
if (
"auth" in arguments
or "api" in arguments
or "config" in arguments
or "version" in arguments
"auth" in arguments
or "api" in arguments
or "config" in arguments
or "version" in arguments
):
return

Expand Down Expand Up @@ -220,14 +219,39 @@ def version():
click.echo(click.style(KEEP_VERSION, bold=True))


@cli.command()
@cli.group()
@pass_info
def config(info: Info):
"""Get the config."""
keep_url = click.prompt("Enter your keep url", default="http://localhost:8080")
api_key = click.prompt(
"Enter your api key (leave blank for localhost)", hide_input=True, default=""
)
"""Manage the config."""
pass


@config.command(name="show")
@pass_info
def show(info: Info):
"""show the current config."""
click.echo(click.style("Current config", bold=True))
for key, value in info.config.items():
click.echo(f"{key}: {value}")


@config.command(name="new")
@click.option("--url", "-u", type=str, required=False, is_flag=False, flag_value="http://localhost:8080", help="The url of the keep api")
@click.option("--api-key", "-a", type=str, required=False, is_flag=False, flag_value="", help="The api key for keep")
@click.option("--interactive", "-i", help="Interactive mode creating keep config (default True)", is_flag=True)
@pass_info
def new_config(info: Info, url: str, api_key: str, interactive: bool):
"""create new config."""
ctx = click.get_current_context()

if not interactive:
keep_url = ctx.params.get("url")
api_key = ctx.params.get("api_key")
else:
keep_url = click.prompt("Enter your keep url", default="http://localhost:8080")
api_key = click.prompt(
"Enter your api key (leave blank for localhost)", hide_input=True, default=""
)
if not api_key:
api_key = "localhost"
with open(f"{get_default_conf_file_path()}", "w") as f:
Expand Down Expand Up @@ -338,14 +362,14 @@ def api(multi_tenant: bool, port: int, host: str):
)
@pass_info
def run(
info: Info,
alerts_directory: str,
alert_url: list[str],
interval: int,
providers_file,
tenant_id,
api_key,
api_url,
info: Info,
alerts_directory: str,
alert_url: list[str],
interval: int,
providers_file,
tenant_id,
api_key,
api_url,
):
"""Run a workflow."""
logger.debug(f"Running alert in {alerts_directory or alert_url}")
Expand Down Expand Up @@ -746,7 +770,7 @@ def list_mappings(info: Info):
)
@pass_info
def create(
info: Info, name: str, description: str, file: str, matchers: str, priority: int
info: Info, name: str, description: str, file: str, matchers: str, priority: int
):
"""Create a mapping rule."""
if os.path.isfile(file) and file.endswith(".csv"):
Expand Down Expand Up @@ -973,7 +997,7 @@ def connect(ctx, help: bool, provider_name, provider_type, params):
for config in provider["config"]:
config_as_flag = f"--{config.replace('_', '-')}"
if config_as_flag not in options_dict and provider["config"][config].get(
"required", True
"required", True
):
raise click.BadOptionUsage(
config_as_flag,
Expand Down

0 comments on commit e51408e

Please sign in to comment.