Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions .chloggen/cobra-cli-error-builder.yaml
Original file line number Diff line number Diff line change
@@ -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]
4 changes: 4 additions & 0 deletions .chloggen/cobra-cli-error-mdatagen.yaml
Original file line number Diff line number Diff line change
@@ -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]
5 changes: 2 additions & 3 deletions cmd/builder/internal/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 22 additions & 5 deletions cmd/builder/internal/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package internal

import (
"bytes"
"strings"
"testing"

Expand All @@ -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,
},
Expand All @@ -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
Expand Down
7 changes: 6 additions & 1 deletion cmd/builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package main

import (
"os"

"github.com/spf13/cobra"

"go.opentelemetry.io/collector/cmd/builder/internal"
Expand All @@ -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)
}
}
18 changes: 18 additions & 0 deletions cmd/mdatagen/internal/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"go/token"
"os"
"path/filepath"
"strings"
"testing"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion cmd/mdatagen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package main
//go:generate mdatagen metadata.yaml

import (
"os"

"github.com/spf13/cobra"

"go.opentelemetry.io/collector/cmd/mdatagen/internal"
Expand All @@ -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)
}
}
Loading