api

package
v0.1.0-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package api provides helpers for initializing and using the StackEye API client.

This package centralizes the pattern of creating an SDK client from the CLI configuration, providing consistent error handling across all commands that need API access.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrConfigNotLoaded is returned when GetClient is called before config is loaded.
	ErrConfigNotLoaded = errors.New("api: configuration not loaded")

	// ErrNoCurrentContext is returned when no context is set in the configuration.
	ErrNoCurrentContext = errors.New("api: no context configured")

	// ErrContextNotFound is returned when the current context doesn't exist.
	ErrContextNotFound = errors.New("api: context not found")

	// ErrNoAPIKey is returned when the context has no API key configured.
	ErrNoAPIKey = errors.New("api: no API key configured for context")
)

Error types for API client initialization failures.

Functions

func GetClient

func GetClient() (*client.Client, error)

GetClient creates and returns an SDK client from the current configuration context.

The function:

  1. Gets the loaded configuration via the configured getter
  2. Retrieves the current context
  3. Validates that an API key is present
  4. Creates and returns an SDK client

Returns an error if:

  • Configuration is not loaded (ErrConfigNotLoaded)
  • No current context is set (ErrNoCurrentContext)
  • Current context doesn't exist (ErrContextNotFound)
  • No API key is configured (ErrNoAPIKey)

Example:

client, err := api.GetClient()
if err != nil {
    return fmt.Errorf("failed to initialize API client: %w", err)
}
// Use client for API calls

func GetClientOptions

func GetClientOptions() []client.Option

GetClientOptions returns SDK client options for use by commands that create clients directly (e.g., login, config set-api-key). This allows them to use verbosity logging without going through GetClient().

Example:

opts := api.GetClientOptions()
c := client.New(apiKey, apiURL, opts...)

func GetClientWithContext

func GetClientWithContext(contextName string) (*client.Client, error)

GetClientWithContext creates an SDK client for a specific named context. This is useful for commands that need to operate on a different context than the current one.

Returns the same errors as GetClient, but for the specified context name.

func RequireClient

func RequireClient() *client.Client

RequireClient creates and returns an SDK client, exiting on error.

This is a convenience function for commands that require authentication. If the client cannot be created, it prints an error message to stderr and exits with code 1.

Example:

func runMyCommand() error {
    client := api.RequireClient()
    // Use client for API calls
}

func SetConfigGetter

func SetConfigGetter(getter func() *config.Config)

SetConfigGetter sets the function used to get the loaded configuration. This should be called during CLI initialization to wire up the config loader.

Example usage in root.go init():

api.SetConfigGetter(GetConfig)

func SetVerbosityGetter

func SetVerbosityGetter(getter func() int)

SetVerbosityGetter sets the function used to get the verbosity level. This should be called during CLI initialization to wire up the verbosity flag.

Example usage in root.go init():

api.SetVerbosityGetter(GetVerbosity)

Types

This section is empty.

Jump to

Keyboard shortcuts

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