Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(generate): reproducing a generation annotation #520

Merged
merged 2 commits into from
Jul 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/cmd/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package generate

import (
"fmt"
"strings"

oscalTypes_1_1_2 "github.com/defenseunicorns/go-oscal/src/types/oscal-1-1-2"
"github.com/defenseunicorns/lula/src/pkg/common/network"
Expand Down Expand Up @@ -59,13 +60,16 @@ var generateComponentCmd = &cobra.Command{
Args: cobra.MaximumNArgs(1),
Short: "Generate a component definition OSCAL template",
Example: componentHelp,
Run: func(_ *cobra.Command, args []string) {
Run: func(cmd *cobra.Command, args []string) {

var remarks []string
var title = "Component Title"

// check for Catalog Source - this field is required
if componentOpts.CatalogSource == "" {
message.Fatal(fmt.Errorf("no catalog source provided"), "generate component requires a catalog input source")
}
source := componentOpts.CatalogSource

// Assign remarks from flag or default to "statement"
if len(componentOpts.Remarks) == 0 {
Expand All @@ -79,7 +83,13 @@ var generateComponentCmd = &cobra.Command{
title = componentOpts.Component
}

source := componentOpts.CatalogSource
// Ensure there are controls provided
if len(componentOpts.Requirements) == 0 {
message.Fatalf(fmt.Errorf("comma-delimited list of control-ids required"), "comma-delimited list of control-ids required")
}

// Used to reproduce the command for documentation
command := fmt.Sprintf("%s --catalog-source %s --component '%s' --requirements %s --remarks %s", cmd.CommandPath(), source, title, strings.Join(componentOpts.Requirements, ","), strings.Join(remarks, ","))

// Fetch the catalog source
data, err := network.Fetch(source)
Expand All @@ -99,6 +109,10 @@ var generateComponentCmd = &cobra.Command{
message.Fatalf(fmt.Errorf("error creating component"), "error creating component")
}

// Add the command to the remarks - this will always overwrite the existing remarks content
components := *comp.Components
components[0].Remarks = fmt.Sprintf("This component was generated using the following command:\n\t %s", command)

var model = oscalTypes_1_1_2.OscalModels{
ComponentDefinition: comp,
}
Expand Down