mirror of
https://gitlab.com/pnx/gosh
synced 2026-06-15 23:03:09 +02:00
35 lines
666 B
Go
35 lines
666 B
Go
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
|
|
}
|