Skip to content

Commit

Permalink
fixed exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiri Otoupal committed Mar 7, 2024
1 parent 929ed78 commit 537fb52
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
2 changes: 1 addition & 1 deletion abst/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"CLI Command making OCI Bastion and kubernetes usage simple and fast"
)

__version__ = "2.3.29"
__version__ = "2.3.30"
__author__ = "Jiri Otoupal"
__author_email__ = "jiri-otoupal@ips-database.eu"
__license__ = "MIT"
Expand Down
28 changes: 19 additions & 9 deletions abst/bastion_support/oci_bastion.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,21 +409,31 @@ def load_config(cls):

@classmethod
def load_json(cls, path=default_creds_path) -> dict:
if not path.name.endswith(".json"):
logging.error(f"File is not json!")
return {}

if not default_conf_path.exists() and path == default_conf_path:
default_conf_path.parent.mkdir(exist_ok=True)
with open(str(path), "w") as f:
json.dump(
{"last-check": datetime.datetime.timestamp(datetime.datetime.now())},
f, indent=3)

with open(str(path), "r") as f:
creds = json.load(f)
if "delete_this" in creds.keys():
rich.print(
f"[red]'Delete This' Tag not removed! Please Remove it in {path} "
f"before continuing[/red]")
exit(1)
logging.debug(f"Loaded Credentials {creds}")
try:
with open(str(path), "r") as f:
creds = json.load(f)
if isinstance(creds, str):
logging.error(f"Failed to load creds {path} content is loaded as string")
return {}
if "delete_this" in creds.keys():
rich.print(
f"[red]'Delete This' Tag not removed! Please Remove it in {path} "
f"before continuing[/red]")
exit(1)
logging.debug(f"Loaded Credentials {creds}")
except JSONDecodeError:
logging.error(f"Failed to load json {path}")
return {}
return creds

@classmethod
Expand Down
2 changes: 2 additions & 0 deletions abst/cli_commands/context/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def _list(debug=False):
tree = Tree("Contexts")

for file in Path(default_contexts_location).iterdir():
if file.name.startswith(".") or not file.name.endswith(".json"):
continue
cfg = Bastion.load_json(file)
used_time = "" if "last-time-used" not in cfg.keys() else f"| Used: {cfg['last-time-used']}"
tree.add(f"{file.name.replace('.json', '')} {used_time}")
Expand Down
10 changes: 5 additions & 5 deletions abst/cli_commands/parallel/commands.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
from pathlib import Path

import click
import rich
from InquirerPy import inquirer
from rich.tree import Tree

from abst.bastion_support.bastion_scheduler import BastionScheduler
from abst.bastion_support.oci_bastion import Bastion
from abst.config import default_parallel_sets_location, default_contexts_location
from abst.config import default_parallel_sets_location
from abst.tools import display_scheduled
from abst.utils.misc_funcs import setup_calls

Expand Down Expand Up @@ -71,13 +69,15 @@ def _list(debug):
tree = Tree("Sets in parallel folder")

for _set in default_parallel_sets_location.iterdir():
if _set.name.startswith(".") and not _set.name.endswith(".json"):
if _set.name.startswith("."):
continue
leaf = tree.add(f"{_set.name.replace('.json', '')}")
for ctx in _set.iterdir():
if ctx.name.startswith(".") or not ctx.name.endswith(".json"):
continue
cfg = Bastion.load_json(ctx)
used_time = "" if "last-time-used" not in cfg.keys() else f"| last time used {cfg['last-time-used']}"
if ctx.name.startswith("."):
if ctx.name.startswith(".") or not ctx.name.endswith(".json"):
continue
leaf.add(f"{ctx.name.replace('.json', '')} {used_time}")

Expand Down

0 comments on commit 537fb52

Please sign in to comment.