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
28 changes: 28 additions & 0 deletions cmd/wfctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,27 @@ func main() {
updateNoticeDone = checkForUpdateNotice()
}

if _, isStatic := commands[cmd]; !isStatic {
registry, err := BuildCLIRegistry(defaultPluginCommandDir())
if err != nil {
fmt.Fprintf(os.Stderr, "error: load plugin CLI commands: %v\n", err) //nolint:gosec // G705
os.Exit(1)
Comment on lines +189 to +193
}
if entry := registry.LookupCLICommand(cmd); entry != nil {
if err := DispatchCLICommand(entry, os.Args[1:]); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err) //nolint:gosec // G705
Comment on lines +190 to +197
os.Exit(1)
}
if updateNoticeDone != nil {
select {
case <-updateNoticeDone:
case <-time.After(time.Second):
}
}
return
}
}

// Set up a context that is cancelled on SIGINT/SIGTERM so that long-running
// commands (e.g. wfctl mcp, wfctl run) can be interrupted cleanly.
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
Expand Down Expand Up @@ -222,3 +243,10 @@ func main() {
}
}
}

func defaultPluginCommandDir() string {
if pluginDir := strings.TrimSpace(os.Getenv("WFCTL_PLUGIN_DIR")); pluginDir != "" {
return pluginDir
}
return defaultDataDir
}
5 changes: 4 additions & 1 deletion cmd/wfctl/plugin_cli_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ var reservedCLICommands = map[string]struct{}{

// isReservedCLICommand reports whether name is a reserved static wfctl command.
func isReservedCLICommand(name string) bool {
_, ok := reservedCLICommands[name]
if _, ok := reservedCLICommands[name]; ok {
return true
}
_, ok := commands[name]
return ok
}

Expand Down
14 changes: 11 additions & 3 deletions cmd/wfctl/plugin_cli_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func writeCLIPlugin(t *testing.T, pluginsDir, name string, commands []string) {
func TestPluginCLIRegistry_TwoPluginsTwoCommands(t *testing.T) {
dir := t.TempDir()
writeCLIPlugin(t, dir, "supply-chain", []string{"supply-chain"})
writeCLIPlugin(t, dir, "migrate", []string{"migrate"})
writeCLIPlugin(t, dir, "data-migrate", []string{"data-migrate"})

reg, err := BuildCLIRegistry(dir)
if err != nil {
Expand All @@ -39,8 +39,8 @@ func TestPluginCLIRegistry_TwoPluginsTwoCommands(t *testing.T) {
if _, ok := reg["supply-chain"]; !ok {
t.Error("supply-chain command should be registered")
}
if _, ok := reg["migrate"]; !ok {
t.Error("migrate command should be registered")
if _, ok := reg["data-migrate"]; !ok {
t.Error("data-migrate command should be registered")
}
}

Expand Down Expand Up @@ -88,3 +88,11 @@ func TestStaticCommandWins(t *testing.T) {
t.Error("validate should be in the reserved list")
}
}

func TestPluginCLIRegistry_AllStaticCommandsReserved(t *testing.T) {
for name := range commands {
if !isReservedCLICommand(name) {
t.Fatalf("static command %q should be reserved", name)
}
}
}
Loading