Skip to content

Commit

Permalink
using constants
Browse files Browse the repository at this point in the history
  • Loading branch information
lnxpy committed Apr 19, 2024
1 parent 6780946 commit 22602c1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
18 changes: 18 additions & 0 deletions pyaction/consts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os

# package info
PROJECT_NAME = "PyAction"
PACKAGE_NAME = "pyaction"
PROJECT_DESC = "Create GitHub Actions Using Python"


# Base-url of package
BASE_URL = os.path.dirname(__file__)


# Path to the copier template
TEMPLATE_PATH = os.path.join(BASE_URL, "template")


# GitHub Action's workflow output environment variable
GITHUB_OUTPUT = os.environ.get("GITHUB_OUTPUT", os.devnull)
14 changes: 7 additions & 7 deletions pyaction/io.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import os
from typing import Dict

from pyaction.consts import GITHUB_OUTPUT
from pyaction.exceptions import WorkflowParameterNotFound

BUFFER_PATH = os.environ.get("GITHUB_OUTPUT", os.devnull)


def write(context: Dict[str, str]) -> None:
"""writes the key(s) (as variables) and value(s) (as values) to the output buffer
"""writes the key(s) (as variables) and value(s) (as values) to the output env variable
Args:
context: variables and values
Expand All @@ -20,9 +19,9 @@ def write(context: Dict[str, str]) -> None:
`name` will be the variable name and `John` is the value.
"""

with open(BUFFER_PATH, "a") as _buffer:
with open(GITHUB_OUTPUT, "a") as _env:
for var, val in context.items():
_buffer.write(f"{var}={val}\r\n")
_env.write(f"{var}={val}\r\n")


def read(param: str) -> str | int | bool | None:
Expand All @@ -36,12 +35,13 @@ def read(param: str) -> str | int | bool | None:
"""

prefix = "INPUT_"
var_name = prefix + param.upper()

try:
value = os.environ[prefix + param.upper()]
value = os.environ[var_name]
except KeyError:
raise WorkflowParameterNotFound(
f"Couldn't read the `{param}` input parameter from your pipeline. "
f"Couldn't read the `{var_name}` input parameter from your pipeline. "
"Make sure it's declared properly."
)

Expand Down

0 comments on commit 22602c1

Please sign in to comment.