35 lines
575 B
Go
35 lines
575 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 == "" {
|
|
new_path = env.Env["HOME"]
|
|
} else {
|
|
if in.Args[0] == '/' {
|
|
new_path = in.Args
|
|
} else {
|
|
new_path = filepath.Join(env.Path, in.Args)
|
|
}
|
|
}
|
|
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
|
|
}
|
|
|
|
|