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

Method setupShellStartupFiles

internal/devbox/shell.go:404–466  ·  view source on GitHub ↗

setupShellStartupFiles creates initialization files for the shell by sourcing the user's originals. We do this instead of linking or copying, so that we can set correct ZDOTDIR when sourcing user's config files which may use the ZDOTDIR env var inside them. This also allows us to make sure any devbo

(shellSettingsDir string)

Source from the content-addressed store, hash-verified

402//
403// We do not link the .{shell}rc files, since devbox modifies them. See writeDevboxShellrc
404func (s *DevboxShell) setupShellStartupFiles(shellSettingsDir string) {
405 // For now, we only need to do this for zsh shell
406 if s.name == shZsh {
407 // List of zsh startup files: https://zsh.sourceforge.io/Intro/intro_3.html
408 filenames := []string{".zshenv", ".zprofile", ".zlogin", ".zlogout"}
409
410 // zim framework
411 // https://zimfw.sh/docs/install/
412 filenames = append(filenames, ".zimrc")
413
414 for _, filename := range filenames {
415 // The userShellrcPath should be set to ZDOTDIR already.
416 userFile := filepath.Join(filepath.Dir(s.userShellrcPath), filename)
417 _, err := os.Stat(userFile)
418 if errors.Is(err, fs.ErrNotExist) {
419 // this file may not be relevant for the user's setup.
420 continue
421 }
422 if err != nil {
423 slog.Debug("os.Stat error for %s is %v", userFile, err)
424 }
425
426 fileNew := filepath.Join(shellSettingsDir, filename)
427
428 // Create template content that sources the original file
429 templateContent := `if [[ -f "{{.UserFile}}" ]]; then
430 local DEVBOX_ZDOTDIR="$ZDOTDIR"
431 export ZDOTDIR="{{.ZDOTDIR}}"
432 . "{{.UserFile}}"
433 export ZDOTDIR="$DEVBOX_ZDOTDIR"
434fi`
435
436 // Parse and execute the template
437 tmpl, err := template.New("shellrc").Parse(templateContent)
438 if err != nil {
439 slog.Error("error parsing template for zsh setting file", "filename", filename, "err", err)
440 continue
441 }
442
443 // Create the new file with template content
444 file, err := os.Create(fileNew)
445 if err != nil {
446 slog.Error("error creating zsh setting file", "filename", filename, "err", err)
447 continue
448 }
449 defer file.Close()
450
451 // Execute template with data
452 data := struct {
453 UserFile string
454 ZDOTDIR string
455 }{
456 UserFile: userFile,
457 ZDOTDIR: filepath.Dir(s.userShellrcPath),
458 }
459
460 if err := tmpl.Execute(file, data); err != nil {
461 slog.Error("error executing template for zsh setting file", "filename", filename, "err", err)

Callers 2

RunMethod · 0.95

Calls 3

IsMethod · 0.80
ExecuteMethod · 0.65
ErrorMethod · 0.45

Tested by 1