util

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2026 License: MIT Imports: 34 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCancel = errors.New("CancelError")

ErrCancel signals user-initiated cancellation

View Source
var ErrNotImplemented = errors.New("NotImplementedError")

ErrNotImplemented is an error that indicates a feature is not implemented

View Source
var ErrSilent = errors.New("SilentError")

ErrSilent is an error that triggers exit code 1 without any error messaging

Functions

func AddFormatFlags added in v0.1.0

func AddFormatFlags(cmd *cobra.Command, exportTarget *Exporter)

func AddGroup added in v0.1.0

func AddGroup(parent *cobra.Command, title string, cmds ...*cobra.Command)

func AddJSONFlags added in v0.1.0

func AddJSONFlags(cmd *cobra.Command, exportTarget *Exporter, fields []string)

func CheckAuth

func CheckAuth(cfg config.Config) bool

func DisableAuthCheck

func DisableAuthCheck(cmd *cobra.Command)

func EnableRepoOverride added in v0.1.0

func EnableRepoOverride(ctx CmdContext, cmds ...*cobra.Command)

func ExactArgs

func ExactArgs(n int, msg string) cobra.PositionalArgs

func FlagErrorWrap

func FlagErrorWrap(err error) error

FlagError returns a new FlagError that wraps the specified error.

func FlagErrorf

func FlagErrorf(format string, args ...any) error

FlagErrorf returns a new FlagError that wraps an error produced by fmt.Errorf(format, args...).

func FormatTimePtr added in v0.8.0

func FormatTimePtr(ts *azuredevops.Time) *string

FormatTimePtr formats an *azuredevops.Time as a *string using its query parameter format. Returns nil for nil input.

func FormatTimeShort added in v0.8.0

func FormatTimeShort(ts *azuredevops.Time) string

FormatTimeShort formats an *azuredevops.Time as a human-readable string ("2006-01-02 15:04:05"). Returns "" for nil or zero-value input.

func IsAuthCheckEnabled

func IsAuthCheckEnabled(cmd *cobra.Command) bool

func IsNotFoundError added in v0.1.0

func IsNotFoundError(err error) bool

IsNotFoundError checks if the given error is an Azure DevOps API "Not Found" (HTTP 404) error.

func IsUserCancellation

func IsUserCancellation(err error) bool

func MinimumArgs

func MinimumArgs(n int, msg string) cobra.PositionalArgs

func MutuallyExclusive

func MutuallyExclusive(message string, conditions ...bool) error

func NewJSONExporter added in v0.1.0

func NewJSONExporter() *jsonExporter

NewJSONExporter returns an Exporter to emit JSON.

func NewPatAuthenticator

func NewPatAuthenticator(cfg config.Config) (instance azdo.Authenticator, err error)

func NilBoolFlag

func NilBoolFlag(cmd *cobra.Command, p **bool, name string, shorthand string, usage string) *pflag.Flag

NilBoolFlag defines a new flag with a bool pointer receiver. This is useful for differentiating between the flag being explicitly set to a false value and the flag not being passed at all.

func NilIntFlag added in v0.8.0

func NilIntFlag(cmd *cobra.Command, p **int, name string, shorthand string, usage string) *pflag.Flag

NilIntFlag defines a new flag with an int pointer receiver. This is useful for differentiating between the flag being explicitly set to 0 and the flag not being passed at all.

func NilStringEnumFlag added in v0.8.0

func NilStringEnumFlag(cmd *cobra.Command, p **string, name, shorthand string, options []string, usage string) *pflag.Flag

func NilStringFlag

func NilStringFlag(cmd *cobra.Command, p **string, name string, shorthand string, usage string) *pflag.Flag

NilStringFlag defines a new flag with a string pointer receiver. This is useful for differentiating between the flag being set to a blank value and the flag not being passed at all.

func NilStringSliceEnumFlag added in v0.8.0

func NilStringSliceEnumFlag(cmd *cobra.Command, p *[]string, name, shorthand string, options []string, usage string) *pflag.Flag

func NoArgsQuoteReminder

func NoArgsQuoteReminder(cmd *cobra.Command, args []string) error

func ParseOrganizationArg added in v0.6.0

func ParseOrganizationArg(ctx CmdContext, arg string) (string, error)

ParseOrganizationArg resolves the organization from an organization-only argument of the form ORG or ORG:. When the input is empty, the default organization from the user configuration is returned. This is the only wrapper that classifies a bare segment as an organization; structured wrappers never do.

func ReadFile added in v0.1.0

func ReadFile(filename string, stdin io.ReadCloser) ([]byte, error)

func RegisterBranchCompletionFlags

func RegisterBranchCompletionFlags(gitc gitClient, cmd *cobra.Command, flags ...string) error

RegisterBranchCompletionFlags suggests and autocompletes known remote git branches for flags passed

func ResolveScopeDescriptor added in v0.5.0

func ResolveScopeDescriptor(ctx CmdContext, organization, project string) (*string, *string, error)

ResolveScopeDescriptor fetches the descriptor representing the project scope when a project is supplied. It returns the descriptor value along with the project ID string to support callers that need to distinguish between identically named groups scoped to different projects.

func StringEnumFlag

func StringEnumFlag(cmd *cobra.Command, p *string, name, shorthand, defaultValue string, options []string, usage string) *pflag.Flag

StringEnumFlag defines a new string flag that only allows values listed in options.

func StringSliceEnumFlag

func StringSliceEnumFlag(cmd *cobra.Command, p *[]string, name, shorthand string, defaultValues, options []string, usage string) *pflag.Flag

Types

type CmdContext

type CmdContext interface {
	util.ContextAware
	RepoContext() RepoContext
	ConnectionFactory() azdo.ConnectionFactory
	ClientFactory() azdo.ClientFactory
	Prompter() (prompter.Prompter, error)
	Config() (config.Config, error)
	IOStreams() (*iostreams.IOStreams, error)
	Printer(string) (printer.Printer, error)
}

func NewCmdContext

func NewCmdContext() (ctx CmdContext, err error)

type Exporter added in v0.1.0

type Exporter interface {
	Fields() []string
	Write(io *iostreams.IOStreams, data any) error
}

type ExternalCommandExitError added in v0.0.2

type ExternalCommandExitError struct {
	// contains filtered or unexported fields
}

func NewExternalCommandExitError

func NewExternalCommandExitError(err *exec.ExitError) ExternalCommandExitError

func (ExternalCommandExitError) Error added in v0.0.2

func (e ExternalCommandExitError) Error() string

func (ExternalCommandExitError) ExitCode added in v0.0.2

func (e ExternalCommandExitError) ExitCode() int

type FlagError added in v0.0.2

type FlagError struct {
	// contains filtered or unexported fields
}

A *FlagError indicates an error processing command-line flags or other arguments. Such errors cause the application to display the usage message.

func (*FlagError) Error added in v0.0.2

func (fe *FlagError) Error() string

func (*FlagError) Unwrap added in v0.0.2

func (fe *FlagError) Unwrap() error

type JSONFlagError added in v0.1.0

type JSONFlagError struct {
	// contains filtered or unexported fields
}

type NoResultsError added in v0.0.2

type NoResultsError struct {
	// contains filtered or unexported fields
}

func NewNoResultsError

func NewNoResultsError(message string) NoResultsError

func (NoResultsError) Error added in v0.0.2

func (e NoResultsError) Error() string

func (NoResultsError) Is added in v0.1.0

func (e NoResultsError) Is(target error) bool

Is allows errors.Is to match any NoResultsError regardless of message content.

type ParseOptions added in v0.8.0

type ParseOptions struct {
	// AllowImplicitOrg allows the ORG: prefix to be omitted. When omitted, Parse
	// loads the default organization from ctx.Config().Authentication().GetDefaultOrganization().
	AllowImplicitOrg bool
	// RequireProject requires one project segment before any target segments.
	RequireProject bool
	// DisallowProject rejects inputs that carry a project segment. Inputs must
	// use the leading-slash no-project marker, for example "/POOL/AGENT" or
	// "ORG:/POOL/AGENT".
	DisallowProject bool
	// DisallowTargets rejects inputs that carry target segments. It must be set
	// whenever a wrapper accepts no targets at all; MaxTargets cannot express
	// this because MaxTargets == 0 means unbounded.
	DisallowTargets bool
	// MinTargets is the required number of trailing target segments.
	MinTargets int
	// MaxTargets is the maximum number of trailing target segments. Zero means
	// unbounded; use DisallowTargets to reject targets entirely.
	MaxTargets int
}

ParseOptions configures how a raw user input is split into a Path.

The organization is always taken from an explicit ORG: prefix. A bare leading segment is never treated as an organization by structured parsing; only ParseOrganizationArg classifies a bare segment as an organization.

type Path added in v0.8.0

type Path struct {
	Organization string
	Project      string
	Targets      []string
}

Path represents a parsed user-input scope of the form [ORG:][PROJECT/]TARGET[/TARGET...] or [ORG:]/TARGET[/TARGET...]. Organization is always populated after a successful Parse.

func Parse added in v0.8.0

func Parse(ctx CmdContext, raw string, opts ParseOptions) (*Path, error)

Parse splits raw command input into a Path using deterministic Azure DevOps-style scope rules. Heuristics never decide whether a segment is an organization, project, or target: an explicit "ORG:" prefix carries the organization and a leading "/" marks the no-project form.

The input is trimmed, then an optional ORG: prefix is recognized. The remainder is split on "/", each segment is trimmed, and empty segments are rejected. After the organization prefix the grammar is:

PROJECT/TARGET...   project-first form; the first segment is the project
/TARGET...          no-project form; every segment is a target

The project rule (required, optional, or disallowed) and the target range select the valid shapes for a mode:

  • AllowImplicitOrg allows the ORG: prefix to be omitted. When omitted, Parse loads the default organization from the user configuration.
  • RequireProject requires the project-first form.
  • DisallowProject requires the no-project form.
  • DisallowTargets rejects any target segments.
  • MinTargets defines the required trailing target count.
  • MaxTargets defines the allowed trailing target count. Zero means unbounded; DisallowTargets is the only way to express that no targets are allowed.

Ambiguous inputs such as a legacy "ORG/SUBJECT" are interpreted as canonical project-first forms (PROJECT/SUBJECT) and are never auto-detected as an organization. Structurally detectable legacy organization forms — for example "ORG/PROJECT" where a mode cannot accept a project-plus-extra-segment shape — are rejected with ORG: guidance. The no-project form requires the "/" marker; an organization-only input in a mode that accepts targets must use "ORG:/".

Examples:

Parse(ctx, "org:/group", ParseOptions{AllowImplicitOrg: false, MinTargets: 1, MaxTargets: 1})
// => &Path{Organization: "org", Targets: []string{"group"}}

Parse(nil, "org:project/group", ParseOptions{AllowImplicitOrg: false, MinTargets: 1, MaxTargets: 1})
// => &Path{Organization: "org", Project: "project", Targets: []string{"group"}}

Parse(ctx, "/group", ParseOptions{AllowImplicitOrg: true, MinTargets: 1, MaxTargets: 1})
// => &Path{Organization: <default org>, Targets: []string{"group"}}

Parse(ctx, "project/group", ParseOptions{AllowImplicitOrg: true, MinTargets: 1, MaxTargets: 1})
// => &Path{Organization: <default org>, Project: "project", Targets: []string{"group"}}

Parse(ctx, "project", ParseOptions{AllowImplicitOrg: true, DisallowTargets: true})
// => &Path{Organization: <default org>, Project: "project"}

Parse(ctx, "org:", ParseOptions{AllowImplicitOrg: true, DisallowTargets: true})
// => &Path{Organization: "org"}

Parse(ctx, "/pool/agent", ParseOptions{AllowImplicitOrg: true, DisallowProject: true, MinTargets: 2, MaxTargets: 2})
// => &Path{Organization: <default org>, Targets: []string{"pool", "agent"}}

Error conditions:

  • opts are invalid, for example a negative target count, MaxTargets below MinTargets, DisallowTargets combined with a positive target bound, or RequireProject combined with DisallowProject
  • the input contains multiple or misplaced colons, an empty ORG: prefix, or an empty segment after trimming, for example "org:project/" or "org:/ /x"
  • the input shape violates the project rule or the target range
  • an explicit organization is required but the ORG: prefix is missing
  • a structurally detectable legacy ORGANIZATION/... form is used without ORG:
  • "ORG:" is used in a mode that accepts targets instead of the "ORG:/" marker
  • organization is omitted but ctx is nil
  • organization is omitted and the default organization lookup fails or returns empty

func ParsePoolAgentTargetWithDefaultOrganization added in v0.8.0

func ParsePoolAgentTargetWithDefaultOrganization(ctx CmdContext, raw string) (*Path, error)

ParsePoolAgentTargetWithDefaultOrganization resolves a pool/agent target that allows an implicit organization by falling back to the configured default. Accepted forms are /POOL/AGENT and ORG:/POOL/AGENT; the no-project marker is required because this mode disallows projects.

func ParseProjectScope added in v0.6.0

func ParseProjectScope(ctx CmdContext, arg string) (*Path, error)

ParseProjectScope parses arguments of the form PROJECT or ORG:PROJECT. When the organization prefix is omitted the default organization from the user's configuration is used. Legacy ORGANIZATION/PROJECT inputs are rejected with ORG: guidance.

func ParseProjectTargetWithDefaultOrganization added in v0.6.0

func ParseProjectTargetWithDefaultOrganization(ctx CmdContext, target string) (*Path, error)

ParseProjectTargetWithDefaultOrganization resolves targets that must include a project segment. Accepted forms are PROJECT/TARGET and ORG:PROJECT/TARGET, falling back to the user's default organization when the organization prefix is omitted.

func ParseScope added in v0.5.0

func ParseScope(ctx CmdContext, scope string) (*Path, error)

ParseScope resolves the organization and optional project from an input argument. Accepted forms are PROJECT, ORG:, and ORG:PROJECT; an empty input resolves to the default organization from the user configuration. Bare segments are project-first, so a bare ORG is parsed as a project.

func ParseTarget added in v0.6.0

func ParseTarget(target string) (*Path, error)

ParseTarget validates and parses a target argument of the form ORG:/TARGET or ORG:PROJECT/TARGET. An explicit organization is required.

func ParseTargetWithDefaultOrganization added in v0.6.0

func ParseTargetWithDefaultOrganization(ctx CmdContext, target string) (*Path, error)

ParseTargetWithDefaultOrganization resolves a target that allows an implicit organization by falling back to the configured default. Accepted forms are /TARGET, PROJECT/TARGET, ORG:/TARGET, and ORG:PROJECT/TARGET. A legacy ORG/TARGET input is parsed as a project-first PROJECT/TARGET.

type RepoContext added in v0.1.0

type RepoContext interface {
	util.ContextAware
	GitCommand() (git.GitCommand, error)
	Remotes() (azdo.RemoteSet, error)
	Remote(*azdogit.GitRepository) (*azdo.Remote, error)
	WithRepo(func() (azdo.Repository, error)) RepoContext
	Repo() (azdo.Repository, error)
	GitClient() (azdogit.Client, error)
	GitRepository() (*azdogit.GitRepository, error)
}

Jump to

Keyboard shortcuts

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