diff --git a/pkg/advisory/data_session.go b/pkg/advisory/data_session.go index f1ad820d..d0bf1ad8 100644 --- a/pkg/advisory/data_session.go +++ b/pkg/advisory/data_session.go @@ -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) diff --git a/pkg/advisory/request.go b/pkg/advisory/request.go index bb3fa3b5..b2c8e13c 100644 --- a/pkg/advisory/request.go +++ b/pkg/advisory/request.go @@ -36,9 +36,9 @@ func (req Request) Validate() error { return err } - if req.VulnerabilityID != "" { - return errors.New("vulnerability should be empty") - } + // if req.VulnerabilityID != "" { + // return errors.New("vulnerability should be empty") + // } if req.Event.IsZero() { return errors.New("event cannot be zero") diff --git a/pkg/cli/advisory_guide.go b/pkg/cli/advisory_guide.go index e7ad3ec8..0aa04b16 100644 --- a/pkg/cli/advisory_guide.go +++ b/pkg/cli/advisory_guide.go @@ -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 @@ -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]{ @@ -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 {