variables

package
v1.4.3 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: MIT Imports: 15 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package variables resolves the {{name}} of the HTTP client (SPEC §10.33 "Variables"): the order in which the places that define a name are looked at, the dynamic variables ({{$guid}}, {{$processEnv NAME}}), the current values that only this computer has (kept in Emetos's state, never in the project), the globals of the user, and the secrets, which are in the desktop keyring and never in a file (D45).

The order, from the strongest: the values extracted by the request in this run (locals), the current and then the initial values of the environment, the folders of the request (the innermost first), the current and then the initial values of the collection, and the globals. A name that is found nowhere stays as it is in the text and is reported.

Index

Constants

View Source
const (
	ScopeEnvironment = "environment"
	ScopeCollection  = "collection"
)

The scopes that have current values (an extraction rule's "scope"; the globals have one value, which is theirs).

View Source
const (
	// GlobalOwner owns the globals.
	GlobalOwner = "global"
)

The owners of variables, which keep the secrets of two places apart.

View Source
const GlobalsFileName = "httpclient-globals.json"

GlobalsFileName is the file of the globals in $XDG_STATE_HOME/emetos.

View Source
const StateKey = "httpclient.currentValues"

StateKey is the key of the current values in the state of the workspace.

Variables

This section is empty.

Functions

func CollectionOwner

func CollectionOwner(key string) string

CollectionOwner is the owner of the variables of the collection whose key is key (its folder name).

func EnvironmentOwner

func EnvironmentOwner(key string) string

EnvironmentOwner is the owner of the variables of the environment whose key is key (the file name without its extension).

func GlobalsPath

func GlobalsPath() (string, error)

GlobalsPath is $XDG_STATE_HOME/emetos/httpclient-globals.json. The error has the tag StateFileDirUnknown.

func Persist

func Persist(vars []format.Variable, current map[string]string) ([]format.Variable, error)

Persist returns the variables with the current values written as their values ("Persist" of SPEC §10.33); the caller saves them into the file. A variable without a current value keeps its value. It fails with HttpPersistSecret when a variable that has a current value is a secret: a secret is never written to a file.

func SecretKey

func SecretKey(folder, owner, name string) string

SecretKey is the key of the secret of a variable in the keyring: the workspace folder, the owner and the name (SPEC §10.33).

Types

type Dynamic

type Dynamic struct {
	// Now is the clock; nil is time.Now.
	Now func() time.Time
	// Rand is the source of random bytes; nil is crypto/rand.
	Rand io.Reader
	// Getenv reads the process environment; nil is os.Getenv.
	Getenv func(string) string
	// AllowedProcessEnv are the names {{$processEnv NAME}} may read (the setting httpClient.allowedProcessEnv, empty by default, so a
	// collection cannot read the environment of the user unasked).
	AllowedProcessEnv []string
}

Dynamic gives the dynamic variables their values. The zero value uses the clock, the system's random source and the process environment; the tests replace them.

type Entry

type Entry struct {
	Name    string
	Value   string
	Enabled bool
	Secret  bool
}

Entry is a global variable. The value of a secret is in the keyring.

type Globals

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

Globals are the variables shared between projects (SPEC §10.33): they are in the state of the user, not in any project. The file has mode 0600 and no secret value.

func NewGlobals

func NewGlobals(path string, secrets Secrets) *Globals

NewGlobals makes the globals kept in the file at path, with the secrets in secrets (nil: no keyring).

func (*Globals) Load

func (g *Globals) Load(ctx context.Context) (entries []Entry, missing []string, err error)

Load reads the globals. The value of a secret comes from the keyring; missing lists the names of secrets it has none for. A file that does not exist is no globals. The errors have the tags of statefile.Read, GlobalsFileInvalid and GlobalsFileVersion, and are those of the keyring.

func (*Globals) Save

func (g *Globals) Save(ctx context.Context, entries []Entry) error

Save writes the globals. The value of a secret goes to the keyring (and the secrets that are no longer globals are removed from it); the file has none. It fails with HttpSecretsUnavailable for a secret when there is no keyring, and with the tag StateFileWriteFailed.

type Input

type Input struct {
	// Locals are the values extracted by the request in this run.
	Locals map[string]string
	// Environment is the active environment, or nil.
	Environment *Scope
	// Folders are the folders of the request, the outermost first.
	Folders []Scope
	// Collection is the collection of the request, or nil.
	Collection *Scope
	// Globals are the variables of the user.
	Globals []Entry
	// Values gives the current values and the secrets of the environment and the collection; nil: only the initial values.
	Values *Values
	// Dynamic gives the dynamic variables.
	Dynamic Dynamic
}

Input is what a request resolves against.

type Resolver

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

Resolver resolves text against the variables it was built with. It does no I/O (Build read the secrets), so it may be used from any goroutine.

func Build

func Build(ctx context.Context, in Input) (*Resolver, error)

Build reads the current values and the secrets of the input and makes the Resolver. A keyring that fails is the error: the resolver is still returned and holds every value that could be read; the secrets that could not are missing.

func (*Resolver) Lookup

func (r *Resolver) Lookup(name string) (value string, source Source, ok bool)

Lookup returns the value a name has and where it came from.

func (*Resolver) Resolve

func (r *Resolver) Resolve(text string) Result

Resolve replaces the {{name}} of the text. A value that has {{names}} itself is resolved too (to a depth of ten; a name that leads back to itself stays unresolved). Dynamic variables are replaced by a new value at each place.

type Result

type Result struct {
	// Text is the text, with every name that was found replaced and the others left as they were ({{name}}).
	Text string
	// Unresolved are the names that were not found (in the order of the text, once each); a dynamic variable that is unknown or
	// not allowed is listed with its "$".
	Unresolved []string
	// MissingSecrets are the names of secret variables that the keyring has no value for: the caller asks the user.
	MissingSecrets []string
}

Result is the text after the variables in it were replaced.

type Scope

type Scope struct {
	// Owner is the owner of its current values and secrets (EnvironmentOwner, CollectionOwner); empty for a folder, whose
	// variables have no current value.
	Owner string
	// Name is shown with the source ("Dev", "Users").
	Name      string
	Variables []format.Variable
}

Scope is a place that defines variables: an environment, a collection or a folder.

type Secrets

type Secrets interface {
	// Store keeps the secret under the key, replacing the one it had. label is what the keyring's own tools show.
	Store(ctx context.Context, key, label, secret string) error
	// Lookup returns the secret; found is false when there is none.
	Lookup(ctx context.Context, key string) (secret string, found bool, err error)
	// Delete removes the secret; one that is not there is not an error.
	Delete(ctx context.Context, key string) error
}

Secrets is where the values of secret variables live: the desktop keyring. dbconn.Keyring has the same methods, so the SecretService and the MemoryKeyring of the database extensions are Secrets.

type Source

type Source string

Source says where a variable's value came from.

const (
	SourceLocal              Source = "local"
	SourceEnvironmentCurrent Source = "environment current value"
	SourceEnvironmentInitial Source = "environment"
	SourceFolder             Source = "folder"
	SourceCollectionCurrent  Source = "collection current value"
	SourceCollectionInitial  Source = "collection"
	SourceGlobal             Source = "global"
)

The places a value comes from, in the order they win.

type State

type State interface {
	Get(key string) (json.RawMessage, bool)
	Set(key string, value any) error
}

State is the part of extension.State the current values use.

type Values

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

Values keeps the current values (SPEC §10.33 "Current values"): the value a variable has on this computer, apart from the initial value in its file. A current value is in the state of the workspace (Emetos's state, not the project). The current value of a secret variable is its secret: it goes to the keyring and nowhere else.

func NewValues

func NewValues(folder string, state State, secrets Secrets) *Values

NewValues makes the current values of the workspace folder, kept in state, with the secrets in secrets (nil: secrets are not kept, and their reads find nothing).

func (*Values) All

func (v *Values) All(owner string) map[string]string

All returns the current values of the owner (a copy).

func (*Values) DeleteSecrets

func (v *Values) DeleteSecrets(ctx context.Context, owner string, names ...string) error

DeleteSecrets removes the secrets of the variables from the keyring (the owner was deleted, or the variables were).

func (*Values) Get

func (v *Values) Get(owner, name string) (string, bool)

Get returns the current value of the variable of the owner, and whether it has one.

func (*Values) Reset

func (v *Values) Reset(ctx context.Context, owner, name string, secret bool) error

Reset takes the current value of the variable away ("Reset" puts the initial value back); for a secret it removes the secret.

func (*Values) ResetOwner

func (v *Values) ResetOwner(owner string) error

ResetOwner takes every current value of the owner away (not the secrets).

func (*Values) Secret

func (v *Values) Secret(ctx context.Context, owner, name string) (secret string, found bool, err error)

Secret returns the secret of a variable; found is false when the keyring has none.

func (*Values) Set

func (v *Values) Set(ctx context.Context, owner, name, value string, secret bool) error

Set makes value the current value of the variable of the owner. For a secret variable it is stored in the keyring (the state gets nothing). The state's errors (ExtensionStateInvalid, ...) and the keyring's are returned as they are; it fails with HttpSecretsUnavailable for a secret when there is no keyring.

Jump to

Keyboard shortcuts

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