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

Add InitializeCommand hook #2397

Merged
merged 1 commit into from
Mar 13, 2024
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
2 changes: 2 additions & 0 deletions cli/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ cilium connectivity test`,
cmd.SetOut(os.Stdout)
cmd.SetErr(os.Stderr)

hooks.InitializeCommand(cmd)
return cmd
}

Expand All @@ -114,3 +115,4 @@ func (*NopHooks) AddConnectivityTestFlags(*pflag.FlagSet)
func (*NopHooks) AddConnectivityTests(*check.ConnectivityTest) error { return nil }
func (*NopHooks) DetectFeatures(context.Context, *check.ConnectivityTest) error { return nil }
func (*NopHooks) SetupAndValidate(context.Context, *check.ConnectivityTest) error { return nil }
func (*NopHooks) InitializeCommand(*cobra.Command) {}
26 changes: 26 additions & 0 deletions cli/cmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium

package cli

import (
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

const usage = "override usage"

type testHooks struct {
NopHooks
}

func (th *testHooks) InitializeCommand(rootCmd *cobra.Command) {
rootCmd.Use = usage
}

func TestInitializeCommandHook(t *testing.T) {
cmd := NewCiliumCommand(&testHooks{})
assert.Equal(t, usage, cmd.Use)
}
4 changes: 4 additions & 0 deletions cli/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package cli

import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/cilium/cilium-cli/connectivity"
Expand All @@ -14,6 +15,9 @@ import (
type Hooks interface {
ConnectivityTestHooks
sysdump.Hooks
// InitializeCommand gets called with the root command before returning it
// from cli.NewCiliumCommand.
InitializeCommand(rootCmd *cobra.Command)
}

// ConnectivityTestHooks to extend cilium-cli with additional connectivity tests and related flags.
Expand Down
Loading