Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: Export Version var to be set externally #1646

Merged
merged 1 commit into from
May 24, 2023
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: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ GOLANGCILINT_VERSION = $(shell golangci-lint version 2>/dev/null)
$(TARGET):
$(GO_BUILD) $(if $(GO_TAGS),-tags $(GO_TAGS)) \
-ldflags "-w -s \
-X 'github.com/cilium/cilium-cli/internal/cli/cmd.Version=${VERSION}'" \
-X 'github.com/cilium/cilium-cli/cli.Version=${VERSION}'" \
-o $(TARGET) \
./cmd/cilium

Expand Down Expand Up @@ -54,7 +54,7 @@ local-release: clean
echo Building release binary for $$OS/$$ARCH...; \
test -d release/$$OS/$$ARCH|| mkdir -p release/$$OS/$$ARCH; \
env GOOS=$$OS GOARCH=$$ARCH $(GO_BUILD) $(if $(GO_TAGS),-tags $(GO_TAGS)) \
-ldflags "-w -s -X 'github.com/cilium/cilium-cli/internal/cli/cmd.Version=${VERSION}'" \
-ldflags "-w -s -X 'github.com/cilium/cilium-cli/cli.Version=${VERSION}'" \
-o release/$$OS/$$ARCH/$(TARGET)$$EXT ./cmd/cilium; \
tar -czf release/$(TARGET)-$$OS-$$ARCH.tar.gz -C release/$$OS/$$ARCH $(TARGET)$$EXT; \
(cd release && sha256sum $(TARGET)-$$OS-$$ARCH.tar.gz > $(TARGET)-$$OS-$$ARCH.tar.gz.sha256sum); \
Expand Down
7 changes: 7 additions & 0 deletions cli/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,20 @@ 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
8 changes: 8 additions & 0 deletions internal/cli/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,16 @@ 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 @@ -71,7 +71,7 @@ func newCmdConnectivityTest(hooks Hooks) *cobra.Command {
}

// Instantiate the test harness.
cc, err := check.NewConnectivityTest(k8sClient, params, Version)
cc, err := check.NewConnectivityTest(k8sClient, params, version)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/cli/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func newCmdUninstall() *cobra.Command {
TestNamespace: params.TestNamespace,
FlowValidation: check.FlowValidationModeDisabled,
Writer: os.Stdout,
}, Version)
}, version)
if err != nil {
fmt.Printf("⚠ ️ Failed to initialize connectivity test uninstaller: %s", err)
} else {
Expand Down Expand Up @@ -311,7 +311,7 @@ func newCmdUninstallWithHelm() *cobra.Command {
TestNamespace: params.TestNamespace,
FlowValidation: check.FlowValidationModeDisabled,
Writer: os.Stdout,
}, Version)
}, version)
if err != nil {
fmt.Printf("⚠ ️ Failed to initialize connectivity test uninstaller: %s", err)
} else {
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/cmd/sysdump.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func newCmdSysdump(hooks SysdumpHooks) *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(), version)
if err != nil {
return fmt.Errorf("failed to create sysdump collector: %w", err)
}
Expand Down
8 changes: 1 addition & 7 deletions internal/cli/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ import (
"github.com/cilium/cilium-cli/defaults"
)

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

func getLatestStableVersion() string {
resp, err := http.Get("https://raw.githubusercontent.com/cilium/cilium/main/stable.txt")
if err != nil {
Expand All @@ -44,7 +38,7 @@ func newCmdVersion() *cobra.Command {
Short: "Display detailed version information",
Long: `Displays information about the version of this software.`,
RunE: func(cmd *cobra.Command, args []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", version, 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