func fuzzySimple(conf *types.Configuration, searchFor string) *types.Connection { words := listWords(conf.AllConnections) reader := bufio.NewReader(os.Stdin) var found string for { color.Yellow("Search for: ") var input string var err error if searchFor != "" { input = searchFor searchFor = "" } else { input, err = reader.ReadString('\n') p(err, "reading stdin") input = strings.Trim(input, "\r\n ") } if input == "" { for _, v := range words { sort.Stable(StringList(words)) fmt.Println(v) } continue } suggs := fuzzy.RankFindFold(input, words) if len(suggs) > 1 { sort.Stable(suggs) color.Yellowln("Suggestions:") for _, v := range suggs { fmt.Println(v.Target) } color.Redln("Please try again.") } else if len(suggs) == 0 { color.Redln("Nothing found for", input+". Please try again.") } else { found = suggs[0].Target break } } conn := conf.AllConnections[found] return conn }
func main() { defer func() { if err := recover(); err != nil { color.Redln(err) if DEBUG { debug.PrintStack() } os.Exit(1) } }() if DEBUG { go http.ListenAndServe(":3000", nil) } pathP := flag.String("connectionsPath", connectionsPath, "Path to PuTTY connections.xml") verbose := false useFuzzySimple := false useOwnSSH := false flag.BoolVar(&verbose, "verbose", false, "Display more info, such as hostnames and passwords") flag.BoolVar(&verbose, "v", false, "Display more info, such as hostnames and passwords") flag.BoolVar(&useFuzzySimple, "simple", false, "Use simple interface") flag.BoolVar(&useOwnSSH, "ssh", true, "Use golang ssh client instead of os-client") flag.Parse() if pathP != nil { connectionsPath = *pathP } if flag.NArg() > 1 { flag.Usage() color.Yellowln("Usage: pcm [search term]") panic("Only one arg allowed.") } var searchFor string if flag.NArg() == 1 { searchFor = flag.Arg(0) } conf := loadConns() var conn *types.Connection if useFuzzySimple { conn = fuzzySimple(&conf, searchFor) } else { conn = selectConnection(&conf, searchFor) } if conn == nil { return } color.Yellowln("Using", conn.Info.Name) color.Redln(conn.Info.Host, conn.Info.Port) if verbose { color.Yellow("User: "******"Password: "******"Commands:") f := util.GetCommandFunc(conn, nil, func() *string { return nil }) for { if v := f(); v == nil { break } else { fmt.Println(v) } } } //fmt.Println(conn.Login) //fmt.Println(conn.Command) var console types.Terminal = &consoleTerminal{ exit: make(chan bool), } oldState, err := util.SetupTerminal() p(err, "making terminal raw") defer util.RestoreTerminal(oldState) var changed bool if useOwnSSH { changed = ssh.Connect(conn, console, func() *string { return nil }) } else { changed = connect(conn, console, func() *string { return nil }) } if changed { saveConns(&conf) } }
func p(err error, where string) { if err != nil { color.Yellow("When " + where + ": ") panic(err) } }