aerc/commands/cd.go

46 lines
730 B
Go
Raw Normal View History

2019-03-15 02:34:34 +00:00
package commands
import (
"errors"
2019-03-15 14:47:09 +00:00
"os"
2019-03-15 02:34:34 +00:00
"git.sr.ht/~sircmpwn/aerc2/widgets"
2019-03-15 14:47:09 +00:00
"github.com/mitchellh/go-homedir"
2019-03-15 02:34:34 +00:00
)
var (
2019-03-15 14:47:09 +00:00
previousDir string
2019-03-15 02:34:34 +00:00
)
func init() {
2019-03-21 20:30:23 +00:00
register("cd", ChangeDirectory)
2019-03-15 02:34:34 +00:00
}
func ChangeDirectory(aerc *widgets.Aerc, args []string) error {
if len(args) != 2 {
2019-03-15 14:47:27 +00:00
return errors.New("Usage: cd <directory>")
2019-03-15 02:34:34 +00:00
}
2019-03-15 14:47:09 +00:00
cwd, err := os.Getwd()
if err != nil {
return err
}
var target string
2019-03-15 02:34:34 +00:00
if args[1] == "-" {
2019-03-15 14:47:09 +00:00
if previousDir == "" {
return errors.New("No previous folder to return to")
2019-03-15 02:34:34 +00:00
} else {
2019-03-15 14:47:09 +00:00
target = previousDir
2019-03-15 02:34:34 +00:00
}
} else {
2019-03-15 14:47:09 +00:00
target = args[1]
}
target, err = homedir.Expand(target)
if err != nil {
return err
}
if err := os.Chdir(target); err == nil {
previousDir = cwd
2019-03-15 02:34:34 +00:00
}
2019-03-15 14:47:09 +00:00
return err
2019-03-15 02:34:34 +00:00
}