Core Interfaces¶
This document provides a reference for all core interfaces in CMDR.
Command Interfaces¶
Command¶
Read-only view of a managed command1:
type Command interface {
GetName() string // Command name (e.g., "kubectl")
GetVersion() string // Version string (e.g., "1.28.0")
GetActivated() bool // Whether this version is activated
GetLocation() string // File path to the binary
}
CommandQuery¶
Fluent API for querying commands2:
type CommandQuery interface {
WithName(name string) CommandQuery
WithVersion(version string) CommandQuery
WithActivated(activated bool) CommandQuery
WithLocation(location string) CommandQuery
All() ([]Command, error) // Get all matching commands
One() (Command, error) // Get first matching command
Count() (int, error) // Count matching commands
}
Usage Example:
query, _ := manager.Query()
// Find activated kubectl
cmd, _ := query.WithName("kubectl").WithActivated(true).One()
// List all versions of node
commands, _ := query.WithName("node").All()
// Count total managed commands
count, _ := query.Count()
CommandManager¶
Full CRUD operations for commands3:
type CommandManager interface {
Close() error
Provider() CommandProvider
Query() (CommandQuery, error)
Define(name, version, location string) (Command, error)
Undefine(name, version string) error
Activate(name, version string) error
Deactivate(name string) error
}
Providers:
| Provider | Value | Implementation |
|---|---|---|
| Database | CommandProviderDatabase |
core/manager/database.go |
| Binary | CommandProviderBinary |
core/manager/binary.go |
| Download | CommandProviderDownload |
core/manager/download.go |
| Doctor | CommandProviderDoctor |
core/manager/doctor.go |
Initialization Interfaces¶
Initializer¶
System initialization tasks4:
Registered Initializers:
| Key | Implementation | Purpose |
|---|---|---|
"binary" |
BinaryManager |
Create bin/ and shims/ directories |
"profile-dir-backup" |
FSBackup |
Backup profile directory |
"profile-dir-export" |
EmbedFSExporter |
Export embedded profile scripts |
"profile-dir-render" |
DirRender |
Render profile templates |
"profile-injector" |
ProfileInjector |
Inject source line into shell profile |
"database" |
DatabaseInitializer |
Initialize database schema |
"command" |
CommandInitializer |
Register cmdr as managed command |
Download Interfaces¶
Fetcher¶
File download abstraction5:
Implementations:
GoGetterFetcher- Uses HashiCorp go-getter for flexible downloadsGoFetcher- Specialized for Go module downloads
DownloadStrategy¶
Strategy for URL transformationand retry logic:
type DownloadStrategy interface {
Name() string
Prepare(uri string) (string, error)
ShouldRetry(err error) bool
ShouldFallback(err error) bool
Configure(cfg Configuration) error
IsEnabled(uri string) bool
SetEnabled(enabled bool)
}
Implementations:
DirectStrategy- Direct downloadsProxyStrategy- HTTP/SOCKS5 proxyRewriteStrategy- URL rewritingChainStrategy- Chain of strategies with retry/fallback
Configuration Interface¶
Configuration¶
Type alias for Viper configuration6:
Common Methods:
// Get values
GetString(key string) string
GetInt(key string) int
GetBool(key string) bool
GetStringSlice(key string) []string
// Set values
Set(key string, value interface{})
SetDefault(key string, value interface{})
// Check existence
IsSet(key string) bool
// Bind flags
BindPFlag(key string, flag *pflag.Flag) error
Database Interface¶
Database¶
Abstraction over Storm/BoltDB7:
type Database interface {
Init() error
Close() error
Save(data interface{}) error
Select(matchers ...q.Matcher) Query
DeleteStruct(data interface{}) error
}
Used By: DatabaseManager for persisting command metadata.
CMDR Searcher Interface¶
CmdrSearcher¶
Search for CMDR releases (for self-upgrade)8:
type CmdrSearcher interface {
GetReleaseAsset(ctx context.Context, releaseName, assetName string) (CmdrReleaseAsset, error)
}
type CmdrReleaseAsset struct {
Name string // Release name
Version string // Version string
Asset string // Asset name
Url string // Download URL
}
Providers:
| Provider | Source |
|---|---|
CmdrSearcherProviderApi |
GitHub API |
CmdrSearcherProviderAtom |
GitHub Atom feed |
Logger Interface¶
CMDR uses logur for logging:
type Logger interface {
Trace(msg string, fields ...map[string]interface{})
Debug(msg string, fields ...map[string]interface{})
Info(msg string, fields ...map[string]interface{})
Warn(msg string, fields ...map[string]interface{})
Error(msg string, fields ...map[string]interface{})
}
Usage:
logger := core.GetLogger()
logger.Info("command installed", map[string]interface{}{
"name": "kubectl",
"version": "1.28.0",
})
Event Bus¶
Simple publish-subscribe for lifecycle events:
Events:
EventExit- Application exit
Usage:
// Subscribe
core.SubscribeEvent(core.EventExit, func() {
// Cleanup
})
// Publish
defer core.PublishEvent(core.EventExit)
-
Command interface in
core/command.goL18-L23 ↩ -
CommandQuery interface in
core/command.goL25-L34 ↩ -
CommandManager interface in
core/command.goL36-L47 ↩ -
Initializer interface in
core/initializer.goL5-L7 ↩ -
Fetcher interface in
core/fetcher.go↩ -
Configuration type in
core/config.goL7 ↩ -
Database interface in
core/database.go↩ -
CmdrSearcher interface in
core/cmdr.goL18-L20 ↩