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
- func CollectionOwner(key string) string
- func EnvironmentOwner(key string) string
- func GlobalsPath() (string, error)
- func Persist(vars []format.Variable, current map[string]string) ([]format.Variable, error)
- func SecretKey(folder, owner, name string) string
- type Dynamic
- type Entry
- type Globals
- type Input
- type Resolver
- type Result
- type Scope
- type Secrets
- type Source
- type State
- type Values
- func (v *Values) All(owner string) map[string]string
- func (v *Values) DeleteSecrets(ctx context.Context, owner string, names ...string) error
- func (v *Values) Get(owner, name string) (string, bool)
- func (v *Values) Reset(ctx context.Context, owner, name string, secret bool) error
- func (v *Values) ResetOwner(owner string) error
- func (v *Values) Secret(ctx context.Context, owner, name string) (secret string, found bool, err error)
- func (v *Values) Set(ctx context.Context, owner, name, value string, secret bool) error
Constants ¶
const ( ScopeEnvironment = "environment" ScopeCollection = "collection" )
The scopes that have current values (an extraction rule's "scope"; the globals have one value, which is theirs).
const (
// GlobalOwner owns the globals.
GlobalOwner = "global"
)
The owners of variables, which keep the secrets of two places apart.
const GlobalsFileName = "httpclient-globals.json"
GlobalsFileName is the file of the globals in $XDG_STATE_HOME/emetos.
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 ¶
CollectionOwner is the owner of the variables of the collection whose key is key (its folder name).
func EnvironmentOwner ¶
EnvironmentOwner is the owner of the variables of the environment whose key is key (the file name without its extension).
func GlobalsPath ¶
GlobalsPath is $XDG_STATE_HOME/emetos/httpclient-globals.json. The error has the tag StateFileDirUnknown.
func Persist ¶
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.
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 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 ¶
NewGlobals makes the globals kept in the file at path, with the secrets in secrets (nil: no keyring).
func (*Globals) Load ¶
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.
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 ¶
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.
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 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 ¶
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) DeleteSecrets ¶
DeleteSecrets removes the secrets of the variables from the keyring (the owner was deleted, or the variables were).
func (*Values) Get ¶
Get returns the current value of the variable of the owner, and whether it has one.
func (*Values) Reset ¶
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 ¶
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 ¶
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.