Documentation
¶
Overview ¶
Package version provides shared version metadata and a reusable version command for azd extensions, so each extension does not reimplement the same boilerplate.
Info holds the values normally injected at build time through ldflags. NewCommand turns an Info into a cobra command that prints either a human-readable summary, the bare version with --quiet, or the full struct as JSON.
Typical wiring, alongside a root command from github.com/azure/azure-dev/cli/azd/pkg/azdext.NewExtensionRootCommand:
var Info = version.New("jongio.azd.example", "azd example")
rootCmd, extCtx := azdext.NewExtensionRootCommand(opts)
rootCmd.AddCommand(version.NewCommand(Info, &extCtx.OutputFormat))
That form declares the supported --output values to azd, which drives shell completion, help text, extension metadata, and parse-time validation.
Two things are configurable because hardcoding either one breaks real extensions:
WithQuietShorthand changes or removes the -q shorthand on --quiet. cobra panics during flag parsing when a subcommand shorthand collides with an inherited persistent flag, so an extension that already uses -q must pass "".
WithOutputFlag names the flag that supplies the outputFormat pointer, or disables the declaration with an empty name. An extension that reads its own output flag rather than azd's --output should disable it, so azd does not validate a flag the command never reads.
Package version provides shared version information and a reusable version command for azd extensions, eliminating duplicated version boilerplate.
Index ¶
Constants ¶
const ( // OutputFlagName is the azd global output flag that [NewCommand] declares // supported values for. OutputFlagName = "output" // OutputFormatDefault renders human-readable output. OutputFormatDefault = "default" // OutputFormatJSON renders the [Info] struct as indented JSON. OutputFormatJSON = "json" )
Variables ¶
This section is empty.
Functions ¶
func NewCommand ¶
NewCommand creates a version command that displays extension version info. outputFormat is an optional pointer to a global output format flag (e.g. "json"). If nil, defaults to human-readable output.
By default the command declares its supported --output values through azdext.RegisterFlagOptions. When the root command comes from azdext.NewExtensionRootCommand, that declaration gives azd shell completion, rejects unsupported values before RunE, and surfaces the values in extension metadata. See WithOutputFlag to point it at a different flag or turn it off.
Types ¶
type Info ¶
type Info struct {
Version string `json:"version"`
BuildDate string `json:"buildDate"`
GitCommit string `json:"gitCommit"`
ExtensionID string `json:"extensionId"`
Name string `json:"name"`
}
Info holds version information for an extension.
type Option ¶ added in v0.6.0
type Option func(*options)
Option customizes the command returned by NewCommand.
func WithOutputFlag ¶ added in v0.6.0
WithOutputFlag names the flag that supplies the outputFormat pointer passed to NewCommand, along with the values it accepts. It defaults to "output" with "default" and "json", matching the flag azdext.NewExtensionRootCommand registers.
Pass an empty name to skip the azdext.RegisterFlagOptions declaration entirely. Extensions that bind outputFormat to a flag of their own, with their own vocabulary, should do that: declaring the wrong flag name makes azd validate and complete a flag the command never reads.
func WithQuietShorthand ¶ added in v0.6.0
WithQuietShorthand overrides the single-letter shorthand bound to --quiet, which defaults to "q". Pass an empty string to register --quiet with no shorthand at all.
This exists because cobra panics when a subcommand's own shorthand collides with one it inherits from a parent's persistent flags, and the panic happens during flag parsing rather than at construction. An extension that already binds -q to one of its own persistent flags must pass "" here, otherwise every invocation of the extension crashes.