Skip to content

Commit

Permalink
implement initial version of profile_set
Browse files Browse the repository at this point in the history
  • Loading branch information
xmnlab committed Dec 14, 2023
1 parent 54ce0d7 commit 6291d12
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 11 deletions.
39 changes: 38 additions & 1 deletion .envers/data.lock
Original file line number Diff line number Diff line change
@@ -1,9 +1,46 @@
version: 0.1
releases:
'2.0':
spec:
status: draft
docs: ''
profiles:
- base
spec:
files:
.env:
type: dotenv
vars:
ENV:
type: string
default: dev
encrypted: false
PROJECT_NAME:
type: string
default: envers
encrypted: false
VERSION:
type: string
default: '1.0'
encrypted: false
USE_CONTAINER:
type: bool
default: true
encrypted: false
data:
base:
files:
.env:
type: dotenv
vars:
ENV: dev
PROJECT_NAME: envers
VERSION: '1.0'
USE_CONTAINER: true
'1.0':
spec:
status: draft
help: ''
docs: ''
profiles:
- base
spec:
Expand Down
28 changes: 27 additions & 1 deletion .envers/specs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: 0.1
releases:
'1.0':
status: draft
help: ''
docs: ''
profiles:
- base
spec:
Expand All @@ -22,3 +22,29 @@ releases:
type: string
default: '1.0'
encrypted: false
'2.0':
status: draft
docs: ''
profiles:
- base
spec:
files:
.env:
type: dotenv
vars:
ENV:
type: string
default: dev
encrypted: false
PROJECT_NAME:
type: string
default: envers
encrypted: false
VERSION:
type: string
default: '1.0'
encrypted: false
USE_CONTAINER:
type: bool
default: true
encrypted: false
29 changes: 26 additions & 3 deletions src/envers/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import typer

from typing_extensions import Annotated

from envers.core import Envers

app = typer.Typer()
Expand Down Expand Up @@ -48,9 +50,30 @@ def draft(version: str, from_version: str = "", from_env: str = "") -> None:


@app.command()
def profile_set(profile_name: str, spec_version: str) -> None:
"""Add new content to a profile."""
print(profile_name, spec_version)
def profile_set(
profile: Annotated[
str, typer.Option(help="The name of the profile to set values for.")
] = "",
spec: Annotated[
str, typer.Option(help="The version of the spec to use.")
] = "",
) -> None:
"""
Set the profile values for a given spec version.
Parameters
----------
profile : str
The name of the profile to set values for.
spec : str
The version of the spec to use.
Returns
-------
None
"""
envers = Envers()
envers.profile_set(profile, spec)


@app.command()
Expand Down
74 changes: 68 additions & 6 deletions src/envers/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def init(self, path: Path) -> None:

# Create and write the default content to spec.yaml
with open(spec_file, "w") as file:
file.write("version: 0.1\nrelease:\n")
file.write("version: 0.1\nreleases:\n")

def draft(
self, version: str, from_version: str = "", from_env: str = ""
Expand Down Expand Up @@ -111,7 +111,7 @@ def draft(
else:
specs["releases"][version] = {
"status": "draft",
"help": "",
"docs": "",
"profiles": ["base"],
"spec": {"files": {}},
}
Expand Down Expand Up @@ -171,10 +171,15 @@ def deploy(self, version: str) -> None:

spec = specs["releases"][version]

data_lock = {
"version": specs["version"],
"releases": {version: {"spec": spec, "data": {}}},
}
if data_lock_file.exists():
with open(data_lock_file, "r") as file:
data_lock = yaml.safe_load(file) or {}
data_lock["releases"][version] = {"spec": spec, "data": {}}
else:
data_lock = {
"version": specs["version"],
"releases": {version: {"spec": spec, "data": {}}},
}

# Populate data with default values
for profile_name in spec.get("profiles", []):
Expand All @@ -194,3 +199,60 @@ def deploy(self, version: str) -> None:

with open(data_lock_file, "w") as file:
yaml.dump(data_lock, file, sort_keys=False)

def profile_set(self, profile: str, spec: str) -> None:
"""
Set the profile values for a given spec version.
Parameters
----------
profile : str
The name of the profile to set values for.
spec : str
The version of the spec to use.
Returns
-------
None
"""
data_lock_file = Path(".envers") / "data.lock"

if not data_lock_file.exists():
typer.echo(
"Data lock file not found. Please deploy a version first."
)
raise typer.Exit()

with open(data_lock_file, "r") as file:
data_lock = yaml.safe_load(file) or {}

if not data_lock.get("releases", {}).get(spec, ""):
typer.echo(f"Version {spec} not found in data.lock.")
raise typer.Exit()

release_data = data_lock["releases"][spec]
spec_data = release_data.get("spec", {})
profile_data = release_data.get("data", {}).get(profile, {"files": {}})

# Iterate over files and variables
for file_path, file_info in spec_data.get("files", {}).items():
for var_name, var_info in file_info.get("vars", {}).items():
current_value = (
profile_data.get("files", {})
.get(file_path, {})
.get("vars", {})
.get(var_name, var_info.get("default", ""))
)
new_value = typer.prompt(
f"Enter value for {var_name} in {file_path} "
f"(Profile: {profile})",
default=current_value,
)
if not profile_data.get("files", {}).get(file_path, {}):
profile_data["files"][file_path] = {"vars": {}}
profile_data["files"][file_path]["vars"][var_name] = new_value

# Update data.lock file
data_lock["releases"][spec]["data"][profile] = profile_data
with open(data_lock_file, "w") as file:
yaml.dump(data_lock, file, sort_keys=False)

0 comments on commit 6291d12

Please sign in to comment.