cmdutil

package
v0.18.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 26, 2026 License: MPL-2.0 Imports: 21 Imported by: 0

Documentation

Overview

Package cmdutil provides utilities for working with cobra commands

Index

Constants

View Source
const (
	SpinnerInterval = 100 * time.Millisecond
	SpinnerColor    = "green"
)

Variables

View Source
var (
	SpinnerCharSet = spinner.CharSets[78]
	SuccessIcon    = color.GreenString("✔")
)

Functions

func DisableAuthCheck

func DisableAuthCheck(cmd *cobra.Command)

func IsAuthCheckEnabled

func IsAuthCheckEnabled(cmd *cobra.Command) bool

func ResolveWorkspaceArg added in v0.18.0

func ResolveWorkspaceArg(teams []model.Team, arg string) (*model.Team, error)

ResolveWorkspaceArg turns user-supplied "<name|id>" into a concrete team from the caller's memberships. The same resolution rules cover both `zeabur workspace switch <arg>` and the global `--workspace <arg>` flag:

  • 24-char hex (case-insensitive) → matched against team IDs in the memberships slice. **Membership is enforced** — an ID not in `teams` is rejected here, before any further backend call. Backend RBAC remains the source of truth for every subsequent operation, so this is a UX shortcut rather than a security gate.
  • non-hex → matched by team name against the membership list.
  • exactly one match → return that team.
  • zero matches → "no workspace named ..." error.
  • two or more matches → "ambiguous" error listing each candidate with the concrete `<bin> workspace switch <id>` invocation that disambiguates it. Team names are unconstrained; users must use the ID to pick.

The caller passes the membership list in (typically Factory.ListTeams) so the per-process cache is shared with the lazy startup verify and any downstream commands that surface roles.

Types

type Factory

type Factory struct {
	Log         *zap.SugaredLogger // logger
	Printer     printer.Printer    // printer
	Config      config.Config      // config(flag, env, file)
	ApiClient   api.Client         // query api
	AuthClient  auth.Client        // login, refresh token
	Prompter    prompt.Prompter    // interactive prompter
	Selector    selector.Selector  // interactive selector
	ParamFiller fill.ParamFiller   // fill params
	PersistentFlags
	// contains filtered or unexported fields
}

Factory is a factory for command runners It is used to pass common dependencies to commands. It is kind of like a "context" for commands.

func NewFactory

func NewFactory() *Factory

NewFactory returns a new cmd factory

func (*Factory) CurrentEnvironmentID added in v0.18.0

func (f *Factory) CurrentEnvironmentID() string

CurrentEnvironmentID — same rule as CurrentProjectID, applied to the persisted environment context. Reserved for future use; today no team command consumes environment context implicitly, but the helper keeps the override contract uniform across all three inner-context fields.

func (*Factory) CurrentOwnerID added in v0.18.0

func (f *Factory) CurrentOwnerID() string

CurrentOwnerID returns the team ObjectID hex that directory-level commands (project list / create / deploy-no-link) should act under. Empty string == the caller's personal account.

Resolution order:

  1. --workspace flag (resolved to a Workspace during PersistentPreRunE)
  2. Persisted workspace in the config file

Returning an empty string is the canonical "personal" signal that the project API uses to fall back to the un-owner-scoped GraphQL query.

func (*Factory) CurrentProjectID added in v0.18.0

func (f *Factory) CurrentProjectID() string

CurrentProjectID returns the persisted project context ID, but only when no --workspace override is active. Under an override the persisted context belongs to a (potentially) different workspace, so reusing it would cross scopes — instead return "" so name-based service / variable / etc. lookups fail-closed with an actionable error and the caller must pass an explicit `--id` / `--service-id`.

func (*Factory) CurrentProjectName added in v0.18.0

func (f *Factory) CurrentProjectName() string

CurrentProjectName mirrors CurrentProjectID. The personal path of the service-by-name lookup uses the project name; return "" under override so that path also refuses rather than reaches into the persisted context.

func (*Factory) CurrentServiceID added in v0.18.0

func (f *Factory) CurrentServiceID() string

CurrentServiceID — same rule as CurrentProjectID for the persisted service context. Currently unused by team-path lookups (services are looked up by project + name) but exposed so future consumers stay on the same override contract.

func (*Factory) CurrentWorkspace added in v0.18.0

func (f *Factory) CurrentWorkspace() *zcontext.Workspace

CurrentWorkspace returns the effective workspace under the same resolution rules as CurrentOwnerID, including the name and kind. Callers that want to display the active workspace (the "creating new project in team workspace X" hint in deploy) should read this rather than the persisted workspace, so a --workspace override shows up correctly.

func (*Factory) EffectiveContext added in v0.18.0

func (f *Factory) EffectiveContext() zcontext.Context

EffectiveContext returns the inner context (project / environment / service) that the current command should consume. Without an override this is the persisted config context, byte-equivalent to reading `f.Config.GetContext()` directly. Under `--workspace` override it returns an in-memory ephemeral context whose initial reads are empty and whose writes never reach the config file — so an interactive command can transiently pick a team-B project / service without polluting the persisted team-A context (PLA-1590 B++).

The ephemeral instance is cached on the Factory so that within a single command, `Set → later Get` cycles inside ParamFiller see the values they just wrote. The cache is implicitly scoped to one invocation because the Factory itself is constructed once per command.

Callers that intentionally manipulate persisted workspace state (`workspace switch`, `workspace clear`, lazy verify in root, `auth logout`) must keep going through `f.Config.GetContext()` directly — EffectiveContext is for inner context only and would silently no-op their writes.

func (*Factory) HasWorkspaceOverride added in v0.18.0

func (f *Factory) HasWorkspaceOverride() bool

HasWorkspaceOverride reports whether the caller invoked the command with a `--workspace <name|id>` flag. Use this to gate any behaviour that should only apply to "one-shot override" mode — most prominently, refusing to read or write the persisted inner context (project / environment / service), because that context belongs to whatever the persisted workspace is and would silently cross workspaces when reused under an override.

PLA-1590 contract: --workspace is a stateless override. It must not observe or modify the persisted inner context. Commands that need a project / service in override mode must take an explicit `--id` flag.

func (*Factory) ListTeams added in v0.18.0

func (f *Factory) ListTeams(ctx context.Context) ([]model.Team, error)

ListTeams returns the caller's teams via api.Client.ListTeams, memoized for the lifetime of this Factory. The same Factory is shared across every PersistentPreRunE / Run / PersistentPostRunE callback within a single CLI invocation, so one fetch covers --workspace flag resolution, the lazy startup verify, and downstream commands that surface roles.

Errors are sticky: a failed fetch is cached and returned on every subsequent call, so callers don't accidentally retry against a broken backend within the same process.

func (*Factory) LoggedIn

func (f *Factory) LoggedIn() bool

func (*Factory) SetWorkspaceOverride added in v0.18.0

func (f *Factory) SetWorkspaceOverride(ws *zcontext.Workspace)

SetWorkspaceOverride records the resolved workspace for a --workspace flag value. Called from PersistentPreRunE after the flag string has been disambiguated against the list of teams. Passing nil clears any prior override.

type PersistentFlags

type PersistentFlags struct {
	Debug            bool   // debug mode, default false
	Interactive      bool   // interactive mode, default true
	AutoRefreshToken bool   // auto refresh token, default true, only when token is from browser(OAuth2)
	AutoCheckUpdate  bool   // auto check update, default true
	JSON             bool   // output in JSON format, default false
	Workspace        string // --workspace <name|id> one-shot override
}

PersistentFlags are flags that are common to all commands

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL