func (c CliClient) Listen() { var err error rl, err = readline.New(" ยป ") if err != nil { panic(err) } defer rl.Close() rlog.SetOutput(rl.Stdout()) for { line, err := rl.Readline() if len(line) == 0 { continue } if err != nil { break } raw := ":RainBot " + irc.PRIVMSG + " #cli :" + line msg := irc.ParseMessage(raw) if err != nil { rlog.Error("cli", err.Error()) } c.Emit(irc.PRIVMSG, msg) } }
// PromptNewPassword asks the user to input a password. // // While typing, the user gets feedback by the prompt color, // which changes with the security of the password to green. // Additionally the entrtopy of the password is shown. // If minEntropy was not reached after hitting enter, // this function will log a message and ask the user again. func PromptNewPassword(minEntropy float64) ([]byte, error) { rl, err := readline.New("") if err != nil { return nil, err } defer util.Closer(rl) passwordCfg := rl.GenPasswordConfig() passwordCfg.SetListener(func(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool) { rl.SetPrompt(createStrengthPrompt(line, " New ")) rl.Refresh() return nil, 0, false }) pwd := []byte{} for { pwd, err = rl.ReadPasswordWithConfig(passwordCfg) if err != nil { return nil, err } strength := zxcvbn.PasswordStrength(string(pwd), nil) if strength.Entropy >= minEntropy { break } fmt.Printf(colors.Colorize(msgLowEntropy, colors.Yellow)+"\n", minEntropy) } passwordCfg.SetListener(func(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool) { rl.SetPrompt(createStrengthPrompt(line, "Retype ")) rl.Refresh() return nil, 0, false }) fmt.Println(colors.Colorize(msgReEnter, colors.Green)) for { newPwd, err := rl.ReadPasswordWithConfig(passwordCfg) if err != nil { return nil, err } if bytes.Equal(pwd, newPwd) { break } fmt.Println(colors.Colorize(msgBadPassword, colors.Yellow)) } return pwd, nil }
func askForMessage() (string, error) { if !isTerminalIn { return "", errors.New("Can't ask for message") } fmt.Println("A commit message is required, enter one below:") reader, err := readline.New("> ") if err != nil { return "", err } message, err := reader.Readline() return strings.Trim(message, " \n"), err }
func promptPasswordColored(color int) (string, error) { prompt := "Password: "******"", err } defer util.Closer(rl) return doPromptLine(rl, prompt, true) }
func main() { rl, err := readline.New("") if err != nil { return } defer rl.Close() setPasswordCfg := rl.GenPasswordConfig() setPasswordCfg.SetListener(func(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool) { rl.SetPrompt(createStrengthPrompt(line)) rl.Refresh() return nil, 0, false }) pswd, err := rl.ReadPasswordWithConfig(setPasswordCfg) if err != nil { return } fmt.Println("Your password was:", string(pswd)) }
func main() { rl, err := readline.New("> ") if err != nil { panic(err) } defer rl.Close() for { line, err := rl.Readline() if err != nil { // io.EOF break } if line == "help" { println(`Usage [run|stop]`) } } }
func EmailClient() { usr, _ := user.Current() if _, err := os.Stat(usr.HomeDir + "/.go-guerrilla"); os.IsNotExist(err) { os.Mkdir(usr.HomeDir+"/.go-guerrilla", os.FileMode(0700)) } db, err := bolt.Open(usr.HomeDir+"/.go-guerrilla/client.db", 0600, nil) if err != nil { log.Fatal(err) } defer db.Close() db.Update(func(tx *bolt.Tx) error { _, err := tx.CreateBucketIfNotExists([]byte("cache")) if err != nil { return fmt.Errorf("create bucket: %s", err) } return nil }) transport := &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, } client := &http.Client{Transport: transport} ctx := Context{ client: client, db: db, } rl, err := readline.New("> ") if err != nil { log.Fatal(err) } defer rl.Close() reader := bufio.NewReader(os.Stdin) for { line, err := rl.Readline() if err != nil { break } parts := strings.Split(line, " ") if len(parts) == 0 { continue } switch parts[0] { case "connect": if len(parts) != 2 { fmt.Println("index <host>") break } ctx.host = fmt.Sprintf("https://%v:3443", parts[1]) fmt.Print("Enter password:"******"index": if len(parts) != 2 { fmt.Println("index <page number>") break } i, err := strconv.Atoi(parts[1]) if err != nil { log.Fatal(err) } ctx.Index(i) case "view": if len(parts) != 2 { fmt.Println("view <email id>") break } i, err := strconv.Atoi(parts[1]) if err != nil { log.Fatal(err) } ctx.View(i) case "exit": return } } }
func startConsole(servers []*Server) { rl, err := readline.New("> ") if err != nil { panic(err) } defer rl.Close() type output struct { serv *Server output string } isTerminal := readline.IsTerminal(int(os.Stdin.Fd())) for { var line string var err error if isTerminal { line, err = rl.Readline() } else { lineb, er := ioutil.ReadAll(os.Stdin) line, err = string(lineb), er } if err != nil { // io.EOF break } outputc := make(chan output) var mutex sync.Mutex var count int var wg sync.WaitGroup wg.Add(1) go func() { for { mutex.Lock() output := <-outputc prompt := output.serv.prompt() if isTerminal { prompt += " " + line } if count > 0 && strings.TrimSpace(output.output) != "" { prompt = "\n" + prompt } fmt.Println(prompt) if strings.TrimSpace(output.output) != "" { fmt.Printf(output.output) } if count++; count >= len(servers) { break } mutex.Unlock() } wg.Done() }() for _, serv := range servers { wg.Add(1) go func(serv *Server) { outputc <- output{serv: serv, output: serv.exec(line)} wg.Done() }(serv) } wg.Wait() if !isTerminal { return } } }
func main() { kingpin.Parse() var conn net.Conn var err error if *rawtls { conn, err = tls.Dial("tcp", *address, &tls.Config{ InsecureSkipVerify: true, }) if err != nil { fmt.Fprintf(os.Stderr, "Connect failed: %v\n", err) return } } else { conn, err = net.Dial("tcp", *address) if err != nil { fmt.Fprintf(os.Stderr, "Connect failed: %v\n", err) return } } c := client.New(conn) go func() { err := c.Serve() if err != nil { fmt.Fprintf(os.Stderr, "\nConnection lost: %v\n", err) os.Exit(-1) } }() _, _, err = c.Version(1024*1024, qp.Version) if err != nil { fmt.Fprintf(os.Stderr, "Version failed: %v\n", err) return } root, _, err := c.Attach(nil, *user, *service) if err != nil { fmt.Fprintf(os.Stderr, "Attach failed: %v\n", err) return } cwd := root confirmation, err = readline.New("") if err != nil { fmt.Fprintf(os.Stderr, "Could not make confirmation readline instance: %v\n", err) return } if len(*command) > 0 { args := "" for i := 1; i < len(*command); i++ { if i != 1 { args += " " } args += (*command)[i] } f, ok := cmds[(*command)[0]] if !ok { fmt.Fprintf(os.Stderr, "no such command: [%s]\n", *command) return } cwd, err = f(root, cwd, args) if err != nil { fmt.Fprintf(os.Stderr, "\ncommand %s failed: %v\n", *command, err) } return } completer := readline.NewPrefixCompleter() for k := range cmds { completer.Children = append(completer.Children, readline.PcItem(k)) } rl, err := readline.NewEx(&readline.Config{ Prompt: "9p> ", AutoComplete: completer, }) if err != nil { fmt.Fprintf(os.Stderr, "failed to create readline: %v\n", err) return } defer rl.Close() fmt.Fprintf(os.Stderr, "Welcome to the qptools 9P cli.\nPress tab to see available commands.\n") for { line, err := rl.Readline() if err != nil { // io.EOF break } idx := strings.Index(line, " ") var cmd, args string if idx != -1 { cmd = line[:idx] args = line[idx+1:] } else { cmd = line } f, ok := cmds[cmd] if !ok { fmt.Fprintf(os.Stderr, "no such command: [%s]\n", cmd) continue } cwd, err = f(root, cwd, args) if err != nil { fmt.Fprintf(os.Stderr, "\ncommand %s failed: %v\n", cmd, err) } } }