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

env

package
v1.6.4-upstream Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2026 License: GPL-3.0 Imports: 30 Imported by: 2

Documentation

Index

Constants

View Source
const (
	DdlDefDecLength = 20
	DdlMinDecLength = 24
	DdlMaxDecScale  = 24
	DdlMaxDecLength = 38
	DdlMinDecScale  = 6
)

Variables

View Source
var (
	Marker         = "Sling CLI | https://slingdata.io"
	HomeDir        = os.Getenv("SLING_HOME_DIR")
	HomeDirEnvFile = ""
	Env            = &EnvFile{}
	PlausibleURL   = ""
	SentryDsn      = ""
	NoColor        = g.In(os.Getenv("SLING_LOGGING"), "NO_COLOR", "JSON")
	LogSink        func(*g.LogLine)
	TelMap         = g.M("begin_time", time.Now().UnixMicro())
	TelMux         = sync.Mutex{}

	NoDebugKey    = " /* nD */"
	Executable    = ""
	IsThreadChild = cast.ToBool(os.Getenv("SLING_THREAD_CHILD"))
	ExecID        = g.Getenv("SLING_EXEC_ID", NewExecID())
	RunnerID      = g.Getenv("SLING_RUNNER_ID", os.Getenv("SLING_AGENT_ID"))
	IsRunnerMode  = RunnerID != ""

	GetOAuthMap = func() map[string]map[string]any {
		return map[string]map[string]any{}
	}
	ExecFolder      = func() string { return filepath.Join(HomeDir, "executions", ExecID) }
	QueueFolder     = func() string { return filepath.Join(ExecFolder(), "queues") }
	RuntimeFolder   = func() string { return filepath.Join(ExecFolder(), "runtime") }
	RuntimeFilePath = func(name string) string {
		name = strings.ReplaceAll(name, "\\", "_")
		name = strings.ReplaceAll(name, "/", "_")
		name = strings.ReplaceAll(name, ":", "_")
		os.MkdirAll(RuntimeFolder(), 0755)
		return filepath.Join(RuntimeFolder(), g.F("%s.json", name))
	}

	ReservedFields = struct {
		LoadedAt  string
		SyncedAt  string
		SyncedOp  string
		DeletedAt string
		StreamURL string
		RowNum    string
		RowID     string
		ExecID    string
		CDCSeq    string
	}{
		LoadedAt:  "_sling_loaded_at",
		SyncedAt:  "_sling_synced_at",
		SyncedOp:  "_sling_synced_op",
		DeletedAt: "_sling_deleted_at",
		StreamURL: "_sling_stream_url",
		RowNum:    "_sling_row_num",
		RowID:     "_sling_row_id",
		ExecID:    "_sling_exec_id",
		CDCSeq:    "_sling_cdc_seq",
	}
)
View Source
var ErrStaleEnvFile = errors.New("env.yaml changed since you loaded it; reload and apply again")

ErrStaleEnvFile is returned by EnvFileEditor.Save when the file on disk changed since the editor was loaded (or since the caller last saved). The client reloads and applies its change again.

View Source
var SecretKeys = []string{
	"access_key_id",
	"account_key",
	"aws_access_key_id",
	"aws_secret_access_key",
	"aws_session_token",
	"azure_account_key",
	"azure_sas_token",
	"conn_str",
	"gcp_credentials_json",
	"gcs_secret_access_key",
	"password",
	"rest_oauth_client_secret",
	"rest_token",
	"s3_secret_access_key",
	"s3_session_token",
	"sas_svc_url",
	"sas_token",
	"secret_access_key",
	"session_token",
	"ssh_passphrase",
	"ssh_private_key",
	"token",
}

SecretKeys is the full set of connection property names whose values are secrets. Clean and parse.Redact redact these. Keep it in sync with `secret: true` in core/dbio/templates/_properties.yaml.

View Source
var TemplateKeyOrder func(props map[string]any) []string

TemplateKeyOrder, when set, returns the canonical key order of a new connection entry built from props: `type` first, then the template property order of the entry's type, then the remaining keys. core/dbio registers the order of core/dbio/templates/_properties.yaml; without it new entries get `type` first and the other keys alphabetically.

Functions

func AddChildProc added in v1.6.4

func AddChildProc(p *os.Process)

AddChildProc registers a child process, so KillChildProcs can stop it. Call RemoveChildProc when the process ends.

func BlueString added in v1.1.8

func BlueString(text string) string

func BodySha added in v1.6.4

func BodySha(body string) string

BodySha returns the sha256 hex digest of an env file body. Clients send it back on save so a stale buffer cannot overwrite a newer file.

func Clean added in v1.2.16

func Clean(props map[string]string, line string) string

Clean removes creds from a log line. CREATE/INSERT/etc. still redact: Redshift COPY/UNLOAD embeds keys in those statements.

func CleanConnData added in v1.6.0

func CleanConnData(data map[string]any, line string) string

CleanConnData redacts registry secrets and every nested secrets: value. Nested API keys are secret even when the key name is not in the registry.

func CleanTableName added in v1.2.22

func CleanTableName(tableName string) string

func CleanWindowsPath added in v1.2.19

func CleanWindowsPath(path string) string

func CloseFileLogging added in v1.5.7

func CloseFileLogging()

CloseFileLogging closes any open log files.

func ConnectionEditorEnabled added in v1.6.4

func ConnectionEditorEnabled() bool

ConnectionEditorEnabled reports whether the GUI connection editor is enabled. SLING_DISABLE_CONNECTION_EDITOR=1 turns it off, which also disables the EnvironmentSet removal guard (a client that does not know about allow_removals cannot act on that refusal).

func CyanString added in v1.1.15

func CyanString(text string) string

func DarkGrayString added in v1.1.15

func DarkGrayString(text string) string

func EnvVarRefName added in v1.6.0

func EnvVarRefName(s string) string

EnvVarRefName returns VAR from ${VAR}. It returns "" when s is not a ref.

func ExpandEntry added in v1.6.4

func ExpandEntry(props map[string]any) map[string]any

ExpandEntry expands ${VAR} refs in all string values of props, also inside strings and nested lists and maps, with the same rules as loadEnvFile. It returns a deep copy: the input map is not modified.

func ExpandEnvVars added in v1.5.17

func ExpandEnvVars(text string) string

expandEnvVars replaces $KEY or ${KEY} with its environment variable value only if the variable is present in the environment. If not present, $KEY or ${KEY} will remain in the config text.

func ExpandRef added in v1.6.4

func ExpandRef(s string) string

ExpandRef expands a whole-string ${VAR} against the process environment. An unset var keeps the ref as-is.

func GetEnvFilePath added in v1.2.4

func GetEnvFilePath(dir string) string

func GetHomeDirConnsMap added in v1.2.4

func GetHomeDirConnsMap() (connsMap map[string]map[string]any, err error)

func GetTempFolder added in v1.2.4

func GetTempFolder() string

func GreenString added in v1.1.8

func GreenString(text string) string

func HomeBinDir added in v1.2.4

func HomeBinDir() string

func InitLogger

func InitLogger()

InitLogger initializes the Logger

func IsEnvVarRef added in v1.6.0

func IsEnvVarRef(s string) bool

IsEnvVarRef is true when s is a whole-string ${VAR} reference.

func IsInteractiveTerminal added in v1.4.10

func IsInteractiveTerminal() bool

IsInteractiveTerminal checks if the current process is running in an interactive terminal

func KillChildProcs added in v1.6.4

func KillChildProcs()

KillChildProcs kills all registered child processes. Call it before the process exits.

func LoadDotEnvSling added in v1.5.10

func LoadDotEnvSling() map[string]string

LoadDotEnvSling reads a `.env.sling` file from the current working directory and injects its key=value pairs into os environment variables. Existing env vars are not overwritten.

func LoadDotEnvSlingFrom added in v1.5.16

func LoadDotEnvSlingFrom(dir string) map[string]string

LoadDotEnvSlingFrom reads a `.env.sling` file from the specified directory and injects its key=value pairs into os environment variables. Existing env vars are not overwritten.

func LoadHomeDir added in v1.5.12

func LoadHomeDir()

func LogSQL added in v1.2.16

func LogSQL(props map[string]string, query string, args ...any)

func MagentaString added in v1.2.2

func MagentaString(text string) string

func MergeDeclaredEnv added in v1.6.0

func MergeDeclaredEnv(declared map[string]any) map[string]any

MergeDeclaredEnv starts from the process environment and overlays declared pipeline/replication env keys. Bare {env.X} then renders even when the YAML has no env: block.

func NewExecID added in v1.5.9

func NewExecID() string

func ParseDotEnv added in v1.5.11

func ParseDotEnv(content string) map[string]string

ParseDotEnv parses a .env file content into key-value pairs. It supports single-line and multi-line values enclosed in matching quotes (' or ").

func ParseEnvFileConnections added in v1.6.4

func ParseEnvFileConnections(body string) (map[string]map[string]any, error)

ParseEnvFileConnections parses an env.yaml body into raw connection prop maps, without ${VAR} interpolation.

func ParseEnvFileKeys added in v1.6.4

func ParseEnvFileKeys(body string) (connNames, envKeys []string, err error)

ParseEnvFileKeys parses a raw env.yaml body and returns its connection names and env keys (legacy `variables:` included). No ${VAR} interpolation.

func Print added in v1.1.6

func Print(text string)

func PrintFatal added in v1.6.0

func PrintFatal(E error, args ...interface{})

PrintFatal prints the fatal error (same text as g.PrintFatal) and captures it in the run-log buffer via Println, so stderr.log includes it.

func Println added in v1.1.6

func Println(text string)

func RecentLogs added in v1.6.0

func RecentLogs() string

RecentLogs returns the buffered tail, oldest first.

func RedString added in v1.1.8

func RedString(text string) string

func RemoveAllLocalTempFile added in v1.2.16

func RemoveAllLocalTempFile(localPath string)

RemoveAllLocalTempFile deletes the local folder

func RemoveChildProc added in v1.6.4

func RemoveChildProc(p *os.Process)

RemoveChildProc unregisters a child process.

func RemoveLocalTempFile added in v1.2.16

func RemoveLocalTempFile(localPath string)

RemoveLocalTempFile deletes the local file

func RemovedKeys added in v1.6.4

func RemovedKeys(oldBody, newBody string) ([]string, error)

RemovedKeys lists connections and env vars (prefixed `env.`) present in oldBody but not in newBody. Used to refuse a raw-editor save that would drop credentials the previous body had, unless the client confirms.

func ScrubLine added in v1.6.0

func ScrubLine(line string) string

ScrubLine redacts secrets from every local connection in Env.

func SetLogger added in v1.1.6

func SetLogger()

func SetTelVal added in v1.2.2

func SetTelVal(key string, value any)

func StartLogCapture added in v1.6.0

func StartLogCapture()

StartLogCapture begins buffering log lines for the failure snapshot.

func StopLogCapture added in v1.6.0

func StopLogCapture()

StopLogCapture stops buffering and drops what was kept.

func UnsetEnvKeys added in v1.5.10

func UnsetEnvKeys(keys []string)

func UseDuckDbCompute added in v1.4.24

func UseDuckDbCompute() bool

func ValidateEnvKey added in v1.6.4

func ValidateEnvKey(key string) error

ValidateEnvKey returns an error if key is not a safe environment variable name (^[A-Z_][A-Z0-9_]*$).

func ValidateKey added in v1.6.4

func ValidateKey(key string) error

ValidateKey returns an error if key is not a safe YAML mapping key (^[A-Za-z_][A-Za-z0-9_-]*$, no leading/trailing whitespace).

func Vars added in v1.2.4

func Vars() (vars map[string]string)

Vars are the variables we are using

func YellowString added in v1.4.21

func YellowString(text string) string

Types

type ConnLocation added in v1.6.0

type ConnLocation struct {
	Path       string       `json:"path"`
	Line       int          `json:"line"`
	Connection string       `json:"connection"`
	Missing    []MissingRef `json:"missing"`
}

ConnLocation is a deep-link into env.yaml for one connection.

func LookupConnectionBody added in v1.6.4

func LookupConnectionBody(body, name, path string) (ConnLocation, error)

LookupConnectionBody is LookupConnection against a body string. path is recorded on the result for display only. No ${VAR} interpolation happens.

type EditOptions added in v1.6.4

type EditOptions struct {
	// Replace treats props as the full entry: keys that are not in props are
	// removed, together with their comments. Without it the entry is merged,
	// and keys that props does not pass are kept.
	Replace bool
	// AllowOverwrite is false on create: an existing entry is an error.
	AllowOverwrite bool
	// EnvUpdates, when non-empty, are written under `env:` in the same edit.
	EnvUpdates map[string]any
	// AllowEnvOverwrite permits replacing existing env: values.
	AllowEnvOverwrite bool
}

EditOptions controls EnvFileEditor.Set.

type EnvFile added in v0.86.56

type EnvFile struct {
	Connections map[string]map[string]any `json:"connections,omitempty" yaml:"connections,omitempty"`
	Env         map[string]any            `json:"env,omitempty" yaml:"env,omitempty"`
	Variables   map[string]any            `json:"variables,omitempty" yaml:"variables,omitempty"` // legacy
	Workbench   *WorkbenchConfig          `json:"workbench,omitempty" yaml:"workbench,omitempty"`

	Path       string `json:"-" yaml:"-"`
	Repaired   bool   `json:"-" yaml:"-"` // indentation was repaired on read
	TopComment string `json:"-" yaml:"-"`
	Body       string `json:"-" yaml:"-"`
}

func LoadEnvFile added in v0.86.56

func LoadEnvFile(path string) (ef EnvFile)

func LoadSlingEnvFile added in v0.86.56

func LoadSlingEnvFile() (ef EnvFile)

func LoadSlingEnvFileBody added in v1.2.15

func LoadSlingEnvFileBody(body string) (ef EnvFile, err error)

func (*EnvFile) CheckFile added in v1.6.4

func (ef *EnvFile) CheckFile() error

CheckFile returns an error when the file at ef.Path does not fully parse into EnvFile. A struct write from a partial parse drops the entries that did not parse. A missing or empty file is valid.

func (*EnvFile) ConnectionNames added in v1.6.4

func (ef *EnvFile) ConnectionNames() ([]string, error)

ConnectionNames returns the connection keys present in the raw file at ef.Path, sorted.

func (*EnvFile) DeleteConnectionNode added in v1.6.4

func (ef *EnvFile) DeleteConnectionNode(name string) error

DeleteConnectionNode removes one connection entry and its head comment from the file at ef.Path and saves. Comments after the entry stay.

func (*EnvFile) EnvKeys added in v1.6.4

func (ef *EnvFile) EnvKeys() ([]string, error)

EnvKeys returns the keys under `env:` (legacy `variables:` included) in the raw file at ef.Path, sorted. No ${VAR} interpolation.

func (*EnvFile) LookupConnection added in v1.6.0

func (ef *EnvFile) LookupConnection(name string) (ConnLocation, error)

LookupConnection re-parses ef.Path and returns line numbers for connections.<NAME> and each ${VAR} field under it.

func (*EnvFile) MarshalBody added in v1.5.14

func (ef *EnvFile) MarshalBody() (string, error)

MarshalBody returns the EnvFile as a formatted YAML string

func (*EnvFile) RawConnections added in v1.6.4

func (ef *EnvFile) RawConnections() (map[string]map[string]any, error)

RawConnections parses the file at ef.Path into connection prop maps, without ${VAR} interpolation. Load/ReadConnections expand refs against the process environment; this does not, so callers that hand values to a UI (or write them back) can never observe a resolved secret.

func (*EnvFile) RawEnv added in v1.6.4

func (ef *EnvFile) RawEnv() (map[string]any, error)

RawEnv returns the `env:` values (legacy `variables:` included) from the raw file at ef.Path, without ${VAR} interpolation.

func (*EnvFile) SetConnectionNode added in v1.6.4

func (ef *EnvFile) SetConnectionNode(name string, props map[string]any, envUpdates map[string]any) error

SetConnectionNode merges props into one connection entry of the file at ef.Path and saves, through EnvFileEditor: only the changed lines change. It never reads ef.Connections, so expanded ${VAR} values held in the struct cannot leak to disk.

envUpdates, when non-empty, are written under `env:` in the same save (one atomic write; used for secret promotion). Existing env values are replaced: callers that must protect hand-set values (EnvFileConns.SetValidated) check first.

func (*EnvFile) SetEnvNodes added in v1.6.4

func (ef *EnvFile) SetEnvNodes(updates map[string]any, allowOverwrite bool) error

SetEnvNodes sets keys under the `env:` block (legacy `variables:` when `env:` is absent) of the file at ef.Path and saves. Existing keys are only replaced when allowOverwrite is true or the value is unchanged.

func (*EnvFile) WriteEnvFile added in v1.2.4

func (ef *EnvFile) WriteEnvFile() (err error)

type EnvFileEditor added in v1.6.4

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

EnvFileEditor edits one env.yaml as text. The parsed tree only tells where an entry is; an edit then changes the lines of that entry and nothing else. Comments, blank lines, indentation, quotes and key order of all other lines stay byte for byte.

func LoadEnvEditor added in v1.6.4

func LoadEnvEditor(path string) (*EnvFileEditor, error)

LoadEnvEditor loads the file at path for editing. A missing or empty file yields an empty document, so a Set creates it.

func LoadEnvEditorBytes added in v1.6.4

func LoadEnvEditorBytes(path string, b []byte) (*EnvFileEditor, error)

LoadEnvEditorBytes loads body as the content of path. The bytes do not need to be on disk yet; Save re-reads the disk file for the sha check.

func (*EnvFileEditor) Bytes added in v1.6.4

func (e *EnvFileEditor) Bytes() ([]byte, error)

Bytes returns the edited file.

func (*EnvFileEditor) Delete added in v1.6.4

func (e *EnvFileEditor) Delete(name string) error

Delete removes one connection entry and the comment right above it. Comments that follow the entry stay. Deleting the only entry leaves `connections: {}`.

func (*EnvFileEditor) Get added in v1.6.4

func (e *EnvFileEditor) Get(name string) (props map[string]any, loc ConnLocation, found bool)

Get returns the raw props of one connection: refs are not expanded, so a secret on disk stays ${VAR}. The location carries the entry line and the ${VAR} fields that reference an env var.

func (*EnvFileEditor) Names added in v1.6.4

func (e *EnvFileEditor) Names() []string

Names returns the connection names in the file, sorted.

func (*EnvFileEditor) Path added in v1.6.4

func (e *EnvFileEditor) Path() string

Path is the file the editor writes on Save.

func (*EnvFileEditor) Rename added in v1.6.4

func (e *EnvFileEditor) Rename(oldName, newName string) error

Rename re-keys one entry. Only the key text changes: the entry keeps its position, its value and its comments. Renaming does not touch the promoted `env:` keys, so the ${VAR} refs of the entry stay valid.

func (*EnvFileEditor) Save added in v1.6.4

func (e *EnvFileEditor) Save(expectSha string) error

Save replaces the file atomically: the bytes land in a temp file in the same folder, are fsynced, get the mode of the original file, and rename over it. A non-empty expectSha must match the sha256 of the file as it is on disk right now, or the save fails with ErrStaleEnvFile and the file is left untouched.

func (*EnvFileEditor) Set added in v1.6.4

func (e *EnvFileEditor) Set(name string, props map[string]any, opts EditOptions) error

Set creates or updates one connection entry. A new entry goes after the last entry of `connections:`. An update changes only the lines of the keys whose value changes; with opts.Replace it also removes the keys that props does not pass. opts.EnvUpdates go under `env:` in the same edit, so one Save carries the entry and its promoted secrets together. Nothing is written until Save runs.

func (*EnvFileEditor) SetEnv added in v1.6.4

func (e *EnvFileEditor) SetEnv(updates map[string]any, allowOverwrite bool) error

SetEnv writes keys under the `env:` block (legacy `variables:` when `env:` is absent). Nothing is written until Save runs.

func (*EnvFileEditor) Sha added in v1.6.4

func (e *EnvFileEditor) Sha() string

Sha returns the sha256 of the body the editor loaded.

type MissingRef added in v1.6.0

type MissingRef struct {
	Key  string `json:"key"`
	Var  string `json:"var"`
	Line int    `json:"line"`
}

MissingRef is one ${VAR} field that still needs a value (or an env var).

type WorkbenchConfig added in v1.6.4

type WorkbenchConfig struct {
	// Host is the listen address. The default is 127.0.0.1.
	Host string `json:"host,omitempty" yaml:"host,omitempty"`
	// Port is the listen port. The default is 7879; 0 picks a free port.
	Port int `json:"port,omitempty" yaml:"port,omitempty"`
	// Token is required when Host is not loopback.
	Token string `json:"token,omitempty" yaml:"token,omitempty"`
	// ProjectsRoot limits the Open-folder dialog to one folder tree.
	ProjectsRoot string `json:"projects_root,omitempty" yaml:"projects_root,omitempty"`
	// Shell allows interactive shell terminals. The default is true.
	Shell *bool `json:"shell,omitempty" yaml:"shell,omitempty"`
	// WorkerIdle stops a project worker that has no sessions and no running
	// work after this duration, for example "15m". The default is 15m.
	WorkerIdle string `json:"worker_idle,omitempty" yaml:"worker_idle,omitempty"`
	// PathExtra is prepended to PATH for shells, runs and agent CLIs, so a
	// launchd or systemd service finds the tools the user's shell does.
	PathExtra string `json:"path_extra,omitempty" yaml:"path_extra,omitempty"`
	// Env holds extra environment variables for workers.
	Env map[string]any `json:"env,omitempty" yaml:"env,omitempty"`
}

WorkbenchConfig is the `workbench:` block of env.yaml: the settings of `sling serve workbench`. A command-line flag wins over the file.

Jump to

Keyboard shortcuts

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