| 279 | } |
| 280 | |
| 281 | func (s *DevboxShell) writeDevboxShellrc() (path string, err error) { |
| 282 | // We need a temp dir (as opposed to a temp file) because zsh uses |
| 283 | // ZDOTDIR to point to a new directory containing the .zshrc. |
| 284 | tmp, err := os.MkdirTemp("", "devbox") |
| 285 | if err != nil { |
| 286 | return "", fmt.Errorf("create temp dir for shell init file: %v", err) |
| 287 | } |
| 288 | |
| 289 | // This is a best-effort to include the user's existing shellrc. |
| 290 | userShellrc := []byte{} |
| 291 | if s.userShellrcPath != "" { |
| 292 | // If we can't read it, then just omit it from the devbox shellrc. |
| 293 | userShellrc, _ = os.ReadFile(s.userShellrcPath) |
| 294 | } |
| 295 | |
| 296 | // If the user already has a shellrc file, then give the devbox shellrc |
| 297 | // file the same name. Otherwise, use an arbitrary name of "shellrc". |
| 298 | shellrcName := "shellrc" |
| 299 | if s.userShellrcPath != "" { |
| 300 | shellrcName = filepath.Base(s.userShellrcPath) |
| 301 | } |
| 302 | path = filepath.Join(tmp, shellrcName) |
| 303 | shellrcf, err := os.Create(path) |
| 304 | if err != nil { |
| 305 | return "", fmt.Errorf("write to shell init file: %v", err) |
| 306 | } |
| 307 | defer func() { |
| 308 | cerr := shellrcf.Close() |
| 309 | if err == nil { |
| 310 | err = cerr |
| 311 | } |
| 312 | }() |
| 313 | |
| 314 | tmpl := shellrcTmpl |
| 315 | if s.name == shFish { |
| 316 | tmpl = fishrcTmpl |
| 317 | } |
| 318 | |
| 319 | err = tmpl.Execute(shellrcf, struct { |
| 320 | ProjectDir string |
| 321 | OriginalInit string |
| 322 | OriginalInitPath string |
| 323 | HooksFilePath string |
| 324 | ShellStartTime string |
| 325 | HistoryFile string |
| 326 | ExportEnv string |
| 327 | ShellName string |
| 328 | |
| 329 | ShellAliases []string |
| 330 | |
| 331 | RefreshAliasName string |
| 332 | RefreshCmd string |
| 333 | RefreshAliasEnvVar string |
| 334 | }{ |
| 335 | ProjectDir: s.projectDir, |
| 336 | OriginalInit: string(bytes.TrimSpace(userShellrc)), |
| 337 | OriginalInitPath: s.userShellrcPath, |
| 338 | HooksFilePath: shellgen.ScriptPath(s.projectDir, shellgen.HooksFilename), |