-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add: Add cli for input and output variables of GitHub Actions
- Loading branch information
1 parent
796bdba
commit b2b821f
Showing
5 changed files
with
167 additions
and
0 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 |
---|---|---|
@@ -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) |
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,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 ''}") |
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,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() |
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