Skip to content

Commit 92ec558

Browse files
Copilotfrjcomp
andauthored
Skip PersistentPreRun during shell completion commands (#579)
* Fix completion bug: skip logging during shell completion commands --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: frjcomp <107982661+frjcomp@users.noreply.github.com>
1 parent d413d57 commit 92ec558

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

internal/cmd/root.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ var (
4141
Example: "pipeleek gl scan --token glpat-xxxxxxxxxxx --gitlab https://gitlab.com",
4242
Version: getVersion(),
4343
PersistentPreRun: func(cmd *cobra.Command, args []string) {
44+
if isCompletionCommand(cmd) {
45+
return
46+
}
4447
initLogger(cmd)
4548
setGlobalLogLevel(cmd)
4649
loadConfigFile(cmd)
@@ -241,6 +244,18 @@ func formatLevelWithHitColor(colorEnabled bool) zerolog.Formatter {
241244
}
242245
}
243246

247+
// isCompletionCommand returns true if the command being executed is a shell
248+
// completion command. Logging must be suppressed for these commands because
249+
// any output to stdout will break the generated completion script.
250+
func isCompletionCommand(cmd *cobra.Command) bool {
251+
for c := cmd; c != nil; c = c.Parent() {
252+
if c.Name() == "completion" || c.Name() == cobra.ShellCompRequestCmd {
253+
return true
254+
}
255+
}
256+
return false
257+
}
258+
244259
func setGlobalLogLevel(cmd *cobra.Command) {
245260
if LogLevel != "" {
246261
switch LogLevel {

internal/cmd/root_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/rs/zerolog"
1010
"github.com/rs/zerolog/log"
11+
"github.com/spf13/cobra"
1112
"github.com/stretchr/testify/assert"
1213
"github.com/stretchr/testify/require"
1314
)
@@ -417,3 +418,80 @@ func TestLoadConfigFile_NoConfigFile(t *testing.T) {
417418
loadConfigFile(rootCmd)
418419
})
419420
}
421+
422+
func TestIsCompletionCommand(t *testing.T) {
423+
tests := []struct {
424+
name string
425+
buildCmd func() *cobra.Command
426+
expected bool
427+
}{
428+
{
429+
name: "completion bash subcommand",
430+
buildCmd: func() *cobra.Command {
431+
root := &cobra.Command{Use: "pipeleek"}
432+
completion := &cobra.Command{Use: "completion"}
433+
bashCmd := &cobra.Command{Use: "bash"}
434+
completion.AddCommand(bashCmd)
435+
root.AddCommand(completion)
436+
return bashCmd
437+
},
438+
expected: true,
439+
},
440+
{
441+
name: "completion command itself",
442+
buildCmd: func() *cobra.Command {
443+
root := &cobra.Command{Use: "pipeleek"}
444+
completion := &cobra.Command{Use: "completion"}
445+
root.AddCommand(completion)
446+
return completion
447+
},
448+
expected: true,
449+
},
450+
{
451+
name: "__complete hidden command",
452+
buildCmd: func() *cobra.Command {
453+
root := &cobra.Command{Use: "pipeleek"}
454+
complete := &cobra.Command{Use: cobra.ShellCompRequestCmd}
455+
root.AddCommand(complete)
456+
return complete
457+
},
458+
expected: true,
459+
},
460+
{
461+
name: "regular command",
462+
buildCmd: func() *cobra.Command {
463+
root := &cobra.Command{Use: "pipeleek"}
464+
scan := &cobra.Command{Use: "scan"}
465+
root.AddCommand(scan)
466+
return scan
467+
},
468+
expected: false,
469+
},
470+
{
471+
name: "root command",
472+
buildCmd: func() *cobra.Command {
473+
return &cobra.Command{Use: "pipeleek"}
474+
},
475+
expected: false,
476+
},
477+
{
478+
name: "nested subcommand not under completion",
479+
buildCmd: func() *cobra.Command {
480+
root := &cobra.Command{Use: "pipeleek"}
481+
gl := &cobra.Command{Use: "gl"}
482+
scan := &cobra.Command{Use: "scan"}
483+
gl.AddCommand(scan)
484+
root.AddCommand(gl)
485+
return scan
486+
},
487+
expected: false,
488+
},
489+
}
490+
491+
for _, tt := range tests {
492+
t.Run(tt.name, func(t *testing.T) {
493+
cmd := tt.buildCmd()
494+
assert.Equal(t, tt.expected, isCompletionCommand(cmd))
495+
})
496+
}
497+
}

0 commit comments

Comments
 (0)