// +build windows package tools import ( "bufio" "os" "os/exec" "path/filepath" "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 { if len(args) != 1 { os.Exit(1) } else { exec.Command("cmd.exe", "/c", "explorer "+filepath.Dir(args[0])).Run() return } } s := bufio.NewScanner(os.Stdin) for s.Scan() { exec.Command("cmd.exe", "/c", "explorer "+filepath.Dir(s.Text())).Run() return } }, } func init() { Maincmd.AddCommand(cdcmd) }