context

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package context provides kubectl-style context management for muster CLI.

This package enables users to manage multiple muster endpoints similar to how kubectl handles kubeconfig contexts. Users can define named contexts pointing to different muster aggregator servers and switch between them without needing to specify --endpoint for every command.

Configuration File

Contexts are stored in ~/.config/muster/contexts.yaml with the following schema:

current-context: production
contexts:
  - name: local
    endpoint: http://localhost:8090/mcp
  - name: production
    endpoint: https://muster.example.com/mcp
    settings:
      output: table

Usage

The package provides CRUD operations for contexts:

  • Add/update contexts with AddContext
  • Remove contexts with DeleteContext
  • List contexts with ListContexts
  • Get current context with GetCurrentContext
  • Switch contexts with SetCurrentContext

Precedence

When determining which endpoint to use, muster checks in this order:

  1. --endpoint flag (highest priority)
  2. --context flag
  3. MUSTER_CONTEXT environment variable
  4. current-context from contexts.yaml
  5. Local fallback (http://localhost:8090/mcp)

Concurrency

Storage operations are thread-safe within a single process using a read-write mutex. However, concurrent access from multiple processes (e.g., running multiple muster commands simultaneously) is not protected. In practice, this is rarely an issue as context modifications are infrequent user-initiated actions, but users should avoid running concurrent context add/delete/rename operations.

Index

Constants

View Source
const ContextEnvVar = "MUSTER_CONTEXT"

ContextEnvVar is the environment variable name for overriding the current context.

Variables

This section is empty.

Functions

func ValidateContextName

func ValidateContextName(name string) error

ValidateContextName validates a context name according to the naming rules. Context names must:

  • Be between 1 and 63 characters
  • Contain only lowercase letters, numbers, and hyphens
  • Start and end with an alphanumeric character

Returns an error if the name is invalid.

Types

type Context

type Context struct {
	// Name is the unique identifier for this context
	Name string `yaml:"name"`
	// Endpoint is the full URL to the muster aggregator (e.g., http://localhost:8090/mcp)
	Endpoint string `yaml:"endpoint"`
	// Settings contains optional context-specific settings
	Settings *ContextSettings `yaml:"settings,omitempty"`
}

Context represents a named muster endpoint configuration. Each context provides a convenient alias for a muster aggregator URL.

type ContextConfig

type ContextConfig struct {
	// CurrentContext is the name of the currently active context
	CurrentContext string `yaml:"current-context,omitempty"`
	// Contexts is the list of all defined contexts
	Contexts []Context `yaml:"contexts,omitempty"`
}

ContextConfig represents the complete contexts configuration file. This is the root structure stored in ~/.config/muster/contexts.yaml.

func (*ContextConfig) AddOrUpdateContext

func (c *ContextConfig) AddOrUpdateContext(ctx Context)

AddOrUpdateContext adds a new context or updates an existing one. If a context with the same name exists, it will be replaced.

func (*ContextConfig) GetContext

func (c *ContextConfig) GetContext(name string) *Context

GetContext returns the context with the given name, or nil if not found.

func (*ContextConfig) HasContext

func (c *ContextConfig) HasContext(name string) bool

HasContext returns true if a context with the given name exists.

func (*ContextConfig) RemoveContext

func (c *ContextConfig) RemoveContext(name string) bool

RemoveContext removes the context with the given name. Returns true if the context was found and removed, false otherwise. If the removed context was the current context, CurrentContext is cleared.

type ContextNotFoundError

type ContextNotFoundError struct {
	Name string
}

ContextNotFoundError indicates a requested context does not exist.

func (*ContextNotFoundError) Error

func (e *ContextNotFoundError) Error() string

type ContextSettings

type ContextSettings struct {
	// Output is the default output format for this context (table, json, yaml)
	Output string `yaml:"output,omitempty"`
}

ContextSettings contains optional per-context settings. These settings override global defaults when using a specific context.

type Storage

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

Storage provides thread-safe access to the contexts configuration file. It handles loading, saving, and manipulating the contexts.yaml file.

func NewStorage

func NewStorage() (*Storage, error)

NewStorage creates a new Storage instance using the default config path. The default path is ~/.config/muster/contexts.yaml.

func (*Storage) AddContext

func (s *Storage) AddContext(name, endpoint string, settings *ContextSettings) error

AddContext adds a new context with the given name and endpoint. Returns an error if a context with the same name already exists.

func (*Storage) DeleteContext

func (s *Storage) DeleteContext(name string) error

DeleteContext removes a context by name. Returns an error if the context doesn't exist.

func (*Storage) GetContext

func (s *Storage) GetContext(name string) (*Context, error)

GetContext returns the context with the given name. Returns nil if the context doesn't exist.

func (*Storage) GetContextNames

func (s *Storage) GetContextNames() ([]string, error)

GetContextNames returns a list of all context names for shell completion.

func (*Storage) GetCurrentContext

func (s *Storage) GetCurrentContext() (*Context, error)

GetCurrentContext returns the currently selected context. If no current context is set or the context doesn't exist, returns nil.

func (*Storage) GetCurrentContextName

func (s *Storage) GetCurrentContextName() (string, error)

GetCurrentContextName returns the name of the currently selected context. Returns an empty string if no context is selected.

func (*Storage) Load

func (s *Storage) Load() (*ContextConfig, error)

Load reads and parses the contexts configuration file. If the file doesn't exist, an empty ContextConfig is returned.

func (*Storage) RenameContext

func (s *Storage) RenameContext(oldName, newName string) error

RenameContext renames a context from oldName to newName. Returns an error if the old context doesn't exist or the new name already exists.

func (*Storage) SetCurrentContext

func (s *Storage) SetCurrentContext(name string) error

SetCurrentContext sets the current context to the given name. Returns an error if the context doesn't exist.

func (*Storage) UpdateContext

func (s *Storage) UpdateContext(name, endpoint string, settings *ContextSettings) error

UpdateContext updates an existing context. Returns an error if the context doesn't exist.

Jump to

Keyboard shortcuts

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