bbash/command/cd/cd.go
Tobias P.L Wennberg e239096ef7 Fix merge
2025-01-12 09:35:41 +01:00

35 lines
591 B
Go

package cd
import (
"bbash/environment"
"bbash/input_parser"
"fmt"
"os"
"path/filepath"
)
func Cd(in input_parser.Input, env *environment.Env) {
var new_path string
if in.Args_raw == "" {
new_path = env.Env["HOME"]
} else {
if in.Args_raw[0] == '/' {
new_path = in.Args_raw
} else {
new_path = filepath.Join(env.Path, in.Args_raw)
}
}
file, err := os.Stat(new_path)
if err != nil {
fmt.Print(fmt.Sprintf("Error changing directory %s", err.Error()))
return
}
if !file.IsDir() {
fmt.Print("New path is not a directory")
return
}
env.Path = new_path
}