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, )
| 996 | // allow env variables from outside the shell to be referenced so |
| 997 | // no leaked variables are caused by this function. |
| 998 | func (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 | } |
no test coverage detected