CLI Package - Go Implementation

Import: github.com/haivivi/giztoy/pkg/cli

📚 Go Documentation

Types

Config

type Config struct {
    AppName        string              `yaml:"-"`
    CurrentContext string              `yaml:"current_context,omitempty"`
    Contexts       map[string]*Context `yaml:"contexts,omitempty"`
}

Methods:

MethodSignatureDescription
LoadConfigfunc LoadConfig(appName string) (*Config, error)Load from default path
LoadConfigWithPathfunc LoadConfigWithPath(appName, path string) (*Config, error)Load from custom path
Save(c *Config) Save() errorSave to disk
Path(c *Config) Path() stringGet config file path
Dir(c *Config) Dir() stringGet config directory
AddContext(c *Config) AddContext(name string, ctx *Context) errorAdd context
DeleteContext(c *Config) DeleteContext(name string) errorDelete context
UseContext(c *Config) UseContext(name string) errorSet current context
GetContext(c *Config) GetContext(name string) (*Context, error)Get specific context
GetCurrentContext(c *Config) GetCurrentContext() (*Context, error)Get current context
ResolveContext(c *Config) ResolveContext(name string) (*Context, error)Resolve by name or current
ListContexts(c *Config) ListContexts() []stringList all context names

Context

type Context struct {
    Name         string              `yaml:"name"`
    Client       *ClientCredentials  `yaml:"client,omitempty"`
    Console      *ConsoleCredentials `yaml:"console,omitempty"`
    APIKey       string              `yaml:"api_key,omitempty"`
    BaseURL      string              `yaml:"base_url,omitempty"`
    Timeout      int                 `yaml:"timeout,omitempty"`
    MaxRetries   int                 `yaml:"max_retries,omitempty"`
    DefaultVoice string              `yaml:"default_voice,omitempty"`
    Extra        map[string]string   `yaml:"extra,omitempty"`
}

Output

type OutputFormat string

const (
    FormatYAML  OutputFormat = "yaml"
    FormatJSON  OutputFormat = "json"
    FormatTable OutputFormat = "table"
    FormatRaw   OutputFormat = "raw"
)

type OutputOptions struct {
    Format OutputFormat
    File   string
    Indent string
    Writer io.Writer
}

Functions:

FunctionSignatureDescription
Outputfunc Output(result any, opts OutputOptions) errorWrite formatted output
OutputBytesfunc OutputBytes(data []byte, path string) errorWrite binary data
PrintSuccessfunc PrintSuccess(format string, args ...any)Print ✓ message
PrintErrorfunc PrintError(format string, args ...any)Print error to stderr
PrintInfofunc PrintInfo(format string, args ...any)Print ℹ message
PrintWarningfunc PrintWarning(format string, args ...any)Print âš  message
PrintVerbosefunc PrintVerbose(verbose bool, format string, args ...any)Conditional verbose

Paths

type Paths struct {
    AppName string
    HomeDir string
}

Methods:

MethodSignatureDescription
NewPathsfunc NewPaths(appName string) (*Paths, error)Create paths instance
BaseDir(p *Paths) BaseDir() string~/.giztoy
AppDir(p *Paths) AppDir() string~/.giztoy/
ConfigFile(p *Paths) ConfigFile() string~/.giztoy//config.yaml
CacheDir(p *Paths) CacheDir() string~/.giztoy//cache
LogDir(p *Paths) LogDir() string~/.giztoy//logs
DataDir(p *Paths) DataDir() string~/.giztoy//data
EnsureAppDir(p *Paths) EnsureAppDir() errorCreate app dir
CachePath(p *Paths) CachePath(name string) stringPath in cache
LogPath(p *Paths) LogPath(name string) stringPath in logs
DataPath(p *Paths) DataPath(name string) stringPath in data

Usage

Load Configuration

cfg, err := cli.LoadConfig("minimax")
if err != nil {
    log.Fatal(err)
}

// Get current context
ctx, err := cfg.GetCurrentContext()
if err != nil {
    log.Fatal(err)
}

fmt.Println("API Key:", cli.MaskAPIKey(ctx.APIKey))

Output Results

result := map[string]string{"status": "ok", "message": "done"}

// Output as JSON to stdout
cli.Output(result, cli.OutputOptions{
    Format: cli.FormatJSON,
})

// Output as YAML to file
cli.Output(result, cli.OutputOptions{
    Format: cli.FormatYAML,
    File:   "output.yaml",
})
cli.PrintSuccess("Created context %q", "production")
cli.PrintError("Failed to connect: %v", err)
cli.PrintInfo("Using API endpoint: %s", baseURL)
cli.PrintWarning("Rate limit approaching")
cli.PrintVerbose(verbose, "Request: %+v", req)

Path Management

paths, _ := cli.NewPaths("minimax")

// Ensure directories exist
paths.EnsureCacheDir()
paths.EnsureLogDir()

// Get paths
cachePath := paths.CachePath("response.json")
logPath := paths.LogPath("2024-01-15.log")

Dependencies

  • github.com/goccy/go-yaml - YAML parsing
  • encoding/json (stdlib) - JSON parsing