diff --git a/.chloggen/cobra-cli-error-builder.yaml b/.chloggen/cobra-cli-error-builder.yaml new file mode 100644 index 00000000000..68a70a81657 --- /dev/null +++ b/.chloggen/cobra-cli-error-builder.yaml @@ -0,0 +1,4 @@ +change_type: bug_fix +component: cmd/builder +note: Fix duplicate error output when CLI command execution fails in the builder tool. +issues: [14436] diff --git a/.chloggen/cobra-cli-error-mdatagen.yaml b/.chloggen/cobra-cli-error-mdatagen.yaml new file mode 100644 index 00000000000..9b54a597005 --- /dev/null +++ b/.chloggen/cobra-cli-error-mdatagen.yaml @@ -0,0 +1,4 @@ +change_type: bug_fix +component: cmd/mdatagen +note: Fix duplicate error output when CLI command execution fails in the mdatagen tool. +issues: [14436] diff --git a/cmd/builder/internal/command.go b/cmd/builder/internal/command.go index 520921229fb..b294924c937 100644 --- a/cmd/builder/internal/command.go +++ b/cmd/builder/internal/command.go @@ -34,9 +34,8 @@ const ( // Command is the main entrypoint for this application func Command() (*cobra.Command, error) { cmd := &cobra.Command{ - SilenceUsage: true, // Don't print usage on Run error. - SilenceErrors: true, // Don't print errors; main does it. - Use: "ocb", + SilenceUsage: true, // Don't print usage on Run error. + Use: "ocb", Long: fmt.Sprintf("OpenTelemetry Collector Builder (%s)", version) + ` ocb generates a custom OpenTelemetry Collector binary using the diff --git a/cmd/builder/internal/command_test.go b/cmd/builder/internal/command_test.go index ecc468c2c7d..328f76d5542 100644 --- a/cmd/builder/internal/command_test.go +++ b/cmd/builder/internal/command_test.go @@ -4,6 +4,7 @@ package internal import ( + "bytes" "strings" "testing" @@ -24,11 +25,10 @@ func TestCommand(t *testing.T) { { name: "command created", want: &cobra.Command{ - SilenceUsage: true, // Don't print usage on Run error. - SilenceErrors: true, // Don't print errors; main does it. - Use: "ocb", - Long: "OpenTelemetry Collector Builder", - Args: cobra.NoArgs, + SilenceUsage: true, // Don't print usage on Run error. + Use: "ocb", + Long: "OpenTelemetry Collector Builder", + Args: cobra.NoArgs, }, wantErr: false, }, @@ -55,6 +55,23 @@ func TestCommand(t *testing.T) { } } +func TestCommandErrorOutputOnce(t *testing.T) { + cmd, err := Command() + require.NoError(t, err) + + var stderr bytes.Buffer + cmd.SetErr(&stderr) + cmd.SetArgs([]string{"/nonexistent/path/metadata.yaml"}) + + err = cmd.Execute() + require.Error(t, err) + out := stderr.String() + require.NotEmpty(t, out) + + msg := err.Error() + assert.Equal(t, 1, strings.Count(out, msg), out) +} + func TestApplyFlags(t *testing.T) { tests := []struct { name string diff --git a/cmd/builder/main.go b/cmd/builder/main.go index c1a27854c6d..89c24a08bbf 100644 --- a/cmd/builder/main.go +++ b/cmd/builder/main.go @@ -4,6 +4,8 @@ package main import ( + "os" + "github.com/spf13/cobra" "go.opentelemetry.io/collector/cmd/builder/internal" @@ -12,5 +14,8 @@ import ( func main() { cmd, err := internal.Command() cobra.CheckErr(err) - cobra.CheckErr(cmd.Execute()) + + if err := cmd.Execute(); err != nil { + os.Exit(1) + } } diff --git a/cmd/mdatagen/internal/command_test.go b/cmd/mdatagen/internal/command_test.go index be990107242..a90f7a8ae32 100644 --- a/cmd/mdatagen/internal/command_test.go +++ b/cmd/mdatagen/internal/command_test.go @@ -10,6 +10,7 @@ import ( "go/token" "os" "path/filepath" + "strings" "testing" "github.com/spf13/cobra" @@ -39,6 +40,23 @@ func TestCommandNoArgs(t *testing.T) { require.Error(t, err) } +func TestCommandErrorOutputOnce(t *testing.T) { + cmd, err := NewCommand() + require.NoError(t, err) + + var stderr bytes.Buffer + cmd.SetErr(&stderr) + cmd.SetArgs([]string{"/nonexistent/path/metadata.yaml"}) + + err = cmd.Execute() + require.Error(t, err) + out := stderr.String() + require.NotEmpty(t, out) + + msg := err.Error() + assert.Equal(t, 1, strings.Count(out, msg), out) +} + func TestRunContents(t *testing.T) { tests := []struct { yml string diff --git a/cmd/mdatagen/main.go b/cmd/mdatagen/main.go index 32bab21949d..e3eb75b3299 100644 --- a/cmd/mdatagen/main.go +++ b/cmd/mdatagen/main.go @@ -6,6 +6,8 @@ package main //go:generate mdatagen metadata.yaml import ( + "os" + "github.com/spf13/cobra" "go.opentelemetry.io/collector/cmd/mdatagen/internal" @@ -14,5 +16,8 @@ import ( func main() { cmd, err := internal.NewCommand() cobra.CheckErr(err) - cobra.CheckErr(cmd.Execute()) + + if err := cmd.Execute(); err != nil { + os.Exit(1) + } }