Skip to content

Commit

Permalink
Create middle dirs when creating a file
Browse files Browse the repository at this point in the history
  • Loading branch information
neoxelox committed Apr 8, 2022
1 parent 05cb391 commit 032dde6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
22 changes: 12 additions & 10 deletions superinvoke/collections/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,47 @@

@task(default=True)
def list(context):
"""List available enviroments."""
"""List available environments."""
from ..main import __ENVS__

cur_env = __ENVS__.Current

table = Table(show_header=True, header_style="bold white")
table.add_column("Name", justify="left")
table.add_column("Tags", style="dim", justify="right")

for env in __ENVS__.All:
table.add_row(
f"[bold green3]{env.name}[/bold green3]" if env == __ENVS__.Current else env.name,
f"[bold green3]{env.name}[/bold green3]" if env == cur_env else env.name,
", ".join(env.tags),
)

constants.console.print("Listing [bold green3]current[/bold green3] and other enviroments:\n")
constants.console.print("Listing [bold green3]current[/bold green3] and other environments:\n")
constants.console.print(table)


@task(
help={
"enviroment": "Environment name to switch to. Example: dev",
"environment": "Environment name to switch to. Example: dev",
}
)
def switch(context, enviroment):
def switch(context, environment):
"""Switch current environment."""
from ..main import __ENVS__

new_env = __ENVS__.ByName(enviroment)
new_env = __ENVS__.ByName(environment)
old_env = __ENVS__.Current

if not new_env:
context.fail(f"{enviroment} is not a valid enviroment")
context.fail(f"{environment} is not a valid environment")

if new_env == old_env:
context.info(f"{enviroment} is already the current enviroment")
context.info(f"{environment} is already the current environment")
return

context.create(utils.path(constants.Paths.ENV), data=[new_env], dir=False)

if new_env != __ENVS__.Current:
context.fail(f"Cannot switch to enviroment {enviroment}")
context.fail(f"Cannot switch to environment {environment}")

context.print(f"Switched to enviroment [green3]{new_env}[/green3] from {old_env}")
context.print(f"Switched to environment [green3]{new_env}[/green3] from {old_env}")
3 changes: 3 additions & 0 deletions superinvoke/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ def create(path: str, data: List[str] = [""], dir: bool = False) -> None:
if dir:
os.makedirs(str(path), exist_ok=True)
else:
dirs = os.path.dirname(str(path))
if dirs:
os.makedirs(dirs, exist_ok=True)
data = [str(line) + "\n" for line in data]
with open(str(path), "w") as f:
f.writelines(data)
Expand Down

0 comments on commit 032dde6

Please sign in to comment.