cdhist/main.go

60 lines
1.2 KiB
Go

package main
import (
"os"
"flag"
"log"
"path"
"fmt"
)
// must run the following to source the cd bash function
// if type cdhist &>/dev/null; then . <(cdhist -i); fi
var (
histFile string = ".cd_history"
outString string = ""
)
func main() {
histFileDir, err := os.UserHomeDir()
if err != nil {
log.Printf("Error reading cd history file: %v", err)
}
histFile := path.Join(histFileDir, histFile)
if len(os.Args) == 2 && os.Args[1] == "--" {
if err := histList(histFile); err != nil {
log.Printf("Error reading cd history file: %s", err)
}
os.Exit(1)
}
flagInit := flag.Bool("i", false, "Creates function to be sourced by bash")
flagL := flag.Bool("L", false, "Passed to cd")
flagP := flag.Bool("P", false, "Passed to cd")
flage := flag.Bool("e", false, "Passed to cd")
flagAt := flag.Bool("@", false, "Passed to cd")
flag.Parse()
switch {
case *flagInit:
makeBashFunc()
os.Exit(1)
case *flagL:
outString = fmt.Sprintf("%s -L", outString)
case *flagP:
outString = fmt.Sprintf("%s -P", outString)
case *flage:
outString = fmt.Sprintf("%s -e", outString)
case *flagAt:
outString = fmt.Sprintf("%s -@", outString)
}
path := flag.Arg(0)
cd(histFile, outString, path)
return
}