diff --git a/cmd/builder/internal/command.go b/cmd/builder/internal/command.go index b6cd83a1102..124b51fdc91 100644 --- a/cmd/builder/internal/command.go +++ b/cmd/builder/internal/command.go @@ -116,7 +116,7 @@ configuration is provided, ocb will generate a default Collector. return nil, err } // version of this binary - cmd.AddCommand(versionCommand(binVersion)) + cmd.AddCommand(versionCommand()) return cmd, nil } diff --git a/cmd/builder/internal/version.go b/cmd/builder/internal/version.go index 886a0483d8f..9aa5e1511b7 100644 --- a/cmd/builder/internal/version.go +++ b/cmd/builder/internal/version.go @@ -14,34 +14,20 @@ var ( version = "" ) -type debugReadBuildInfoFunc func() (info *debug.BuildInfo, ok bool) -type binVersionFunc func(fn debugReadBuildInfoFunc) (string, error) - -// binVersion returns the version of the binary. -// If the version is not set, it attempts to read the build information. -// Returns an error if the build information cannot be read. -func binVersion(fn debugReadBuildInfoFunc) (string, error) { - if version != "" { - return version, nil - } - info, ok := fn() - if !ok { - return "", fmt.Errorf("failed to read build info") +func init() { + // the second returned value is a boolean, which is true if the binaries are built with module support. + if version == "" { + info, _ := debug.ReadBuildInfo() + version = info.Main.Version } - return info.Main.Version, nil } -func versionCommand(fn binVersionFunc) *cobra.Command { - var err error +func versionCommand() *cobra.Command { return &cobra.Command{ Use: "version", Short: "Version of ocb", Long: "Prints the version of the ocb binary", RunE: func(cmd *cobra.Command, _ []string) error { - version, err = fn(debug.ReadBuildInfo) - if err != nil { - return err - } cmd.Println(fmt.Sprintf("%s version %s", cmd.Parent().Name(), version)) return nil }, diff --git a/cmd/builder/internal/version_test.go b/cmd/builder/internal/version_test.go deleted file mode 100644 index 1e352baea97..00000000000 --- a/cmd/builder/internal/version_test.go +++ /dev/null @@ -1,123 +0,0 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -package internal - -import ( - "bytes" - "context" - "fmt" - "os" - "runtime/debug" - "testing" - - "github.com/spf13/cobra" -) - -// Mock debug.ReadBuildInfo function -var readBuildInfo = debug.ReadBuildInfo - -func TestBinVersion(t *testing.T) { - // Test case: version is set - version = "v1.0.0" - v, err := binVersion(readBuildInfo) - if err != nil { - t.Fatalf("expected no error, got %v", err) - } - if v != "v1.0.0" { - t.Fatalf("expected version 'v1.0.0', got %v", v) - } - - // // Test case: version is not set, ReadBuildInfo returns valid info - version = "" - readBuildInfo = func() (*debug.BuildInfo, bool) { - return &debug.BuildInfo{ - Main: debug.Module{ - Version: "v2.0.0", - }, - }, true - } - v, err = binVersion(readBuildInfo) - fmt.Printf("v: %v, err: %v", v, err) - if err != nil { - t.Fatalf("expected no error, got %v", err) - } - if v != "v2.0.0" { - t.Fatalf("expected version 'v2.0.0', got %v", v) - } - - // Test case: version is not set, ReadBuildInfo fails - readBuildInfo = func() (*debug.BuildInfo, bool) { - return nil, false - } - v, err = binVersion(readBuildInfo) - if err == nil { - t.Fatalf("expected error, got nil") - } - if v != "" { - t.Fatalf("expected empty version, got %v", v) - } -} - -var validBinVersionFunc binVersionFunc = func(_ debugReadBuildInfoFunc) (string, error) { - return "v1.0.0", nil -} - -var invalidBinVersionFunc binVersionFunc = func(_ debugReadBuildInfoFunc) (string, error) { - return "", fmt.Errorf("failed to read build info") -} - -func TestVersionCommand(t *testing.T) { - tests := []struct { - name string - binVersion binVersionFunc - expectedOutput string - expectedError bool - }{ - { - name: "valid version", - binVersion: validBinVersionFunc, - expectedOutput: "ocb version v1.0.0\n", - expectedError: false, - }, - { - name: "error in binVersion", - binVersion: invalidBinVersionFunc, - expectedOutput: "", - expectedError: true, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - // Set a mock parent command name - parentCmd := &cobra.Command{ - Use: "ocb ", - } - // Create the command - var cmd = versionCommand(tt.binVersion) - parentCmd.AddCommand(cmd) - // Capture the output - output := bytes.NewBufferString("") - errOutput := bytes.NewBufferString("") - cmd.SetOut(output) - cmd.SetErr(errOutput) - // Create a new context with a fake value - type contextKey string - ctx := context.WithValue(context.Background(), contextKey("key"), "value") - // Set fake CLI arguments - fakeArgs := []string{"cmd", "version"} - os.Args = fakeArgs - // Execute the command - err := cmd.ExecuteContext(ctx) - // Check for expected error - if (err != nil) != tt.expectedError { - t.Fatalf("expected error: %v, got: %v", tt.expectedError, err) - } - // Check for expected output - if output.String() != tt.expectedOutput { - t.Fatalf("expected output: %v, got: %v", tt.expectedOutput, output.String()) - } - }) - } -}