Skip to content

Commit

Permalink
fixed windows stdin newline parsing and added some delays
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianrudnik committed Sep 15, 2021
1 parent ac83cd3 commit 45af395
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions cmd/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ var connectCmd = &cobra.Command{

// Trigger the first account read, lets see if we can pass through
log.Info().Msg("Triggering config read, please watch the phone and confirm a possible access request screen, confirm it, then restart this command")
log.Info().Str("pass", password).Str("user", username).Msg("Tralala")

accounts, err := yealink.ReadAccounts(&device)

Expand Down Expand Up @@ -127,17 +128,21 @@ var connectCmd = &cobra.Command{
}

func askPassword() (string, error) {
time.Sleep(250 * time.Millisecond)

reader := bufio.NewReader(os.Stdin)
fmt.Print("Please enter the password:")
fmt.Println("")

password, _ := reader.ReadString('\n')
password = strings.Replace(password, "\n", "", -1)
password = strings.Trim(password, "\r\n")

return password, nil
}

func askAccount(accounts []yealink.Account) yealink.Account {
time.Sleep(250 * time.Millisecond)

reader := bufio.NewReader(os.Stdin)
fmt.Println("Please pick the default outgoing URI:")
fmt.Println("")
Expand All @@ -149,13 +154,13 @@ func askAccount(accounts []yealink.Account) yealink.Account {
for {
fmt.Print("> ")
text, _ := reader.ReadString('\n')
text = strings.Replace(text, "\n", "", -1)
text = strings.Trim(text, "\r\n")

// Validate input
in, err := strconv.Atoi(text)
if err != nil || in < 0 || in > len(accounts)-1 {
log.Error().Msgf("Invalid input, please choose a value between 0 and %d", len(accounts)-1)
time.Sleep(100 * time.Millisecond)
time.Sleep(250 * time.Millisecond)
} else {
return accounts[in]
}
Expand Down

0 comments on commit 45af395

Please sign in to comment.