Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ab-smith committed Sep 28, 2024
1 parent ece5ad3 commit 01a655d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
2 changes: 2 additions & 0 deletions cli/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.tmp.yaml
config.yaml
.config.yaml
.clica_config.yaml
32 changes: 19 additions & 13 deletions cli/clica.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
API_URL = ""
GLOBAL_FOLDER_ID = None
TOKEN = ""
USERNAME = ""
EMAIL = ""
PASSWORD = ""

CLICA_CONFG_PATH = ".clica_config.yaml"


@click.group()
def cli():
Expand All @@ -25,29 +27,32 @@ def cli():


@click.command()
def init():
"""Create/Reset the config file"""
def init_config():
"""Create/Reset the config file."""
template_data = {
"rest": {"url": "http://localhost:8000/api"},
"credentials": {"username": "[email protected]", "password": ""},
"credentials": {"email": "[email protected]", "password": ""},
}
if click.confirm(
"This will create a config.yaml file for you to fill and will RESET any exisiting one. Do you wish to continue?"
f"This will create {CLICA_CONFG_PATH} for you to fill and will RESET any exisiting one. Do you wish to continue?"
):
with open("config.yaml", "w") as yfile:
with open(CLICA_CONFG_PATH, "w") as yfile:
yaml.safe_dump(
template_data, yfile, default_flow_style=False, sort_keys=False
)
print(
f"Config file is available at {CLICA_CONFG_PATH}. Please update it with your credentials."
)


try:
with open("config.yaml", "r") as yfile:
with open(CLICA_CONFG_PATH, "r") as yfile:
cli_cfg = yaml.safe_load(yfile)
except FileNotFoundError:
print(
"Config file not found. Running the init command to create it but you need to fill it."
)
init()
init_config()

try:
API_URL = cli_cfg["rest"]["url"]
Expand All @@ -58,7 +63,7 @@ def init():
sys.exit(1)

try:
USERNAME = cli_cfg["credentials"]["username"]
EMAIL = cli_cfg["credentials"]["email"]
PASSWORD = cli_cfg["credentials"]["password"]
except KeyError:
print(
Expand Down Expand Up @@ -89,8 +94,8 @@ def auth(email, password):
data = {"username": email, "password": password}
else:
print("trying credentials from the config file")
if USERNAME and PASSWORD:
data = {"username": USERNAME, "password": PASSWORD}
if EMAIL and PASSWORD:
data = {"username": EMAIL, "password": PASSWORD}
else:
print("Could not find any usable credentials.")
sys.exit(1)
Expand All @@ -106,6 +111,7 @@ def auth(email, password):
print(
"Check your credentials again. You can set them on the config file or on the command line."
)
print(res.json())


def _get_folders():
Expand All @@ -122,7 +128,7 @@ def _get_folders():

@click.command()
def get_folders():
"""Get folders"""
"""Get folders."""
GLOBAL_FOLDER_ID, res = _get_folders()
print("GLOBAL_FOLDER_ID: ", GLOBAL_FOLDER_ID)
print(res)
Expand Down Expand Up @@ -230,7 +236,7 @@ def evidences_templates(file):
cli.add_command(import_assets)
cli.add_command(import_controls)
cli.add_command(evidences_templates)
cli.add_command(init)
cli.add_command(init_config)

if __name__ == "__main__":
cli()

0 comments on commit 01a655d

Please sign in to comment.