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