Documentation
¶
Overview ¶
Package cli provides shared CLI infrastructure for writ and lore commands.
Index ¶
- Constants
- Variables
- func AddMutationFlags(cmd *cobra.Command, flags *MutationFlags)
- func AddOutputFlags(cmd *cobra.Command, flags *OutputFlags)
- func AddSilentFlag(cmd *cobra.Command)
- func AllSettings() map[string]interface{}
- func BashCompletionPath() string
- func BindFlags(cmd *cobra.Command, toolName string, useSharedConfig bool) error
- func BindFlagsWithPrefix(flags *pflag.FlagSet, prefix string) error
- func CacheHome() string
- func ConfigFileUsed() string
- func ConfigHome() string
- func DataHome() string
- func Debug()
- func DevloreCacheHome() string
- func DevloreConfigHome() string
- func DevloreDataHome() string
- func DevloreStateHome() string
- func DisplayManPage(cmd *cobra.Command, header *doc.GenManHeader) error
- func Error(format string, args ...interface{})
- func ExitCode(err error) int
- func ExitWith(code int, err error) error
- func Failure(format string, args ...interface{}) error
- func FishCompletionPath() string
- func Get(toolName, key string, useSharedConfig bool) interface{}
- func GetBool(toolName, key string, useSharedConfig bool) bool
- func GetInt(toolName, key string, useSharedConfig bool) int
- func GetString(toolName, key string, useSharedConfig bool) string
- func GetStringMap(toolName, key string, useSharedConfig bool) map[string]interface{}
- func GetStringSlice(toolName, key string, useSharedConfig bool) []string
- func InitViper(cfg ViperConfig) error
- func LatestReceiptPath(producer string) string
- func LoadLatestReceipt(producer string) (*op.Graph, error)
- func LoadReceipt(path string) (*op.Graph, error)
- func ManPath() string
- func NewConfigCmd(info ConfigInfo) *cobra.Command
- func NewHelpCmd(rootCmd *cobra.Command, header ManHeader) *cobra.Command
- func NewManCmd(rootCmd *cobra.Command, header ManHeader) *cobra.Command
- func NewSelfInstallCmd(rootCmd *cobra.Command, info SelfInstallInfo) *cobra.Command
- func NewVersionCmd(info VersionInfo) *cobra.Command
- func Note(format string, args ...interface{})
- func ReceiptsDir() string
- func Render(w io.Writer, data interface{}, flags OutputFlags) error
- func RenderMutation(w io.Writer, data interface{}, flags MutationFlags) error
- func RenderMutationTo(data interface{}, flags MutationFlags) error
- func RenderTo(data interface{}, flags OutputFlags) error
- func SetProgramName(name string)
- func SetSilent(s bool)
- func SharedConfigPath() string
- func StateHome() string
- func Success(format string, args ...interface{})
- func ToolConfigPath(toolName string) string
- func Warn(format string, args ...interface{})
- func WritLayersDir() string
- func WriteReceipt(g *op.Graph, producer string) (string, error)
- func WriteReceiptWithSigningDir(g *op.Graph, producer, signingDir string) (string, error)
- func ZshCompletionPath() string
- type ConfigInfo
- type ManHeader
- type MutationFlags
- type OutputFlags
- type SelfInstallInfo
- type VersionInfo
- type ViperConfig
Constants ¶
const ( ExitOK = 0 // Success ExitError = 1 // Generic error ExitUsage = 64 // Bad CLI syntax ExitDataErr = 65 // Invalid manifest/config ExitNoInput = 66 // File not found ExitSoftware = 70 // Internal error (bug) ExitCantCreate = 73 // Can't create file/symlink ExitIOErr = 74 // Read/write failure ExitNoPerm = 77 // Permission denied )
Exit codes follow BSD sysexits.h conventions for portable process status.
const DefaultFormat = "json"
DefaultFormat is the default output format (JSON for scriptability).
Variables ¶
var ErrManNotAvailable = errors.New("man command not available")
ErrManNotAvailable indicates the man command is not available on this system.
Functions ¶
func AddMutationFlags ¶
func AddMutationFlags(cmd *cobra.Command, flags *MutationFlags)
AddMutationFlags adds --passthru and --format flags for mutating commands. Mutating commands are silent by default; --passthru outputs what was changed. Usage:
var flags cli.MutationFlags cli.AddMutationFlags(cmd, &flags)
func AddOutputFlags ¶
func AddOutputFlags(cmd *cobra.Command, flags *OutputFlags)
AddOutputFlags adds --filter and --format flags to a command. Usage:
var output cli.OutputFlags cli.AddOutputFlags(cmd, &output)
func AddSilentFlag ¶
AddSilentFlag adds the --silent flag to a root command.
func AllSettings ¶
func AllSettings() map[string]interface{}
AllSettings returns all settings as a map.
func BashCompletionPath ¶
func BashCompletionPath() string
BashCompletionPath returns the bash completion directory. XDG_DATA_HOME/bash-completion/completions
func BindFlags ¶
BindFlags binds all persistent flags from a command to Viper. Call this after defining flags and before Execute().
Flags are bound with the tool's section prefix when using shared config:
- --repo flag → viper key "writ.repo" (with UseSharedConfig)
- --repo flag → viper key "repo" (without UseSharedConfig)
func BindFlagsWithPrefix ¶
BindFlagsWithPrefix binds flags with a custom prefix. Useful for subcommands that need their own config section.
Example:
BindFlagsWithPrefix(addCmd.Flags(), "writ.add") // --overwrite flag → viper key "writ.add.overwrite"
func ConfigFileUsed ¶
func ConfigFileUsed() string
ConfigFileUsed returns the config file path that Viper loaded. Returns empty string if no config file was found.
func DevloreCacheHome ¶
func DevloreCacheHome() string
DevloreCacheHome returns the unified devlore cache directory. XDG_CACHE_HOME/devlore
func DevloreConfigHome ¶
func DevloreConfigHome() string
DevloreConfigHome returns the unified devlore config directory. XDG_CONFIG_HOME/devlore
func DevloreDataHome ¶
func DevloreDataHome() string
DevloreDataHome returns the unified devlore data directory. XDG_DATA_HOME/devlore
func DevloreStateHome ¶
func DevloreStateHome() string
DevloreStateHome returns the unified devlore state directory. XDG_STATE_HOME/devlore
func DisplayManPage ¶
func DisplayManPage(cmd *cobra.Command, header *doc.GenManHeader) error
DisplayManPage generates a man page and displays it with the system pager. Returns ErrManNotAvailable if man is not available on this system.
func Error ¶
func Error(format string, args ...interface{})
Error prints an error message to stderr. Unlike Failure, this does not return an error—use for non-fatal errors.
func ExitCode ¶
ExitCode extracts the exit code from an error. Returns the wrapped code if present, or ExitError (1) for plain errors.
func Failure ¶
Failure prints an error message to stderr and returns an error. Use when the operation cannot continue.
func FishCompletionPath ¶
func FishCompletionPath() string
FishCompletionPath returns the fish completion directory. XDG_CONFIG_HOME/fish/completions
func Get ¶
Get retrieves a config value with the tool's section prefix. When using shared config, automatically prefixes with tool name.
Example:
Get("writ", "repo", true) → viper.Get("writ.repo")
Get("writ", "repo", false) → viper.Get("repo")
func GetStringMap ¶
GetStringMap retrieves a string map config value.
func GetStringSlice ¶
GetStringSlice retrieves a string slice config value.
func InitViper ¶
func InitViper(cfg ViperConfig) error
InitViper initializes Viper with standard devlore conventions. Call this in PersistentPreRunE of the root command.
Precedence (lowest to highest):
- Config file defaults
- Config file values
- Environment variables (TOOL_KEY_NAME)
- Command-line flags
Environment variable mapping:
- WRIT_REPO → writ.repo (with UseSharedConfig)
- WRIT_VARS_USER_NAME → writ.vars.user_name
- Dots become underscores, keys are case-insensitive
func LatestReceiptPath ¶
LatestReceiptPath returns the path to the latest receipt symlink for a producer. Producer is typically a command name: "writ", "lore", etc.
func LoadLatestReceipt ¶
LoadLatestReceipt loads the most recent receipt for a producer.
func LoadReceipt ¶
LoadReceipt loads an execution graph from a YAML receipt file.
func ManPath ¶
func ManPath() string
ManPath returns the user man page directory: XDG_DATA_HOME/man/man1
func NewConfigCmd ¶
func NewConfigCmd(info ConfigInfo) *cobra.Command
NewConfigCmd creates the config command with git-style subcommands.
Dot-paths match the config file structure exactly. No implicit prefixing. "writ.repos.0.path" in the CLI reads writ.repos[0].path in the file. "secrets.mode" reads secrets.mode. WYSIWYG.
Usage:
tool config get <key>... # Get values tool config set <key>=<value>... # Set values tool config unset <key>... # Remove keys tool config list # List all settings tool config edit # Open in $EDITOR tool config validate # Validate against schema tool config schema # Output JSON schema tool config path # Show config file location
func NewHelpCmd ¶
NewHelpCmd creates a help command that prefers man pages when available. This follows git's model: if man pages are installed, display them via pager; otherwise fall back to console text output.
func NewManCmd ¶
NewManCmd creates the man command for displaying/installing man pages. Usage:
tool man # display man page with pager tool man --install # install to ~/.local/share/man/man1/ tool man deploy # display man page for subcommand
func NewSelfInstallCmd ¶
func NewSelfInstallCmd(rootCmd *cobra.Command, info SelfInstallInfo) *cobra.Command
NewSelfInstallCmd creates the self-install command. Usage:
./tool self-install --prefix=~/.local ./tool self-install --prefix=~/.local --shell bash --shell zsh
This performs complete installation:
- Copies binary to <root>/bin/
- Installs man pages to <root>/share/man/man1/ (if man command exists)
- Installs completions for detected shells (or specified via --shell)
- Initializes config in XDG_CONFIG_HOME/devlore/
- Initializes cache in XDG_CACHE_HOME/devlore/
- Creates layer directories in XDG_DATA_HOME/devlore/writ/layers/
func NewVersionCmd ¶
func NewVersionCmd(info VersionInfo) *cobra.Command
NewVersionCmd creates the version command.
func Note ¶
func Note(format string, args ...interface{})
Note prints an informational message to stderr.
func ReceiptsDir ¶
func ReceiptsDir() string
ReceiptsDir returns the directory where receipts are stored. Location: $XDG_STATE_HOME/devlore/receipts (typically ~/.local/state/devlore/receipts)
func Render ¶
func Render(w io.Writer, data interface{}, flags OutputFlags) error
Render outputs data according to the format specification. Supported formats: json, yaml, table, or a Go template string.
func RenderMutation ¶
func RenderMutation(w io.Writer, data interface{}, flags MutationFlags) error
RenderMutation outputs data only if --passthru is set. Returns nil without output if passthru is false.
func RenderMutationTo ¶
func RenderMutationTo(data interface{}, flags MutationFlags) error
RenderMutationTo is a convenience function that renders to stdout if --passthru is set.
func RenderTo ¶
func RenderTo(data interface{}, flags OutputFlags) error
RenderTo is a convenience function that renders to stdout.
func SetProgramName ¶
func SetProgramName(name string)
SetProgramName sets the program name used in output prefixes.
func SharedConfigPath ¶
func SharedConfigPath() string
SharedConfigPath returns the path to the shared devlore config file.
func Success ¶
func Success(format string, args ...interface{})
Success prints a success message to stderr.
func ToolConfigPath ¶
ToolConfigPath returns the path to a tool-specific config file.
func WritLayersDir ¶
func WritLayersDir() string
WritLayersDir returns the writ layers directory. XDG_DATA_HOME/devlore/writ/layers
func WriteReceipt ¶
WriteReceipt writes the graph as a receipt to the receipts directory. The producer identifies which command created the receipt (e.g., "writ", "lore"). Returns the path where the receipt was written.
Receipts are produced at the end of lifecycle operations: writ: Migrate, Adopt, Deploy, Upgrade, Reconcile, Decommission lore: Onboard
The receipt is checksummed before writing. Signing is performed using the first available backend from .sops.yaml (GPG, AWS KMS, GCP KMS, or Azure Key Vault). The .sops.yaml is expected at ${XDG_STATE_HOME}/devlore/.sops.yaml.
func WriteReceiptWithSigningDir ¶
WriteReceiptWithSigningDir writes the graph as a receipt, searching for .sops.yaml starting from signingDir to configure signing backends.
func ZshCompletionPath ¶
func ZshCompletionPath() string
ZshCompletionPath returns the zsh completion directory. XDG_DATA_HOME/zsh/site-functions
Types ¶
type ConfigInfo ¶
type ConfigInfo struct {
Name string // Tool name (e.g., "lore", "writ")
Schema []byte // Embedded JSON schema
DefaultConfig []byte // Default configuration content
}
ConfigInfo contains configuration metadata for a tool.
type MutationFlags ¶
MutationFlags holds flags for mutating commands (--passthru).
type OutputFlags ¶
OutputFlags holds the --filter and --format flag values.
type SelfInstallInfo ¶
type SelfInstallInfo struct {
Name string // Tool name (e.g., "lore", "writ")
ManHeader ManHeader // Man page header metadata
ConfigInfo ConfigInfo // Config schema and defaults
}
SelfInstallInfo contains metadata needed for self-installation.
type VersionInfo ¶
type VersionInfo struct {
Version string // Semantic version (e.g., "0.1.0")
Commit string // Git commit hash
BuildDate string // Build timestamp
}
VersionInfo contains version metadata set at build time.
type ViperConfig ¶
type ViperConfig struct {
// Name is the tool name (e.g., "lore", "writ")
Name string
// EnvPrefix is the environment variable prefix (e.g., "LORE", "WRIT")
// If empty, defaults to uppercase Name
EnvPrefix string
// ConfigName is the config file name without extension (default: "config")
ConfigName string
// ConfigType is the config file type (default: "yaml")
ConfigType string
// When true, config is read from the tool's section (e.g., config.writ.repo)
UseSharedConfig bool
}
ViperConfig holds configuration for Viper initialization.