package cd import ( "errors" "fmt" "os" "syscall" "gosh/internal/paths" ) func fmtError(dir string, err error) string { switch { case errors.Is(err, os.ErrNotExist): return fmt.Sprintf("directory \"%s\" does not exist", dir) case errors.Is(err, os.ErrPermission): return fmt.Sprintf("Permission denied: \"%s\"", dir) case errors.Is(err, syscall.ENOTDIR): return fmt.Sprintf("\"%s\" is not a directory", dir) } return err.Error() } func Exec(args []string) error { dir := "" if len(args) > 0 { dir = args[0] } dir = paths.Expand(dir) if err := os.Chdir(dir); err != nil { return errors.New("cd: " + fmtError(dir, err)) } return nil }