Help us shape the future of Go! Take the 2026 Go Developer Survey

config

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package config turns a Config file into the Upstream definitions it declares.

Index

Constants

This section is empty.

Variables

View Source
var ErrExists = errors.New("already in the config")

ErrExists is the error of adding an Upstream whose name is taken.

View Source
var ErrNotFound = errors.New("not in the config")

ErrNotFound is the error of naming an Upstream the Config doesn't have.

Functions

func Parse

func Parse(data []byte, lookupEnv LookupEnv) ([]Upstream, []Warning, error)

Parse parses Config bytes into the Upstreams to start, in Config order, plus warnings about the Config. lookupEnv resolves ${VAR} references.

func ParseEntries added in v0.3.0

func ParseEntries(data []byte, lookupEnv LookupEnv) ([]Entry, []Warning, error)

ParseEntries parses Config bytes into all of its entries, in Config order, plus warnings about the Config. Like Parse, it fails on an invalid entry unless the entry is disabled, and it warns about no disabled entry.

func Path

func Path(flag string, lookupEnv LookupEnv) (path string, named bool, err error)

Path resolves which Config file to read: flag (from -c/--config), then $SPRUT_CONFIG, then $XDG_CONFIG_HOME/sprut/config.json, then ~/.config/sprut/config.json. It reports whether the path was named, by flag or $SPRUT_CONFIG, rather than being the default location.

func XDGDir added in v0.2.0

func XDGDir(lookupEnv LookupEnv, variable, fallback string) (string, bool)

XDGDir returns the base directory that the XDG variable names, or the fallback under $HOME that the XDG spec gives for it. It reports false if neither is set.

Types

type Config added in v0.3.0

type Config struct {
	// Path is the Config's absolute path.
	Path     string
	Entries  []Entry
	Warnings []Warning
	// contains filtered or unexported fields
}

Config is a Config file as sprut opened it: all of its entries, enabled or not, in Config order.

func Edit added in v0.3.0

func Edit(flag string, lookupEnv LookupEnv, edit func(*Document) (bool, error)) (*Config, error)

Edit applies edit to the Config that flag and lookupEnv point at, as Open finds it, and writes the result back with writeFile, returning the Config as written. A missing Config is edited as an empty one, and so created; a Config that fails to parse is refused, and so is a result that does, leaving the file as it was. edit reports whether it changed the Config, and must leave it as it was if not: then the file is left as it is and Edit returns the Config as opened.

func Open added in v0.3.0

func Open(flag string, lookupEnv LookupEnv) (*Config, error)

Open reads and parses the Config that flag (from -c/--config) and lookupEnv point at, as Path resolves it; lookupEnv also resolves ${VAR} references. Unless the Config is at the default location, each resolved Upstream carries its absolute path as ConfigPath: the Agent may run sprut with -c or $SPRUT_CONFIG that the user's shell lacks, and in another directory.

Reading a missing Config fails with an fs.ErrNotExist, and still returns the Config as empty, at its path, so that an edit can create it.

func (*Config) Lookup added in v0.3.0

func (c *Config) Lookup(name string) (Entry, error)

Lookup returns the entry called name, enabled or not. It fails with ErrNotFound if the Config has none.

func (*Config) Upstream added in v0.3.0

func (c *Config) Upstream(name string) (Upstream, error)

Upstream returns the entry called name resolved, enabled or not, so that its Credentials can be found. It fails as Lookup does, or with why the entry doesn't resolve.

func (*Config) Upstreams added in v0.3.0

func (c *Config) Upstreams() []Upstream

Upstreams returns the Upstreams that the Gateway starts: those neither disabled nor skipped, in Config order.

type Definition added in v0.3.0

type Definition struct {
	Command string            `json:"command,omitempty"`
	Args    []string          `json:"args,omitempty"`
	Env     map[string]string `json:"env,omitempty"`
	URL     string            `json:"url,omitempty"`
	Headers map[string]string `json:"headers,omitempty"`
	OAuth   *OAuth            `json:"oauth,omitempty"`
	// Disabled is written only when true: "disabled": false is the same as
	// none.
	Disabled bool `json:"disabled,omitempty"`
}

Definition is an Upstream as the user writes it into the Config, with ${VAR} references left in. Its JSON is the entry as written.

func (Definition) Transport added in v0.3.0

func (def Definition) Transport() Transport

Transport is the Transport the parser infers for the entry def writes, or "" if it sets both "command" and "url", or neither.

type Document added in v0.3.0

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

Document is a Config as the user wrote it, to edit and write back. It keeps every entry's JSON, the order of entries and the keys sprut doesn't know; writing it back changes only their layout.

func ParseDocument added in v0.3.0

func ParseDocument(data []byte) (*Document, error)

ParseDocument parses Config bytes into a Document. It fails where ParseEntries would on the Config's shape, but checks no entry.

func (*Document) Add added in v0.3.0

func (d *Document) Add(name string, def Definition, replace bool) error

Add adds the Upstream name, defined by def, after the others. If the name is taken, Add fails with ErrExists, or replaces that entry in its place when replace is set.

func (*Document) Bytes added in v0.3.0

func (d *Document) Bytes() []byte

Bytes returns the Config as JSON indented by two spaces, ending in a newline.

func (*Document) Entries added in v0.3.0

func (d *Document) Entries(lookupEnv LookupEnv) ([]Entry, []Warning, error)

Entries is ParseEntries of the Config d is, as edited so far.

func (*Document) Remove added in v0.3.0

func (d *Document) Remove(name string) error

Remove removes the Upstream name, keeping the others in their order. It fails if there is no Upstream of that name.

func (*Document) SetDisabled added in v0.3.0

func (d *Document) SetDisabled(name string, disabled bool) (bool, error)

SetDisabled switches the Upstream name off, writing "disabled": true, or on, removing "disabled" rather than writing false. An Upstream already switched as asked is left as it is, and SetDisabled reports whether it changed the entry. It fails if there is no Upstream of that name, or its entry is not a JSON object.

type Entry added in v0.3.0

type Entry struct {
	Name string
	// Transport, Command, Args and URL are as the entry writes them, with
	// ${VAR} references left in. Transport is "" when the entry neither
	// names one nor sets the one target it could be inferred from.
	Transport Transport
	Command   string
	Args      []string
	URL       string

	Disabled bool
	// Skipped says why the Gateway skips the entry although it is enabled,
	// or is "".
	Skipped string
	// Unresolved says why the entry, disabled, would be invalid or skipped
	// if it were enabled, or is "".
	Unresolved string
	// Upstream is the entry resolved, or nil when it is skipped or
	// Unresolved.
	Upstream *Upstream
}

Entry is one entry of the Config, whether or not the Gateway starts it.

func (Entry) Starts added in v0.3.0

func (e Entry) Starts() bool

Starts reports whether the Gateway starts the entry: it is neither disabled nor skipped.

type LookupEnv

type LookupEnv func(string) (string, bool)

LookupEnv looks up an environment variable, like os.LookupEnv.

func LookupIn

func LookupIn(env []string) LookupEnv

LookupIn returns a lookup over env, which is in os.Environ form. As with a real environment, the last entry for a key wins.

type OAuth added in v0.2.0

type OAuth struct {
	// A pre-registered client, in place of Dynamic Client Registration.
	ClientID     string `json:"clientId,omitempty"`
	ClientSecret string `json:"clientSecret,omitempty"`
	// The scopes to request, in place of those the Upstream asks for; nil
	// when the Config has none.
	Scopes []string `json:"scopes,omitempty"`
}

OAuth is how sprut logs in to an OAuth Upstream, where the Config says. Its JSON is the Config's "oauth".

type Transport

type Transport string

Transport is how the Gateway talks to an Upstream.

const (
	Stdio Transport = "stdio"
	HTTP  Transport = "http"
)

type Upstream

type Upstream struct {
	Name      string
	Transport Transport

	// stdio
	Command string
	Args    []string
	Env     map[string]string // layered on top of the inherited environment

	// HTTP
	URL     string
	Headers map[string]string
	OAuth   *OAuth // nil when the Config has no "oauth"

	// ConfigPath is the absolute path of the Config file, which a command
	// run from another shell needs with -c to find it, or "" when it is
	// where sprut looks by default. Parse leaves it empty.
	ConfigPath string
}

Upstream is one resolved Upstream definition from the Config.

type Warning

type Warning struct {
	Upstream string
	Message  string
}

Warning is a problem in the Config that does not stop the Gateway.

Jump to

Keyboard shortcuts

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