cli

package
v1.7.3 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ColorReset  = "\033[0m"
	ColorRed    = "\033[31m"
	ColorGreen  = "\033[32m"
	ColorYellow = "\033[33m"
	ColorBlue   = "\033[34m"
	ColorGray   = "\033[90m"
)

Color codes for terminal output

Variables

This section is empty.

Functions

func CaptureOutput

func CaptureOutput(f func()) (stdout, stderr string)

CaptureOutput captures stdout and stderr for testing

func Error

func Error(format string, args ...interface{})

Error prints an error message in red to stderr

func Errorf

func Errorf(err error)

Errorf prints an error object in red to stderr

func Fatalf

func Fatalf(format string, args ...interface{})

Fatalf prints an error message in red to stderr and exits with code 1

func Info

func Info(format string, args ...interface{})

Info prints an info message in blue

func JSON

func JSON(v interface{}, pretty bool) error

JSON prints a value as JSON (pretty-printed by default)

func KeyValue

func KeyValue(pairs map[string]string)

KeyValue prints key-value pairs

func Print

func Print(format string, args ...interface{})

Print prints a formatted message

func Success

func Success(format string, args ...interface{})

Success prints a success message in green

func Table

func Table(headers []string, rows [][]string)

Table prints a simple table

func TestDB

func TestDB(t *testing.T, composeFile string) *sql.DB

TestDB opens a database connection derived from a schemaf compose file. It reads the x-schemaf metadata (project name, db-port, dev-db-pass) from the resolved compose files so tests never need to hardcode credentials or set DATABASE_URL manually.

Falls back to DATABASE_URL if set (CI / external Postgres). Registers t.Cleanup to close the connection automatically.

Example:

db := cli.TestDB(t, "../compose/test.yml")

func Warning

func Warning(format string, args ...interface{})

Warning prints a warning message in yellow

Types

type APIDefinition

type APIDefinition struct {
	Name        string // e.g. "foo"
	Description string // Human-readable description of the API

	URLDefault   string // base url of the API, e.g. "http://127.0.0.1:8080/api"
	URLConfigKey string // key in config to customize URL, e.g. "foo.api.url"

	HasEvents bool // Whether this API has standardized /events endpoint
	HasHealth bool // Whether this API has a standardized /health endpoint
	HasStatus bool // Whether this API has a standardized /status endpoint

	EventsPath *string // Path to events endpoint, default: "/events"
	HealthPath *string // Path to health endpoint, default: "/health"
	StatusPath *string // Path to status endpoint, default: "/status"
}

APIDefinition defines a registered API

func (*APIDefinition) CheckHealth

func (a *APIDefinition) CheckHealth(url string, timeout time.Duration) *APIStatus

CheckHealth checks the health of an API

type APIRegistry

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

APIRegistry holds registered APIs and their configuration

func NewAPIRegistry

func NewAPIRegistry() *APIRegistry

NewAPIRegistry creates a new API registry

func (*APIRegistry) GetAll

func (r *APIRegistry) GetAll() []*APIDefinition

GetAll returns all registered APIs sorted by name

func (*APIRegistry) Register

func (r *APIRegistry) Register(api APIDefinition)

Register adds an API to the registry

type APIStatus

type APIStatus struct {
	Name         string `json:"name"`
	URL          string `json:"url"`
	Status       string `json:"status"`      // "healthy", "down", "unknown"
	ResponseTime int64  `json:"response_ms"` // Response time in milliseconds
	Error        string `json:"error,omitempty"`
}

APIStatus represents the health status of an API

type ApiProvider

type ApiProvider func() []APIDefinition

ApiProvider registers APIs with the API registry

type CLI

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

CLI represents the composable CLI framework

func New

func New(opts ...Option) (*CLI, error)

New creates a new CLI framework instance

func (*CLI) AddApis

func (c *CLI) AddApis(apiProviders ...ApiProvider) *CLI

func (*CLI) AddSubcommands

func (c *CLI) AddSubcommands(providers ...SubcommandProvider) *CLI

AddSubcommands mounts subcommand providers to this CLI

func (*CLI) Context

func (c *CLI) Context() *Context

Context returns the CLI context (useful for providers)

func (*CLI) Execute

func (c *CLI) Execute() error

Execute runs the CLI

func (*CLI) Root

func (c *CLI) Root() *cobra.Command

Root returns the root command (useful for testing)

func (*CLI) Subcommand

func (c *CLI) Subcommand(use, short, long string) *CLI

Subcommand creates and returns a new subcommand group

type CanonicalEvent

type CanonicalEvent struct {
	Event   string                 `json:"event"`
	TS      string                 `json:"ts"`
	Source  string                 `json:"source"`
	User    string                 `json:"user"`
	Payload map[string]interface{} `json:"payload"`
}

CanonicalEvent represents the standardized Schemaf event format

type Config

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

Config handles config.toml (static configuration)

func (*Config) Get

func (c *Config) Get(key string) interface{}

Get returns a value from config

func (*Config) GetBool

func (c *Config) GetBool(key string) bool

GetBool returns a bool value from config

func (*Config) GetInt

func (c *Config) GetInt(key string) int

GetInt returns an int value from config

func (*Config) GetString

func (c *Config) GetString(key string) string

GetString returns a string value from config

func (*Config) GetStringSlice

func (c *Config) GetStringSlice(key string) []string

GetStringSlice returns a string slice from config

type Context

type Context struct {
	APIs       *APIRegistry
	Config     *Config
	State      *State
	HTTPClient *HTTPClient
	HomeDir    string
	Verbose    bool
}

Context provides access to framework features

func NewTestContext

func NewTestContext() *Context

NewTestContext creates a Context for testing with in-memory config/state

func (*Context) SetTestConfig

func (ctx *Context) SetTestConfig(key string, value interface{})

SetTestConfig sets config values for testing

func (*Context) SetTestState

func (ctx *Context) SetTestState(key string, value interface{})

SetTestState sets state values for testing

type HTTPClient

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

HTTPClient provides HTTP request utilities

func NewHTTPClient

func NewHTTPClient(verbose bool) *HTTPClient

NewHTTPClient creates a new HTTP client with default settings

func (*HTTPClient) Get

func (c *HTTPClient) Get(url string) (*Response, error)

Get performs a GET request

func (*HTTPClient) Post

func (c *HTTPClient) Post(url string, body interface{}) (*Response, error)

Post performs a POST request with JSON body

func (*HTTPClient) WithHeader

func (c *HTTPClient) WithHeader(key, value string) *HTTPClient

WithHeader sets a header for all requests

func (*HTTPClient) WithTimeout

func (c *HTTPClient) WithTimeout(seconds int) *HTTPClient

WithTimeout sets the HTTP client timeout in seconds

type Option

type Option func(*cliOptions)

Option for functional options pattern

func WithVerbose

func WithVerbose(verbose bool) Option

WithVerbose sets verbose mode

func WithVersion

func WithVersion(version string) Option

WithVersion sets the version string

type Response

type Response struct {
	StatusCode int
	Body       []byte
	Headers    http.Header
}

Response represents an HTTP response

func (*Response) IsSuccess

func (r *Response) IsSuccess() bool

IsSuccess returns true if status code is 2xx

func (*Response) JSON

func (r *Response) JSON(v interface{}) error

JSON unmarshals the response body into v

func (*Response) String

func (r *Response) String() string

String returns the response body as a string

type State

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

State handles state.toml (dynamic runtime state - extensible)

func (*State) Get

func (s *State) Get(key string) interface{}

Get returns a value from state

func (*State) GetString

func (s *State) GetString(key string) string

GetString returns a string value from state

func (*State) Save

func (s *State) Save() error

Save persists state to disk

func (*State) Set

func (s *State) Set(key string, value interface{})

Set sets a value in state

type SubcommandProvider

type SubcommandProvider func(ctx *Context) []*cobra.Command

SubcommandProvider returns a slice of commands to be mounted Providers receive a Context with access to config, state, and utilities

Directories

Path Synopsis
cmd
ctl
Package ctl re-exports the compose resolver types from github.com/flocko-motion/schemaf/compose for use by the CLI subcommands.
Package ctl re-exports the compose resolver types from github.com/flocko-motion/schemaf/compose for use by the CLI subcommands.
run

Jump to

Keyboard shortcuts

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