Skip to content

Commit

Permalink
Option in adv guide to print yaml advisory to console
Browse files Browse the repository at this point in the history
Signed-off-by: Josh Dolitsky <[email protected]>
  • Loading branch information
jdolitsky committed Jul 24, 2024
1 parent 9c9e271 commit 6f11524
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
9 changes: 9 additions & 0 deletions pkg/advisory/data_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,15 @@ func (ds DataSession) Push(ctx context.Context) error {
return nil
}

// ModifiedPackages returns a map of package name o=to document for all modified packages
func (ds DataSession) ModifiedPackages() map[string]configs.Entry[v2.Document] {
modified := map[string]configs.Entry[v2.Document]{}
for _, n := range slices.Compact(ds.modifiedPackages) {
modified[n] = ds.index.Select().WhereName(n).Entries()[0]
}
return modified
}

// OpenPullRequest opens a pull request for the changes made during the session.
func (ds DataSession) OpenPullRequest(ctx context.Context) (*PullRequest, error) {
slices.Sort(ds.modifiedPackages)
Expand Down
6 changes: 3 additions & 3 deletions pkg/advisory/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ func (req Request) Validate() error {
return err
}

if req.VulnerabilityID != "" {
return errors.New("vulnerability should be empty")
}
// if req.VulnerabilityID != "" {

Check failure on line 39 in pkg/advisory/request.go

View workflow job for this annotation

GitHub Actions / lint

commentedOutCode: may want to remove commented-out code (gocritic)
// return errors.New("vulnerability should be empty")
// }

if req.Event.IsZero() {
return errors.New("event cannot be zero")
Expand Down
21 changes: 20 additions & 1 deletion pkg/cli/advisory_guide.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
question2 "github.com/wolfi-dev/wolfictl/pkg/question"
"github.com/wolfi-dev/wolfictl/pkg/scan"
"github.com/wolfi-dev/wolfictl/pkg/vuln"
"gopkg.in/yaml.v3"
)

//nolint:gocyclo
Expand Down Expand Up @@ -237,7 +238,7 @@ func cmdAdvisoryGuide() *cobra.Command {
actions = append(actions, customActionBrowser)
}
if sess.Modified() {
actions = append(actions, newCustomActionPR(ctx, sess))
actions = append(actions, newCustomActionPR(ctx, sess), newCustomActionOut(ctx, sess))
}

vaPickerOpts := picker.Options[resultWithAPKs]{
Expand Down Expand Up @@ -389,6 +390,24 @@ var (
},
}
}

newCustomActionOut = func(_ context.Context, sess *advisory.DataSession) picker.CustomAction[resultWithAPKs] {
return picker.CustomAction[resultWithAPKs]{
Key: "o",
Description: "print changes to stdout",
Do: func(_ resultWithAPKs) tea.Cmd {
o := []string{}
for name, doc := range sess.ModifiedPackages() {
b, err := yaml.Marshal(doc.Configuration())
if err != nil {
return picker.ErrCmd(fmt.Errorf("marshalling yaml for %s: %w", name, err))
}
o = append(o, fmt.Sprintf("# %s.advisories.yaml\n%s\n\n", name, string(b)))
}
return tea.Printf(strings.Join(o, "\n"))
},
}
}
)

type advisoryGuideParams struct {
Expand Down

0 comments on commit 6f11524

Please sign in to comment.