GenerateEnvrcFile generates a .envrc file that makes direnv integration convenient
(ctx context.Context, opts devopt.EnvrcOpts)
| 551 | |
| 552 | // GenerateEnvrcFile generates a .envrc file that makes direnv integration convenient |
| 553 | func (d *Devbox) GenerateEnvrcFile(ctx context.Context, opts devopt.EnvrcOpts) error { |
| 554 | ctx, task := trace.NewTask(ctx, "devboxGenerateEnvrc") |
| 555 | defer task.End() |
| 556 | |
| 557 | // If no envrcDir was specified, use the configDir. This is for backward compatibility |
| 558 | // where the .envrc was placed in the same location as specified by --config. Note that |
| 559 | // if that is also blank, the .envrc will be generated in the current working directory. |
| 560 | if opts.EnvrcDir == "" { |
| 561 | opts.EnvrcDir = opts.ConfigDir |
| 562 | } |
| 563 | |
| 564 | envrcFilePath := filepath.Join(opts.EnvrcDir, ".envrc") |
| 565 | filesExist := fileutil.Exists(envrcFilePath) |
| 566 | if !opts.Force && filesExist { |
| 567 | return usererr.New( |
| 568 | "A .envrc is already present in %q. Remove it or use --force to overwrite it.", |
| 569 | opts.EnvrcDir, |
| 570 | ) |
| 571 | } |
| 572 | |
| 573 | // generate all shell files to ensure we can refer to them in the .envrc script |
| 574 | if err := d.ensureStateIsUpToDate(ctx, ensure); err != nil { |
| 575 | return err |
| 576 | } |
| 577 | |
| 578 | // .envrc file creation |
| 579 | err := generate.CreateEnvrc(ctx, opts) |
| 580 | if err != nil { |
| 581 | return errors.WithStack(err) |
| 582 | } |
| 583 | ux.Fsuccessf(d.stderr, "generated .envrc file in %q.\n", opts.EnvrcDir) |
| 584 | if cmdutil.Exists("direnv") { |
| 585 | cmd := exec.Command("direnv", "allow", opts.EnvrcDir) |
| 586 | err := cmd.Run() |
| 587 | if err != nil { |
| 588 | return errors.WithStack(err) |
| 589 | } |
| 590 | ux.Fsuccessf(d.stderr, "ran `direnv allow %s`\n", opts.EnvrcDir) |
| 591 | } |
| 592 | return nil |
| 593 | } |
| 594 | |
| 595 | // saveCfg writes the config file to the devbox directory. |
| 596 | func (d *Devbox) saveCfg() error { |
no test coverage detected