Load evaluates the RC file and returns the new Env or error. This functions is key to the implementation of direnv.
(previousEnv Env)
| 205 | // |
| 206 | // This functions is key to the implementation of direnv. |
| 207 | func (rc *RC) Load(previousEnv Env) (newEnv Env, err error) { |
| 208 | config := rc.config |
| 209 | wd := config.WorkDir |
| 210 | direnv := config.SelfPath |
| 211 | newEnv = previousEnv.Copy() |
| 212 | newEnv[DIRENV_WATCHES] = rc.times.Marshal() |
| 213 | defer func() { |
| 214 | // Record directory changes even if load is disallowed or fails |
| 215 | newEnv[DIRENV_DIR] = "-" + filepath.Dir(rc.path) |
| 216 | newEnv[DIRENV_FILE] = rc.path |
| 217 | newEnv[DIRENV_DIFF] = previousEnv.Diff(newEnv).Serialize() |
| 218 | }() |
| 219 | |
| 220 | // Abort if the file is not allowed |
| 221 | switch rc.Allowed() { |
| 222 | case NotAllowed: |
| 223 | err = fmt.Errorf(notAllowed, rc.Path()) |
| 224 | return |
| 225 | case Allowed: |
| 226 | case Denied: |
| 227 | return |
| 228 | } |
| 229 | |
| 230 | // Allow RC loads to be canceled with SIGINT |
| 231 | ctx, cancel := context.WithCancel(context.Background()) |
| 232 | c := make(chan os.Signal, 1) |
| 233 | signal.Notify(c, os.Interrupt) |
| 234 | go func() { |
| 235 | <-c |
| 236 | cancel() |
| 237 | }() |
| 238 | |
| 239 | // check what type of RC we're processing |
| 240 | // use different exec method for each |
| 241 | fn := "source_env" |
| 242 | if filepath.Base(rc.path) == ".env" { |
| 243 | fn = "dotenv" |
| 244 | } |
| 245 | |
| 246 | // Set stdin based on the config |
| 247 | var stdin *os.File |
| 248 | if config.DisableStdin { |
| 249 | stdin, err = os.Open(os.DevNull) |
| 250 | if err != nil { |
| 251 | return |
| 252 | } |
| 253 | } else { |
| 254 | stdin = os.Stdin |
| 255 | } |
| 256 | |
| 257 | prelude := "" |
| 258 | if config.StrictEnv { |
| 259 | prelude = "set -euo pipefail && " |
| 260 | } |
| 261 | |
| 262 | // Non-Windows platforms will already use slashes. However, on Windows |
| 263 | // backslashes are used by default which can result in unexpected escapes |
| 264 | // like \b or \r in paths. Force slash usage to avoid issues on Windows. |
no test coverage detected