Skip to content

Commit

Permalink
feat(entrypoint): add CreatCDEntrypoint
Browse files Browse the repository at this point in the history
Related issues: PSCE-256
Majority of code generated by chatGPT
Signed-off-by: Alex Flom <[email protected]>
  • Loading branch information
afflom authored and jpower432 committed Oct 23, 2023
1 parent 83a0e59 commit 88cf980
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
9 changes: 9 additions & 0 deletions tests/testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,12 @@ def replace_string_in_file(file_path: str, old_string: str, new_string: str) ->
# Write the updated content back to the file
with open(file_path, "w") as file:
file.write(updated_content)


def args_dict_to_list(args_dict: Dict[str, str]) -> List[str]:
args = []
for k, v in args_dict.items():
args.append(f"--{k}")
if v is not None:
args.append(v)
return args
102 changes: 102 additions & 0 deletions trestlebot/entrypoints/create_cd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/usr/bin/python

# Copyright 2023 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import argparse
from typing import List

from trestlebot.entrypoints.entrypoint_base import EntrypointBase, comma_sep_to_list
from trestlebot.tasks.authored.compdef import AuthoredComponentDefinition
from trestlebot.tasks.base_task import TaskBase
from trestlebot.tasks.regenerate_task import RegenerateTask
from trestlebot.tasks.rule_transform_task import RuleTransformTask
from trestlebot.transformers.yaml_transformer import ToRulesYAMLTransformer


class CreateCDEntrypoint(EntrypointBase):
"""Entrypoint for setting default component fields."""

def __init__(self, parser: argparse.ArgumentParser) -> None:
"""Initialize."""
super().__init__(parser)
self.setup_set_default_component_fields_arguments()

def setup_set_default_component_fields_arguments(self) -> None:
"""Setup specific arguments for this entrypoint."""
self.parser.add_argument(
"--profile-name", required=True, help="Name of profile"
)
self.parser.add_argument(
"--compdef-name", required=True, help="Name of component definition"
)
self.parser.add_argument(
"--comp-title", required=True, help="Title of component"
)
self.parser.add_argument(
"--comp-description", required=True, help="Description of component"
)
self.parser.add_argument(
"--cd-type",
required=False,
default="service",
help="Type of component definition",
)

def run(self, args: argparse.Namespace) -> None:
"""Run the entrypoint."""
authored_comp = AuthoredComponentDefinition(args.working_dir)
authored_comp.create_new_default(
args.profile_name,
args.compdef_name,
args.comp_title,
args.comp_description,
args.cd_type,
)

transformer: ToRulesYAMLTransformer = ToRulesYAMLTransformer()

rule_transform_task: RuleTransformTask = RuleTransformTask(
args.working_dir,
args.rules_view_path,
transformer,
comma_sep_to_list(args.skip_items),
)
pre_tasks: List[TaskBase] = [rule_transform_task]

regenerate_task = RegenerateTask(
args.working_dir,
args.oscal_model,
args.markdown_path,
args.ssp_index_path,
comma_sep_to_list(args.skip_items),
)
pre_tasks.append(regenerate_task)

super().run_base(args, pre_tasks)


def main() -> None:
"""Run the CLI."""
parser = argparse.ArgumentParser(
description="Set default component fields entrypoint for trestle."
)
set_default_component_fields = CreateCDEntrypoint(parser=parser)

args = parser.parse_args()
set_default_component_fields.run(args)


if __name__ == "__main__":
main()

0 comments on commit 88cf980

Please sign in to comment.