bf9395e022
CI / license-header (push) Has been skipped
CI / e2e-dry-run (push) Has been skipped
CI / fast-gate (push) Failing after 0s
Test PR Label Logic / test-pr-labels (push) Failing after 1s
Skill Format Check / check-format (push) Failing after 2s
CI / security (push) Failing after 5s
CI / unit-test (push) Has been skipped
CI / lint (push) Has been skipped
CI / script-test (push) Has been skipped
CI / deterministic-gate (push) Has been skipped
CI / coverage (push) Has been skipped
CI / results (push) Has been cancelled
CI / deadcode (push) Has been cancelled
CI / e2e-live (push) Has been cancelled
39 lines
1.3 KiB
Go
39 lines
1.3 KiB
Go
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package cmdutil
|
|
|
|
import (
|
|
"sync/atomic"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// Cobra keeps completion callbacks in a package-global map keyed by
|
|
// *pflag.Flag with no removal path, so registrations made for a *cobra.Command
|
|
// outlive the command itself. Default to disabled (zero value = false) and let
|
|
// callers that actually serve a completion request opt in via
|
|
// SetFlagCompletionsEnabled(true).
|
|
var flagCompletionsEnabled atomic.Bool
|
|
|
|
// SetFlagCompletionsEnabled toggles whether RegisterFlagCompletion actually
|
|
// registers callbacks with cobra. Typically set once at process start.
|
|
func SetFlagCompletionsEnabled(enabled bool) {
|
|
flagCompletionsEnabled.Store(enabled)
|
|
}
|
|
|
|
// FlagCompletionsEnabled reports the current switch state.
|
|
func FlagCompletionsEnabled() bool {
|
|
return flagCompletionsEnabled.Load()
|
|
}
|
|
|
|
// RegisterFlagCompletion wraps (*cobra.Command).RegisterFlagCompletionFunc
|
|
// and honors the package switch. The underlying error is swallowed to match
|
|
// the `_ = cmd.RegisterFlagCompletionFunc(...)` style already used here.
|
|
func RegisterFlagCompletion(cmd *cobra.Command, flagName string, fn cobra.CompletionFunc) {
|
|
if !flagCompletionsEnabled.Load() {
|
|
return
|
|
}
|
|
_ = cmd.RegisterFlagCompletionFunc(flagName, fn)
|
|
}
|