-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
feat(aws)!: Remove aws subcommand #6995
Conversation
Are you going to just remove the subcommand? Don't we show a hint to existing users somehow? (e.g. aquasecurity/trivy-plugin-index#8 (comment)) |
Added! |
Hey, sorry for reviving this old PR. I noticed that, with the removal of the subcommand and the flags, if/when the As a workaround, we've added the flags back in and have kept the However, I wanted to bring this up as it's an issue in its current state. Do we have any objections to adding back the |
hi @k-le - good point! I just did a quick search and didn't find any other usages of the |
Can do! |
@simar7 I'm taking a look at adding Here's an example of the approach that I'm taking: Reintroduce the // trivy-aws/pkg/flag/cloud_flags.go
package flag
import (
trivyFlag "github.com/aquasecurity/trivy/pkg/flag"
"golang.org/x/xerrors"
"time"
)
var (
cloudUpdateCacheFlag = trivyFlag.Flag[bool]{
Name: "update-cache",
ConfigName: "cloud.update-cache",
Usage: "Update the cache for the applicable cloud provider instead of using cached results.",
}
cloudMaxCacheAgeFlag = trivyFlag.Flag[time.Duration]{
Name: "max-cache-age",
ConfigName: "cloud.max-cache-age",
Default: time.Hour * 24,
Usage: "The maximum age of the cloud cache. Cached data will be required from the cloud provider if it is older than this.",
}
)
type CloudFlagGroup struct {
UpdateCache *trivyFlag.Flag[bool]
MaxCacheAge *trivyFlag.Flag[time.Duration]
}
type CloudOptions struct {
MaxCacheAge time.Duration
UpdateCache bool
}
func NewCloudFlagGroup() *CloudFlagGroup {
return &CloudFlagGroup{
UpdateCache: cloudUpdateCacheFlag.Clone(),
MaxCacheAge: cloudMaxCacheAgeFlag.Clone(),
}
}
func (f *CloudFlagGroup) Name() string {
return "Cloud"
}
func (f *CloudFlagGroup) Flags() []trivyFlag.Flagger {
return []trivyFlag.Flagger{
f.UpdateCache,
f.MaxCacheAge,
}
}
func (f *CloudFlagGroup) ToOptions() (CloudOptions, error) {
if err := parseFlags(f); err != nil {
return CloudOptions{}, err
}
return CloudOptions{
UpdateCache: f.UpdateCache.Value(),
MaxCacheAge: f.MaxCacheAge.Value(),
}, nil
}
func parseFlags(fg trivyFlag.FlagGroup) error {
for _, flag := range fg.Flags() {
if err := flag.Parse(); err != nil {
return xerrors.Errorf("unable to parse flag: %w", err)
}
}
return nil
} Create new // trivy-aws/pkg/flag/options.go
package flag
import (
trivyFlag "github.com/aquasecurity/trivy/pkg/flag"
"github.com/spf13/cobra"
"golang.org/x/xerrors"
)
type Flags struct {
BaseFlags trivyFlag.Flags
CloudFlagGroup *CloudFlagGroup
}
type Options struct {
BaseOptions trivyFlag.Options
CloudOptions
}
func (f *Flags) Bind(cmd *cobra.Command) error {
err := f.BaseFlags.Bind(cmd)
if err != nil {
return xerrors.Errorf("%w", err)
}
return nil
}
func (f *Flags) ToOptions(args []string) (Options, error) {
baseOptions, err := f.BaseFlags.ToOptions(args)
if err != nil {
return Options{}, xerrors.Errorf("%w", err)
}
opts := Options{
BaseOptions: baseOptions,
}
if f.CloudFlagGroup != nil {
opts.CloudOptions, err = f.CloudFlagGroup.ToOptions()
if err != nil {
return Options{}, xerrors.Errorf("cloud flag error: %w", err)
}
}
return opts, nil
} Now, this approach leads to a handful of changes where, in package commands
import (
"github.com/aquasecurity/trivy-aws/pkg/flag"
trivyFlag "github.com/aquasecurity/trivy/pkg/flag"
)
func example() {
globalFlags := trivyFlag.NewGlobalFlagGroup()
awsFlags := &flag.Flags{
BaseFlags: trivyFlag.Flags{
GlobalFlagGroup: globalFlags,
},
CloudFlagGroup: flag.NewCloudFlagGroup(),
}
opts, err := awsFlags.ToOptions(args)
if err != nil { ... }
if opts.BaseOptions.Timeout < time.Hour {
// some logic...
}
} I agree with you that, conceptually, since I'm open to other ideas and suggestions, please let me know! I'd love to learn. |
hi @k-le - to me that your approach looks reasonable, what was your concern? In essence, all plugins are free to do extend their functionality the way it's appropriate for them and in this case it sounds like adding a few new options within the plugin itself should be reasonable. @nikpivkin do you have any ideas on how to approach this besides what @k-le already mentioned? |
Description
See discussion: #6818
Checklist