Skip to content

Commit

Permalink
Move the CLI version string to the default package
Browse files Browse the repository at this point in the history
Move the CLI version string from internal/cli/cmd to default/ so that it
can be exported from outside the cilium-cli repo.

Ref: cilium/design-cfps#9

Signed-off-by: Michi Mutsuzaki <[email protected]>
  • Loading branch information
michi-covalent committed Mar 5, 2024
1 parent 4558846 commit b922331
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ GOLANGCILINT_VERSION = $(shell golangci-lint version --format short 2>/dev/null)
$(TARGET):
$(GO_BUILD) $(if $(GO_TAGS),-tags $(GO_TAGS)) \
-ldflags "$(STRIP_DEBUG) \
-X 'github.com/cilium/cilium-cli/cli.Version=${VERSION}'" \
-X 'github.com/cilium/cilium-cli/defaults.CLIVersion=${VERSION}'" \
-o $(TARGET) \
./cmd/cilium

Expand Down
7 changes: 0 additions & 7 deletions cli/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,13 @@ import (
"github.com/cilium/cilium-cli/sysdump"
)

// The following variables are set at compile time via LDFLAGS.
var (
// Version is the software version.
Version string
)

// NewDefaultCiliumCommand returns a new "cilium" cli cobra command without any additional hooks.
func NewDefaultCiliumCommand() *cobra.Command {
return NewCiliumCommand(&NopHooks{})
}

// NewCiliumCommand returns a new "cilium" cli cobra command registering all the additional input hooks.
func NewCiliumCommand(hooks Hooks) *cobra.Command {
cmd.SetVersion(Version)
return cmd.NewCiliumCommand(hooks)
}

Expand Down
5 changes: 5 additions & 0 deletions defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,9 @@ var (
"inbound_other", // XfrmInError
"inbound_state_invalid", // XfrmInStateInvalid
}

// The following variables are set at compile time via LDFLAGS.

// CLIVersion is the software version of the Cilium CLI.
CLIVersion string
)
8 changes: 0 additions & 8 deletions internal/cli/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,8 @@ var (
namespace string

k8sClient *k8s.Client

// version is the version string of the cilium-cli itself
version string
)

// SetVersion sets the version string for the cilium command
func SetVersion(v string) {
version = v
}

func NewCiliumCommand(hooks Hooks) *cobra.Command {
cmd := &cobra.Command{
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/cmd/connectivity.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func RunE(hooks Hooks) func(cmd *cobra.Command, args []string) error {
}

// Instantiate the test harness.
cc, err := check.NewConnectivityTest(k8sClient, params, version)
cc, err := check.NewConnectivityTest(k8sClient, params, defaults.CLIVersion)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func newCmdUninstallWithHelm() *cobra.Command {
TestNamespace: params.TestNamespace,
FlowValidation: check.FlowValidationModeDisabled,
Writer: os.Stdout,
}, version)
}, defaults.CLIVersion)
if err != nil {
fmt.Printf("⚠ ️ Failed to initialize connectivity test uninstaller: %s\n", err)
} else {
Expand Down
3 changes: 2 additions & 1 deletion internal/cli/cmd/sysdump.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/spf13/cobra"
"k8s.io/klog/v2"

"github.com/cilium/cilium-cli/defaults"
"github.com/cilium/cilium-cli/sysdump"
)

Expand All @@ -36,7 +37,7 @@ func newCmdSysdump(hooks sysdump.Hooks) *cobra.Command {
// Silence klog to avoid displaying "throttling" messages - those are expected.
klog.SetOutput(io.Discard)
// Collect the sysdump.
collector, err := sysdump.NewCollector(k8sClient, sysdumpOptions, time.Now(), version)
collector, err := sysdump.NewCollector(k8sClient, sysdumpOptions, time.Now(), defaults.CLIVersion)
if err != nil {
return fmt.Errorf("failed to create sysdump collector: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func newCmdVersion() *cobra.Command {
Short: "Display detailed version information",
Long: `Displays information about the version of this software.`,
RunE: func(_ *cobra.Command, _ []string) error {
fmt.Printf("cilium-cli: %s compiled with %v on %v/%v\n", version, runtime.Version(), runtime.GOOS, runtime.GOARCH)
fmt.Printf("cilium-cli: %s compiled with %v on %v/%v\n", defaults.CLIVersion, runtime.Version(), runtime.GOOS, runtime.GOARCH)
fmt.Printf("cilium image (default): %s\n", defaults.Version)
fmt.Printf("cilium image (stable): %s\n", getLatestStableVersion())
if clientOnly {
Expand Down

0 comments on commit b922331

Please sign in to comment.