Skip to content

Commit

Permalink
Add: Add cli for input and output variables of GitHub Actions
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoernricks committed Oct 25, 2022
1 parent 796bdba commit b2b821f
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pontos/github/actions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from .errors import GitHubActionsError
from .main import main

__all__ = (
"main",
"GitHubActionsError",
)
75 changes: 75 additions & 0 deletions pontos/github/actions/argparser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Copyright (C) 2022 Greenbone Networks GmbH
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

""" Argument parser for pontos-github-actions """

from argparse import ArgumentParser, Namespace
from typing import List

from .cmds import actions_input, actions_output


def split_pairs(value: str):
if not "=" in value:
raise ValueError(f"Must contain a 'name=value' pair not '{value}'.")
return tuple(value.split("=", 1))


def parse_args(
args: List[str] = None,
) -> Namespace:
"""
Parsing args for Pontos GitHub Actions
"""

parser = ArgumentParser(
description="Greenbone GitHub Actions API.",
)

parser.add_argument(
"--quiet",
"-q",
action="store_true",
help="Don't print messages to the terminal",
)

subparsers = parser.add_subparsers(
title="subcommands",
description="valid subcommands",
required=True,
help="additional help",
dest="command",
)

output_parser = subparsers.add_parser("output", help="Set output variables")
output_parser.add_argument(
"output", help="Output as name=value pairs", type=split_pairs, nargs="+"
)

output_parser.set_defaults(func=actions_output)

input_parser = subparsers.add_parser("input", help="Print input variables")
input_parser.add_argument(
"input",
help="Name of the input variable to print",
nargs="+",
)
input_parser.add_argument("--format", choices=["json"])

input_parser.set_defaults(func=actions_input)

return parser.parse_args(args)
48 changes: 48 additions & 0 deletions pontos/github/actions/cmds.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Copyright (C) 2022 Greenbone Networks GmbH
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import json
from argparse import Namespace
from typing import List, Tuple

from pontos.terminal import Terminal

from .core import ActionIO


def actions_output(_terminal: Terminal, args: Namespace) -> None:
"""
Set output variables
"""
output: List[Tuple[str, str]] = args.output
for pair in output:
name, value = pair
ActionIO.output(name, value)


def actions_input(terminal: Terminal, args: Namespace) -> None:
names: List[str] = args.input

inputs = {}
for name in names:
inputs[name] = ActionIO.input(name)

if args.format == "json":
terminal.out(json.dumps(inputs))
else:
for name, value in inputs.items():
terminal.out(f"{name}={value if value else ''}")
35 changes: 35 additions & 0 deletions pontos/github/actions/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright (C) 2021 Greenbone Networks GmbH
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from pontos.github.actions.argparser import parse_args
from pontos.terminal.null import NullTerminal
from pontos.terminal.rich import RichTerminal


def main(args=None):
parsed_args = parse_args(args)

if parsed_args.quiet:
term = NullTerminal()
else:
term = RichTerminal()

parsed_args.func(term, parsed_args)


if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ pontos-release = 'pontos.release:main'
pontos-update-header = 'pontos.updateheader:main'
pontos-changelog = 'pontos.changelog:main'
pontos-github = 'pontos.github:main'
pontos-github-actions = 'pontos.github.actions:main'

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down

0 comments on commit b2b821f

Please sign in to comment.