-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
248 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
from . import misc, tool | ||
from . import env, misc, tool |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from invoke import task | ||
from rich.table import Table | ||
|
||
from .. import constants, utils | ||
|
||
|
||
@task(default=True) | ||
def list(context): | ||
"""List available enviroments.""" | ||
from ..main import __ENVS__ | ||
|
||
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, | ||
", ".join(env.tags), | ||
) | ||
|
||
constants.console.print("Listing [bold green3]current[/bold green3] and other enviroments:\n") | ||
constants.console.print(table) | ||
|
||
|
||
@task( | ||
help={ | ||
"enviroment": "Environment name to switch to. Example: dev", | ||
} | ||
) | ||
def switch(context, enviroment): | ||
"""Switch current environment.""" | ||
from ..main import __ENVS__ | ||
|
||
new_env = __ENVS__.ByName(enviroment) | ||
old_env = __ENVS__.Current | ||
|
||
if not new_env: | ||
context.fail(f"{enviroment} is not a valid enviroment") | ||
|
||
if new_env == old_env: | ||
context.info(f"{enviroment} is already the current enviroment") | ||
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.print(f"Switched to enviroment [green3]{new_env}[/green3] from {old_env}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
from .tool import Tags, Tool, Tools | ||
from .common import Tags | ||
from .env import Env, Envs | ||
from .tool import Tool, Tools |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from .. import utils | ||
|
||
|
||
# Represents the list of available tags. | ||
class Tags(utils.StrEnum): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from typing import Any, Callable, List, Optional | ||
|
||
from .. import constants, utils | ||
from .common import Tags | ||
|
||
|
||
# Represents an environment. | ||
class Env: | ||
name: str | ||
tags: List[Tags] | ||
|
||
def __init__(self, name: str, tags: List[Tags]): | ||
self.name = name | ||
self.tags = tags | ||
|
||
def __str__(self) -> str: | ||
return self.name | ||
|
||
def __eq__(self, other: object) -> bool: | ||
return ( | ||
isinstance(other, Env) | ||
and self.name == other.name | ||
and self.tags == other.tags | ||
) | ||
|
||
def __hash__(self) -> int: | ||
return hash((self.name, tuple(self.tags))) | ||
|
||
|
||
# Represents the list of available environments. | ||
class Envs: | ||
Default: Optional[Callable[[Any], Env]] = None | ||
|
||
@utils.classproperty | ||
def All(cls) -> List[Env]: | ||
all = [] | ||
|
||
for env in dir(cls): | ||
if env in ["All", "Default", "Current"]: | ||
continue | ||
|
||
env = getattr(cls, env) | ||
if isinstance(env, Env): | ||
all.append(env) | ||
|
||
return all | ||
|
||
@utils.classproperty | ||
def Current(cls) -> Optional[Env]: | ||
if utils.exists(utils.path(constants.Paths.ENV)) == "file": | ||
return cls.ByName(utils.read(utils.path(constants.Paths.ENV))[0]) | ||
|
||
if hasattr(cls, "Default") and cls.Default is not None: | ||
return cls.Default(cls) | ||
|
||
return None | ||
|
||
@classmethod | ||
def ByTag(cls, tag) -> List[Env]: | ||
return [env for env in cls.All if tag in env.tags] | ||
|
||
@classmethod | ||
def ByName(cls, name) -> Optional[Env]: | ||
for env in cls.All: | ||
if name == env.name: | ||
return env | ||
|
||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.