Documentation
¶
Overview ¶
Package config turns a Config file into the Upstream definitions it declares.
Index ¶
- Variables
- func Parse(data []byte, lookupEnv LookupEnv) ([]Upstream, []Warning, error)
- func ParseEntries(data []byte, lookupEnv LookupEnv) ([]Entry, []Warning, error)
- func Path(flag string, lookupEnv LookupEnv) (path string, named bool, err error)
- func XDGDir(lookupEnv LookupEnv, variable, fallback string) (string, bool)
- type Config
- type Definition
- type Document
- type Entry
- type LookupEnv
- type OAuth
- type Transport
- type Upstream
- type Warning
Constants ¶
This section is empty.
Variables ¶
var ErrExists = errors.New("already in the config")
ErrExists is the error of adding an Upstream whose name is taken.
var ErrNotFound = errors.New("not in the config")
ErrNotFound is the error of naming an Upstream the Config doesn't have.
Functions ¶
func Parse ¶
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
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.
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
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
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
Lookup returns the entry called name, enabled or not. It fails with ErrNotFound if the Config has none.
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
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
Bytes returns the Config as JSON indented by two spaces, ending in a newline.
func (*Document) Entries ¶ added in v0.3.0
Entries is ParseEntries of the Config d is, as edited so far.
func (*Document) Remove ¶ added in v0.3.0
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
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.
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 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.