MCPcopy Index your code
hub / github.com/jetify-com/devbox / configEnvs

Method configEnvs

internal/devbox/devbox.go:998–1055  ·  view source on GitHub ↗

configEnvs takes the existing environment (nix + plugin) and adds env variables defined in Config. It also parses variables in config that are referenced by $VAR or ${VAR} and replaces them with their value in the existing env variables. Note, this doesn't allow env variables from outside the shell

(
	ctx context.Context,
	existingEnv map[string]string,
)

Source from the content-addressed store, hash-verified

996// allow env variables from outside the shell to be referenced so
997// no leaked variables are caused by this function.
998func (d *Devbox) configEnvs(
999 ctx context.Context,
1000 existingEnv map[string]string,
1001) (map[string]string, error) {
1002 defer debug.FunctionTimer().End()
1003 env := map[string]string{}
1004 if d.cfg.IsEnvsecEnabled() {
1005 secrets, err := d.Secrets(ctx)
1006 // TODO: replace this with error.Is check once envsec exports it.
1007 if err != nil && !strings.Contains(err.Error(), "project not initialized") {
1008 return nil, err
1009 } else if err != nil {
1010 ux.Fwarningf(
1011 d.stderr,
1012 "Ignoring env_from directive. jetify cloud secrets is not "+
1013 "initialized. Run `devbox secrets init` to initialize it.\n",
1014 )
1015 } else {
1016 cloudSecrets, err := secrets.List(ctx)
1017 if err != nil {
1018 ux.Fwarningf(
1019 os.Stderr,
1020 "Error reading secrets from jetify cloud: %s\n\n",
1021 err,
1022 )
1023 } else {
1024 for _, secret := range cloudSecrets {
1025 env[secret.Name] = secret.Value
1026 }
1027 }
1028 }
1029 } else if d.cfg.Root.IsdotEnvEnabled() {
1030 // if env_from points to a .env file, parse and add it
1031 parsedEnvs, err := d.cfg.Root.ParseEnvsFromDotEnv()
1032 if err != nil {
1033 // it's fine to include the error ParseEnvsFromDotEnv here because
1034 // the error message is relevant to the user
1035 return nil, usererr.New(
1036 "failed parsing %s file. Error: %v",
1037 d.cfg.Root.EnvFrom,
1038 err,
1039 )
1040 }
1041 for k, v := range parsedEnvs {
1042 env[k] = v
1043 }
1044 } else if d.cfg.Root.EnvFrom != "" {
1045 return nil, usererr.New(
1046 "unknown env_from value: %s. Supported values are: \"%q\" or a path to a file ending in \".env\"",
1047 d.cfg.Root.EnvFrom,
1048 configfile.JetifyCloudEnvFromValue,
1049 )
1050 }
1051 for k, v := range d.cfg.Env() {
1052 env[k] = v
1053 }
1054 return conf.OSExpandEnvMap(env, existingEnv, d.ProjectDir()), nil
1055}

Callers 1

computeEnvMethod · 0.95

Calls 12

SecretsMethod · 0.95
ProjectDirMethod · 0.95
FunctionTimerFunction · 0.92
FwarningfFunction · 0.92
NewFunction · 0.92
OSExpandEnvMapFunction · 0.92
EndMethod · 0.80
IsdotEnvEnabledMethod · 0.80
ParseEnvsFromDotEnvMethod · 0.80
EnvMethod · 0.65
IsEnvsecEnabledMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected