mirror of
https://github.com/aquasecurity/trivy.git
synced 2025-12-05 20:40:16 -08:00
fix(cli): show help when no argument is passed (#628)
* Fix subcommands help * refactor: call ShowAppHelpAndExit * refactor: remove an unused error * test: remove exit cases Co-authored-by: knqyf263 <knqyf263@gmail.com>
This commit is contained in:
@@ -57,8 +57,7 @@ func (c *Config) Init(image bool) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := c.ArtifactConfig.Init(c.Context.Args(), c.Logger); err != nil {
|
||||
cli.ShowAppHelp(c.Context)
|
||||
if err := c.ArtifactConfig.Init(c.Context, c.Logger); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -184,10 +184,6 @@ func TestConfig_Init(t *testing.T) {
|
||||
},
|
||||
wantErr: "arguments error",
|
||||
},
|
||||
{
|
||||
name: "sad: no image name",
|
||||
wantErr: "no target is specified",
|
||||
},
|
||||
{
|
||||
name: "sad: invalid image name",
|
||||
args: []string{`!"#$%&'()`},
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
|
||||
"github.com/aquasecurity/fanal/cache"
|
||||
"github.com/aquasecurity/trivy/internal/artifact/config"
|
||||
artifact "github.com/aquasecurity/trivy/internal/config"
|
||||
"github.com/aquasecurity/trivy/pkg/scanner"
|
||||
)
|
||||
|
||||
@@ -38,10 +37,7 @@ func ImageRun(cliCtx *cli.Context) error {
|
||||
}
|
||||
|
||||
// initialize config
|
||||
err = c.Init(true)
|
||||
if xerrors.Is(err, artifact.ErrNoTarget) {
|
||||
return nil
|
||||
} else if err != nil {
|
||||
if err := c.Init(true); err != nil {
|
||||
return xerrors.Errorf("failed to initialize options: %w", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -60,12 +60,11 @@ func (c *Config) Init() (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := c.ArtifactConfig.Init(c.Context.Args(), c.Logger); err != nil {
|
||||
if err := c.ArtifactConfig.Init(c.Context, c.Logger); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := c.ImageConfig.Init(c.Context.Args(), c.Logger); err != nil {
|
||||
cli.ShowAppHelp(c.Context)
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -224,10 +224,6 @@ func TestConfig_Init(t *testing.T) {
|
||||
},
|
||||
wantErr: "arguments error",
|
||||
},
|
||||
{
|
||||
name: "sad: no image name",
|
||||
wantErr: "no target is specified",
|
||||
},
|
||||
{
|
||||
name: "sad: invalid image name",
|
||||
args: []string{`!"#$%&'()`},
|
||||
|
||||
@@ -33,19 +33,17 @@ func NewArtifactConfig(c *cli.Context) ArtifactConfig {
|
||||
}
|
||||
}
|
||||
|
||||
var ErrNoTarget = xerrors.New("no target is specified")
|
||||
|
||||
func (c *ArtifactConfig) Init(args cli.Args, logger *zap.SugaredLogger) (err error) {
|
||||
if c.Input == "" && args.Len() == 0 {
|
||||
func (c *ArtifactConfig) Init(ctx *cli.Context, logger *zap.SugaredLogger) (err error) {
|
||||
if c.Input == "" && ctx.Args().Len() == 0 {
|
||||
logger.Debug(`trivy requires at least 1 argument or --input option`)
|
||||
return ErrNoTarget
|
||||
} else if args.Len() > 1 {
|
||||
cli.ShowAppHelpAndExit(ctx, 0)
|
||||
} else if ctx.Args().Len() > 1 {
|
||||
logger.Error(`multiple targets cannot be specified`)
|
||||
return xerrors.New("arguments error")
|
||||
}
|
||||
|
||||
if c.Input == "" {
|
||||
c.Target = args.First()
|
||||
c.Target = ctx.Args().First()
|
||||
}
|
||||
|
||||
if c.skipDirectories != "" {
|
||||
|
||||
@@ -35,13 +35,6 @@ func TestArtifactConfig_Init(t *testing.T) {
|
||||
},
|
||||
wantErr: "arguments error",
|
||||
},
|
||||
{
|
||||
name: "sad: no image name",
|
||||
logs: []string{
|
||||
"trivy requires at least 1 argument or --input option",
|
||||
},
|
||||
wantErr: "no target is specified",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
@@ -55,7 +48,7 @@ func TestArtifactConfig_Init(t *testing.T) {
|
||||
|
||||
c := config.NewArtifactConfig(ctx)
|
||||
|
||||
err := c.Init(ctx.Args(), logger.Sugar())
|
||||
err := c.Init(ctx, logger.Sugar())
|
||||
|
||||
// tests log messages
|
||||
var gotMessages []string
|
||||
|
||||
Reference in New Issue
Block a user