Skip to content

Commit

Permalink
feat: add custom min/max err messages
Browse files Browse the repository at this point in the history
Signed-off-by: Marko Kungla <[email protected]>
  • Loading branch information
mkungla committed Sep 1, 2024
1 parent e4830d4 commit cb7d79a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 32 deletions.
62 changes: 33 additions & 29 deletions sdk/cli/command/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,22 +229,12 @@ func (c *Cmd) ExecBefore(sess *session.Context) (err error) {
c.mu.Lock()
defer c.mu.Unlock()

args := action.NewArgs(c.flags)
argnmin := c.cnf.Get("min_args").Value().Uint()
argnmax := c.cnf.Get("max_args").Value().Uint()
name := c.cnf.Get("name").String()

if argnmin == 0 && argnmax == 0 && args.Argn() > 0 {
return fmt.Errorf("%w: %s does not accept arguments", Error, name)
}

if args.Argn() < argnmin {
return fmt.Errorf("%w: %s: requires min %d arguments, %d provided", Error, name, argnmin, args.Argn())
args, err := c.getArgs()
if err != nil {
return err
}

if args.Argn() > argnmax {
return fmt.Errorf("%w: %s: accepts max %d arguments, %d provided, extra %v", Error, name, argnmax, args.Argn(), args.Args()[argnmax:args.Argn()])
}
name := c.cnf.Get("name").String()

if c.parent != nil && !c.sharedCalled && !c.cnf.Get("skip_shared_before").Value().Bool() {
if err := c.parent.callSharedBeforeAction(sess); err != nil {
Expand Down Expand Up @@ -273,21 +263,9 @@ func (c *Cmd) ExecDo(sess *session.Context) (err error) {
return nil
}

args := action.NewArgs(c.flags)
argnmin := c.cnf.Get("min_args").Value().Uint()
argnmax := c.cnf.Get("max_args").Value().Uint()
name := c.cnf.Get("name").String()

if argnmin == 0 && argnmax == 0 && args.Argn() > 0 {
return fmt.Errorf("%w: %s does not accept arguments", Error, name)
}

if args.Argn() < argnmin {
return fmt.Errorf("%w: %s: requires min %d arguments, %d provided", Error, name, argnmin, args.Argn())
}

if args.Argn() > argnmax {
return fmt.Errorf("%w: %s: accepts max %d arguments, %d provided, extra %v", Error, name, argnmax, args.Argn(), args.Args()[argnmax:args.Argn()])
args, err := c.getArgs()
if err != nil {
return err
}

if err := c.doAction(sess, args); err != nil {
Expand Down Expand Up @@ -375,3 +353,29 @@ func (c *Cmd) SkipSharedBeforeAction() bool {
func (c *Cmd) HasBefore() bool {
return c.beforeAction != nil
}

func (c *Cmd) getArgs() (action.Args, error) {
args := action.NewArgs(c.flags)
argnmin := c.cnf.Get("min_args").Value().Uint()
argnmax := c.cnf.Get("max_args").Value().Uint()
name := c.cnf.Get("name").String()

if argnmin == 0 && argnmax == 0 && args.Argn() > 0 {
return args, fmt.Errorf("%w: %s does not accept arguments", Error, name)
}

if args.Argn() < argnmin {
if err := c.cnf.Get("min_args_err").Value(); !err.Empty() {
return args, errors.New(err.String())
}
return args, fmt.Errorf("%w: %s: requires min %d arguments, %d provided", Error, name, argnmin, args.Argn())
}
if args.Argn() > argnmax {
if err := c.cnf.Get("max_args_err").Value(); !err.Empty() {
return args, errors.New(err.String())
}
return args, fmt.Errorf("%w: %s: accepts max %d arguments, %d provided, extra %v", Error, name, argnmax, args.Argn(), args.Args()[argnmax:args.Argn()])
}

return args, nil
}
6 changes: 4 additions & 2 deletions sdk/cli/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ type Config struct {
Category settings.String `key:"category"`
Description settings.String `key:"description"`
// MinArgs Minimum argument count for command
MinArgs settings.Uint `key:"min_args" default:"0" mutation:"once"`
MinArgs settings.Uint `key:"min_args" default:"0" mutation:"once"`
MinArgsErr settings.String
// MaxArgs Maximum argument count for command
MaxArgs settings.Uint `key:"max_args" default:"0" mutation:"once"`
MaxArgs settings.Uint `key:"max_args" default:"0" mutation:"once"`
MaxArgsErr settings.String
// SharedBeforeAction share Before action for all its subcommands
SharedBeforeAction settings.Bool `key:"shared_before_action" default:"false"`
// Indicates that the command should be executed immediately, without waiting for the full runtime setup.
Expand Down
2 changes: 1 addition & 1 deletion sdk/datetime/datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

type Settings struct {
Location settings.String `key:"location,config" default:"Local" desc:"The location to use for time operations."`
Location settings.String `key:"location,config" default:"Local" mutation:"once" desc:"The location to use for time operations."`
}

func (s Settings) Blueprint() (*settings.Blueprint, error) {
Expand Down

0 comments on commit cb7d79a

Please sign in to comment.