Skip to content

Follow improve code quality #21465

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

Merged
merged 5 commits into from
Oct 15, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions modules/git/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Command struct {
parentContext context.Context
desc string
globalArgsLength int
broken bool
}

func (c *Command) String() string {
Expand Down Expand Up @@ -89,18 +90,19 @@ func (c *Command) SetDescription(desc string) *Command {
return c
}

// AddArguments adds new argument(s) to the command.
// AddArguments adds new argument(s) to the command. Each argument must be safe to be trusted.
func (c *Command) AddArguments(args ...string) *Command {
c.args = append(c.args, args...)
return c
}

// AddDynamicArguments adds new dynamic argument(s) to the command.
// If the argument is invalid (it shouldn't happen in real life), it panics to caller
// The arguments may come from user input and can not be trusted, so no leading '-' is allowed to avoid passing options
func (c *Command) AddDynamicArguments(args ...string) *Command {
for _, arg := range args {
if arg != "" && arg[0] == '-' {
panic("invalid argument: " + arg)
c.broken = true
return c
}
}
c.args = append(c.args, args...)
Expand Down Expand Up @@ -150,8 +152,14 @@ func CommonCmdServEnvs() []string {
return commonBaseEnvs()
}

var ErrBrokenCommand = errors.New("git command is command")

// Run runs the command with the RunOpts
func (c *Command) Run(opts *RunOpts) error {
if c.broken {
log.Error("git command is broken: %s", c.String())
return ErrBrokenCommand
}
if opts == nil {
opts = &RunOpts{}
}
Expand Down
16 changes: 7 additions & 9 deletions modules/git/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,13 @@ func TestRunWithContextStd(t *testing.T) {
assert.Empty(t, stdout)
}

assert.Panics(t, func() {
cmd = NewCommand(context.Background())
cmd.AddDynamicArguments("-test")
})

assert.Panics(t, func() {
cmd = NewCommand(context.Background())
cmd.AddDynamicArguments("--test")
})
cmd = NewCommand(context.Background())
cmd.AddDynamicArguments("-test")
assert.ErrorIs(t, cmd.Run(&RunOpts{}), ErrBrokenCommand)

cmd = NewCommand(context.Background())
cmd.AddDynamicArguments("--test")
assert.ErrorIs(t, cmd.Run(&RunOpts{}), ErrBrokenCommand)

subCmd := "version"
cmd = NewCommand(context.Background()).AddDynamicArguments(subCmd) // for test purpose only, the sub-command should never be dynamic for production
Expand Down