Skip to content
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
a058e72
Add YAML config file support
pateash Aug 22, 2024
bd5c589
Add configuration command group with set and get functionalities
pateash Aug 23, 2024
9c429b6
Refactor configuration management and add DB config command
pateash Aug 23, 2024
3d13dd2
Add database utilities and commands
pateash Aug 24, 2024
f3a8fd8
Add config command group and enhance database configuration
pateash Aug 24, 2024
6147396
Update DB config handling and fix import errors
pateash Aug 24, 2024
af46876
Add initialization command for config and refactor tests
pateash Aug 24, 2024
ce69287
Add initialization command for config and refactor tests
pateash Aug 24, 2024
068dc34
Refactor database configuration, enhance tests, add logging
pateash Aug 24, 2024
bbf4db8
Refactor query command to return and display DataFrame
pateash Aug 24, 2024
45e2397
Enhance table printing in `print_df_as_table`
pateash Aug 25, 2024
406f0f7
Add options for number of rows and columns to query command.
pateash Aug 25, 2024
bd31853
Refactor message logging and remove redundant tests
pateash Aug 27, 2024
8d4ec34
Update src/hckr/cli/config.py
pateash Aug 29, 2024
f765031
Remove error exits and improve config CLI documentation
pateash Aug 29, 2024
f45a99e
Merge branch 'hckr-23' of github.com:hckr-cli/hckr into hckr-23
pateash Aug 29, 2024
55ac924
Refactor query handling and remove unused imports
pateash Aug 29, 2024
0beaf9f
Remove redundant import statement from config.py
pateash Aug 29, 2024
470aead
Add comprehensive configuration documentation and examples
pateash Aug 29, 2024
bf8de96
Add database command documentation and examples
pateash Aug 29, 2024
62eab53
Refactor CLI tests to use cli_runner fixture
pateash Aug 29, 2024
7513969
Update sonar exclusions to ignore specific Python files
pateash Aug 29, 2024
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,4 @@ output/
!output/.gitkeep
_build/
out/
*.sqlite
12 changes: 9 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,17 @@ dependencies = [
"pyarrow", # For saving data in Parquet format.
"fastavro", # For handling Avro file format.
"requests",
# "tomli",
"packaging",
"kubernetes",
"kubernetes", # for k8s commands
"yaspin",
"speedtest-cli"
"speedtest-cli", # for net speed command

# SQL Support
"sqlalchemy", # for SQL ORM
"psycopg2-binary", # for Postgres
"pymysql", # for MySQL
# "sqlite", # for SQLITE
"snowflake-sqlalchemy", # For Snowflake
]

[project.urls]
Expand Down
2 changes: 1 addition & 1 deletion src/hckr/__about__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: 2024-present Ashish Patel <ashishpatel0720@gmail.com>
#
# SPDX-License-Identifier: MIT
__version__ = "0.3.3.dev0"
__version__ = "0.4.0.dev0"
13 changes: 12 additions & 1 deletion src/hckr/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
import click
from click_repl import register_repl # type: ignore

from hckr.cli.db import db
from hckr.cli.configure import configure
from hckr.cli.config import config
from hckr.cli.k8s.context import context
from hckr.cli.k8s.namespace import namespace
from hckr.cli.k8s.pod import pod
from .net import net
from .crypto.fernet import fernet
from .data import data
from .info import info
from .k8s import k8s
from .net import net
from ..__about__ import __version__
from ..cli.cron import cron
from ..cli.crypto import crypto
Expand Down Expand Up @@ -103,6 +106,14 @@ def cli(
# NETWORK command
cli.add_command(net)

# config
cli.add_command(config)
cli.add_command(configure)

# database
cli.add_command(db)


# implementing this so that if the user just uses `hckr` we show them something
if __name__ == "__main__":
cli()
168 changes: 168 additions & 0 deletions src/hckr/cli/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
# from ..utils.MessageUtils import *

import click

from ..utils.MessageUtils import PError, PSuccess
from ..utils.config.ConfigUtils import (
init_config,
DEFAULT_CONFIG,
configMessage,
list_config,
set_config_value,
get_config_value,
)


@click.group(
help="Config commands",
context_settings={"help_option_names": ["-h", "--help"]},
)
@click.pass_context
def config(ctx):
"""
Defines a command group for managing application configurations. This group includes commands to set, get, show, and initialize configuration values.
"""
pass
Comment thread
pateash marked this conversation as resolved.


def common_config_options(func):
func = click.option(
"-c",
"--config",
help="Config instance, default: DEFAULT",
default=DEFAULT_CONFIG,
)(func)
return func


@config.command()
@common_config_options
@click.argument("key")
@click.argument("value")
def set(config, key, value):
"""
This command adds a new entry to the config file with key and value

**Example Usage**:

* Setting a value inside DEFAULT config
Note - DEFAULT config is parent of all other configurations and others will inherit its values if not overridden

.. code-block:: shell

$ hckr config set database_host 127.0.0.1

* Similarly, we can also set a value in specific configuration, configuration will be created if not exists

.. code-block:: shell
$ hckr config set database_host 127.0.0.1 --config MY_DATABASE

**Command Reference**:
"""

configMessage(config)
set_config_value(config, key, value)
PSuccess(f"[{config}] {key} <- {value}")

Comment thread
pateash marked this conversation as resolved.

@config.command()
@common_config_options
@click.argument("key")
def get(config, key):
"""
This command returns value for a key in a configuration

**Example Usage**:

* Getting a value for key in DEFAULT config

Note - DEFAULT config is parent of all other configurations and others will inherit its values if not overridden

.. code-block:: shell

$ hckr config get database_host

* Similarly, we can also get a value in specific configuration

.. code-block:: shell
$ hckr config get database_host --config MY_DATABASE

**Command Reference**:
"""

configMessage(config)
try:
value = get_config_value(config, key)
PSuccess(f"[{config}] {key} = {value}")
except ValueError as e:
PError(f"{e}")

Comment thread
pateash marked this conversation as resolved.

@config.command()
@common_config_options
@click.option(
"-a",
"--all",
default=False,
is_flag=True,
help="Whether to show all configs (default: False)",
)
def show(config, all):
"""
This command show list of all keys available in given configuration,
we can also see values in all configurations by providing -a/--all flag

**Example Usage**:

* Getting values for keys in DEFAULT config

Note - DEFAULT config is parent of all other configurations and others will inherit its values if not overridden

.. code-block:: shell

$ hckr config show

* Similarly, we can also get all values in a specific configuration using -c/--config flag

.. code-block:: shell
$ hckr config show -c MY_DATABASE

* Additionally, we can also see all configurations using -a/--all flag

.. code-block:: shell
$ hckr config show --all

**Command Reference**:
"""
list_config(config, all)


@config.command()
@click.option(
"-o",
"--overwrite",
default=False,
is_flag=True,
help="Whether to delete and recreate .hckrcfg file (default: False)",
)
def init(overwrite):
"""
This command Initializes the configuration for the application,
we can also use this option to overwrite existing config and reinitialize.

**Example Usage**:

* Initialising config file .hckrcfg with default settings

.. code-block:: shell

$ hckr config init

* Similarly, we can also delete existing file and recreate using -o/--overwrite flag

.. code-block:: shell
$ hckr config init --overwrite

**Command Reference**:
"""
init_config(overwrite)
89 changes: 89 additions & 0 deletions src/hckr/cli/configure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import click

from ..utils.MessageUtils import PSuccess
from ..utils.config.ConfigUtils import (
list_config,
set_config_value,
)
from ..utils.config.ConfigureUtils import configure_host, configure_creds
from ..utils.config.Constants import (
CONFIG_TYPE,
DB_TYPE,
ConfigType,
db_type_mapping,
DB_NAME,
)


@click.group(
help="easy configurations for other commands (eg. db)",
context_settings={"help_option_names": ["-h", "--help"]},
)
@click.pass_context
def configure(ctx):
"""
Defines a command group for configuration-related commands.
"""
pass


@configure.command("db")
@click.option(
"--config-name",
prompt="Enter a name for this database configuration",
help="Name of the config instance",
)
@click.option(
"--database-type",
prompt="Select the type of database (1=PostgreSQL, 2=MySQL, 3=SQLite, 4=Snowflake)",
type=click.Choice(["1", "2", "3", "4"]),
help="Database type",
)
@click.option("--host", prompt=False, help="Database host")
@click.option("--port", prompt=False, help="Database port")
@click.option("--user", prompt=False, help="Database user")
@click.option(
"--password",
prompt=False, # we will get this value later if it is not provided
hide_input=True,
confirmation_prompt=True,
help="Database password",
)
@click.option("--database-name", prompt=False, help="Database name")
@click.option("--schema", prompt=False, help="Database schema")
@click.option("--account", prompt=False, help="Snowflake Account Id")
@click.option("--warehouse", prompt=False, help="Snowflake warehouse")
@click.option("--role", prompt=False, help="Snowflake role")
def configure_db(
config_name,
database_type,
host,
port,
user,
password,
database_name,
schema,
account,
warehouse,
role,
):
"""Configure database credentials based on the selected database type."""

set_config_value(config_name, CONFIG_TYPE, ConfigType.DATABASE)
selected_db_type = db_type_mapping[database_type]
set_config_value(config_name, DB_TYPE, selected_db_type)

configure_creds(config_name, password, selected_db_type, user)

if not database_name:
database_name = click.prompt("Enter the database name")
set_config_value(config_name, DB_NAME, database_name)

configure_host(
account, config_name, host, port, role, schema, selected_db_type, warehouse
)

PSuccess(
f"Database configuration saved successfully in config instance '{config_name}'"
)
list_config(config_name)
Comment thread
pateash marked this conversation as resolved.
Loading