Skip to content

Commit

Permalink
Adding Config and db query Support (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
pateash authored Aug 29, 2024
1 parent 570c5c5 commit 63f17b6
Show file tree
Hide file tree
Showing 26 changed files with 1,169 additions and 20 deletions.
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
11 changes: 11 additions & 0 deletions docs/source/commands/config/config.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.. hckr documentation master file, created by
sphinx-quickstart on Wed Jun 12 20:06:39 2024.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
.. important::
``DEFAULT`` config is the parent of all other configurations and others will inherit its values if not overridden

.. click:: hckr.cli.config:config
:prog: hckr config
:nested: full
8 changes: 8 additions & 0 deletions docs/source/commands/config/configure.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.. _configuration:

.. important::::
``DEFAULT`` config is the parent of all other configurations and others will inherit its values if not overridden
.. click:: hckr.cli.configure:configure
:prog: hckr configure
:nested: full
23 changes: 23 additions & 0 deletions docs/source/commands/config/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.. hckr documentation master file, created by
sphinx-quickstart on Wed Jun 12 20:06:39 2024.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
Configuration
===============
``hckr`` supports various of config commands which helps in configuring other commands, eg. ``hckr db`` command.

.. important::
``DEFAULT`` config is the parent of all other configurations and others will inherit its values if not overridden

.. tip::
More commands will be added in future updates. Stay tuned!


commands
---------------
.. toctree::
config
configure

7 changes: 7 additions & 0 deletions docs/source/commands/database/db.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.. important::
Before using these commands you need to add your database configuration in config (``.hckrcfg`` file), please refer :ref:`Configuring your databases <configuration>`


.. click:: hckr.cli.db:db
:prog: hckr db
:nested: full
15 changes: 15 additions & 0 deletions docs/source/commands/database/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Database
===============
``hckr`` supports various of database commands.

.. important::
Before using these commands you need to add your database configuration in config (``.hckrcfg`` file), please refer :ref:`Configuring your database <configuration>`

.. tip::
More commands will be added in future updates. Stay tuned!

commands
---------------
.. toctree::
db

14 changes: 14 additions & 0 deletions docs/source/commands/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ hckr
:prog: hckr
:nested: short

.. toctree::
:maxdepth: 2
:caption: Configure
:hidden:

config/index

.. toctree::
:maxdepth: 2
:caption: Cron
Expand All @@ -27,6 +34,13 @@ hckr

data/index

.. toctree::
:maxdepth: 2
:caption: Database
:hidden:

database/index

.. toctree::
:maxdepth: 2
:caption: Hash
Expand Down
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 sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ sonar.sources=src
sonar.tests=tests

# exclude for being considered main source files
sonar.exclusions=_build/**,docs/**,**/resources/**,.github/**,tests/**,out/**
sonar.exclusions=_build/**,docs/**,**/resources/**,.github/**,tests/**,out/**,src/hckr/__about__.py,**/__init__.py
# Include test files
sonar.test.inclusions=tests/**

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 <[email protected]>
#
# 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()
170 changes: 170 additions & 0 deletions src/hckr/cli/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
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


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
.. 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}")


@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}")


@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)
Loading

0 comments on commit 63f17b6

Please sign in to comment.