// +build windows package tools import ( "bufio" "fmt" "os" "os/exec" "path/filepath" "b612.me/starainrt" "github.com/spf13/cobra" ) var cdcmd = &cobra.Command{ Use: "cd", Short: "便捷进入文件夹", Long: "使用stdin便捷进入文件夹", Run: func(this *cobra.Command, args []string) { fileInfo, _ := os.Stdin.Stat() if (fileInfo.Mode() & os.ModeNamedPipe) != os.ModeNamedPipe { fmt.Println(args) if len(args) != 0 { os.Exit(1) } else { exec.Command("cmd.exe", "/c", "explorer "+filepath.Dir(args[0])).Run() return } } s := bufio.NewScanner(os.Stdin) for s.Scan() { dir := s.Text() if starainrt.IsFile(dir) { exec.Command("cmd.exe", "/c", "explorer /n,/select,"+dir).Run() } else { exec.Command("cmd.exe", "/c", "explorer "+dir).Run() } return } }, } func init() { Maincmd.AddCommand(cdcmd) }