Skip to content

Commit 8b57442

Browse files
Cleanup in response to PR comments
1 parent d005a8d commit 8b57442

File tree

9 files changed

+62
-23
lines changed

9 files changed

+62
-23
lines changed

internal/boxcli/generate.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,14 @@ func direnvCmd() *cobra.Command {
157157
// --envrc-dir allows users to specify a directory where the .envrc file should be generated
158158
// separately from the devbox config directory. Without this flag, the .envrc file
159159
// will be generated in the same directory as the devbox config file (i.e., either the current
160-
// directory or the directory specified by --config). This is useful for users who want to keep
161-
// their .envrc and devbox config files in different locations.
160+
// directory or the directory specified by --config). This flag is useful for users who want to
161+
// keep their .envrc and devbox config files in different locations.
162162
command.Flags().StringVar(
163163
&flags.envrcDir, "envrc-dir", "",
164-
"path to directory where the .envrc file should be generated. "+
165-
"If not specified, the .envrc file will be generated in "+
166-
"the current working directory.")
164+
"path to directory where the .envrc file should be generated.\n"+
165+
"If not specified, the .envrc file will be generated in the same directory as\n"+
166+
"the devbox.json config file. Also, when specified along with --config, the config file\n"+
167+
"location will be relative to the .envrc file location.")
167168

168169
flags.config.register(command)
169170
return command
@@ -288,6 +289,11 @@ func runGenerateDirenvCmd(cmd *cobra.Command, flags *generateCmdFlags) error {
288289
"Use --envrc-dir to specify the directory where the .envrc file should be generated.")
289290
}
290291

292+
if flags.printEnvrcContent {
293+
return devbox.PrintEnvrcContent(
294+
cmd.OutOrStdout(), devopt.EnvFlags(flags.envFlag), flags.config.path)
295+
}
296+
291297
// Determine the directories for .envrc and config
292298
configDir, envrcDir, err := determineDirenvDirs(flags.config.path, flags.envrcDir)
293299
if err != nil {
@@ -300,10 +306,6 @@ func runGenerateDirenvCmd(cmd *cobra.Command, flags *generateCmdFlags) error {
300306
EnvFlags: devopt.EnvFlags(flags.envFlag),
301307
}
302308

303-
if flags.printEnvrcContent {
304-
return devbox.PrintEnvrcContent(cmd.OutOrStdout(), generateOpts)
305-
}
306-
307309
box, err := devbox.Open(&devopt.Opts{
308310
Dir: filepath.Join(envrcDir, configDir),
309311
Environment: flags.config.environment,
@@ -317,7 +319,7 @@ func runGenerateDirenvCmd(cmd *cobra.Command, flags *generateCmdFlags) error {
317319
cmd.Context(), flags.force, generateOpts)
318320
}
319321

320-
// Returns cononical paths for configDir and envrcDir. Both locations are relative to the current
322+
// Returns canonical paths for configDir and envrcDir. Both locations are relative to the current
321323
// working directory when provided to this function. However, since the config file will ultimately
322324
// be relative to the .envrc file, we need to determine the relative path from envrcDir to configDir.
323325
func determineDirenvDirs(configDir, envrcDir string) (string, string, error) {

internal/devbox/devbox.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -527,8 +527,8 @@ func (d *Devbox) GenerateDockerfile(ctx context.Context, generateOpts devopt.Gen
527527
}))
528528
}
529529

530-
func PrintEnvrcContent(w io.Writer, opts devopt.EnvrcOpts) error {
531-
return generate.EnvrcContent(w, opts)
530+
func PrintEnvrcContent(w io.Writer, opts devopt.EnvFlags, configDir string) error {
531+
return generate.EnvrcContent(w, opts, configDir)
532532
}
533533

534534
// GenerateEnvrcFile generates a .envrc file that makes direnv integration convenient

internal/devbox/generate/devcontainer_util.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ func CreateEnvrc(ctx context.Context, opts devopt.EnvrcOpts) error {
165165

166166
// write content into file
167167
return t.Execute(file, map[string]string{
168-
"Flags": strings.Join(flags, " "),
169-
"Dir": opts.ConfigDir,
168+
"EnvFlag": strings.Join(flags, " "),
169+
"ConfigDir": formatConfigDirArg(opts.ConfigDir),
170170
})
171171
}
172172

@@ -220,7 +220,7 @@ func (g *Options) getDevcontainerContent() *devcontainerObject {
220220
return devcontainerContent
221221
}
222222

223-
func EnvrcContent(w io.Writer, envFlags devopt.EnvrcOpts) error {
223+
func EnvrcContent(w io.Writer, envFlags devopt.EnvFlags, configDir string) error {
224224
tmplName := "envrcContent.tmpl"
225225
t := template.Must(template.ParseFS(tmplFS, "tmpl/"+tmplName))
226226
envFlag := ""
@@ -229,9 +229,21 @@ func EnvrcContent(w io.Writer, envFlags devopt.EnvrcOpts) error {
229229
envFlag += fmt.Sprintf("--env %s=%s ", k, v)
230230
}
231231
}
232+
232233
return t.Execute(w, map[string]string{
233-
"EnvFlag": envFlag,
234-
"EnvFile": envFlags.EnvFile,
235-
"Dir": envFlags.EnvrcDir,
234+
"JSONPath": filepath.Join(configDir, "devbox.json"),
235+
"LockPath": filepath.Join(configDir, "devbox.lock"),
236+
"EnvFlag": envFlag,
237+
"EnvFile": envFlags.EnvFile,
238+
"ConfigDir": formatConfigDirArg(configDir),
236239
})
237240
}
241+
242+
func formatConfigDirArg(configDir string) string {
243+
configDirArg := ""
244+
if configDir != "" {
245+
configDirArg = "--config " + configDir
246+
}
247+
248+
return configDirArg
249+
}

internal/devbox/generate/tmpl/envrc.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Automatically sets up your devbox environment whenever you cd into this
44
# directory via our direnv integration:
55

6-
eval "$(devbox generate direnv --print-envrc{{ if .Flags}} {{ .Flags }}{{ end }}{{ if .Dir }} --config {{ .Dir -}}{{ end }})"
6+
eval "$(devbox generate direnv --print-envrc{{ if .EnvFlag}} {{ .EnvFlag }}{{ end }}{{ if .ConfigDir }} {{ .ConfigDir }}{{ end }})"
77
88
# check out https://www.jetpack.io/devbox/docs/ide_configuration/direnv/
99
# for more details

internal/devbox/generate/tmpl/envrcContent.tmpl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
{{define "DirPrefix"}}{{ if .Dir }}{{ .Dir }}/{{ end }}{{end}}
21
use_devbox() {
3-
watch_file {{template "DirPrefix" .}}devbox.json {{template "DirPrefix" .}}devbox.lock
4-
eval "$(devbox shellenv --init-hook --install --no-refresh-alias {{ if .EnvFlag }}{{ .EnvFlag }}{{ end }} {{- if .Dir }}--config {{ .Dir -}}{{ end }})"
2+
watch_file {{ .JSONPath }} {{ .LockPath }}
3+
eval "$(devbox shellenv --init-hook --install --no-refresh-alias{{ if .EnvFlag}} {{ .EnvFlag }}{{ end }}{{ if .ConfigDir }} {{ .ConfigDir }}{{ end }})"
54
}
65
use devbox
76
{{ if .EnvFile }}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Testscript to validate generating the contents of the .envrc file.
2+
3+
exec devbox init
4+
exec devbox generate direnv --env x=y
5+
exists .envrc
6+
exists devbox.json
7+
grep 'eval "\$\(devbox generate direnv --print-envrc --env x=y\)"' .envrc
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Testscript to validate generating a direnv .envrc in a specified location (./dir) that also
2+
# references a devbox config in another dir (./cfg) that is a subdir of the first.
3+
4+
mkdir dir/cfg
5+
exec devbox init dir/cfg
6+
exists dir/cfg/devbox.json
7+
8+
exec devbox generate direnv --envrc-dir dir --config dir/cfg --env x=y
9+
grep 'eval "\$\(devbox generate direnv --print-envrc --env x=y --config cfg\)"' dir/.envrc
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Testscript to validate generating the contents of the .envrc file.
2+
# Note that since --envrc-dir was NOT specified, the .envrc will be in the `dir` directory and
3+
# the config will be found there, which means the `--print-env` doesn't need to specify the dir.
4+
5+
mkdir dir
6+
exec devbox init dir
7+
exec devbox generate direnv --env x=y --config dir
8+
exists dir/.envrc
9+
exists dir/devbox.json
10+
grep 'eval "\$\(devbox generate direnv --print-envrc --env x=y\)"' dir/.envrc

testscripts/generate/direnv-printenvrc.test.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ exec devbox init
44
exec devbox generate direnv --print-envrc
55
cp stdout results.txt
66
grep 'watch_file devbox.json devbox.lock' results.txt
7-
grep 'eval "\$\(devbox shellenv --init-hook --install --no-refresh-alias \)"' results.txt
7+
grep 'eval "\$\(devbox shellenv --init-hook --install --no-refresh-alias\)"' results.txt
88
! exists .envrc

0 commit comments

Comments
 (0)