Skip to content

Commit

Permalink
use bufio scanner instead of fmt.scanln
Browse files Browse the repository at this point in the history
  • Loading branch information
therealpaulgg committed Apr 26, 2023
1 parent a407108 commit 99fefaf
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 10 deletions.
4 changes: 3 additions & 1 deletion pkg/actions/challenge-response.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package actions

import (
"bufio"
"context"
"fmt"
"net/http"
Expand Down Expand Up @@ -46,8 +47,9 @@ func ChallengeResponse(c *cli.Context) error {
}
defer conn.Close()
fmt.Print("Please enter the challenge phrase: ")
scanner := bufio.NewScanner(os.Stdin)
var answer string
if _, err := fmt.Scanln(&answer); err != nil {
if err := utils.ReadLineFromStdin(scanner, &answer); err != nil {
return err
}
if err := utils.WriteClientMessage(&conn, dto.ChallengeResponseDto{
Expand Down
4 changes: 3 additions & 1 deletion pkg/actions/remove-machine.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package actions

import (
"bufio"
"bytes"
"encoding/json"
"fmt"
Expand All @@ -26,8 +27,9 @@ func RemoveMachine(c *cli.Context) error {
return err
}
fmt.Print("Please enter the machine name: ")
scanner := bufio.NewScanner(os.Stdin)
var answer string
if _, err := fmt.Scanln(&answer); err != nil {
if err := utils.ReadLineFromStdin(scanner, &answer); err != nil {
return err
}
buf := new(bytes.Buffer)
Expand Down
4 changes: 3 additions & 1 deletion pkg/actions/reset.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package actions

import (
"bufio"
"bytes"
"encoding/json"
"fmt"
Expand All @@ -24,8 +25,9 @@ func Reset(c *cli.Context) error {
return nil
}
fmt.Print("This will delete all ssh-sync data relating to this machine. Continue? (y/n): ")
scanner := bufio.NewScanner(os.Stdin)
var answer string
if _, err := fmt.Scanln(&answer); err != nil {
if err := utils.ReadLineFromStdin(scanner, &answer); err != nil {
return err
}
if answer != "y" {
Expand Down
17 changes: 10 additions & 7 deletions pkg/actions/setup.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package actions

import (
"bufio"
"bytes"
"context"
"crypto/ecdsa"
Expand Down Expand Up @@ -180,10 +181,11 @@ func createMasterKey() ([]byte, error) {
}

func newAccountSetup(serverUrl *url.URL) error {
scanner := bufio.NewScanner(os.Stdin)
// ask user to pick a username.
fmt.Print("Please enter a username. This will be used to identify your account on the server: ")
var username string
_, err := fmt.Scanln(&username)
err := utils.ReadLineFromStdin(scanner, &username)
if err != nil {
return err
}
Expand All @@ -197,8 +199,7 @@ func newAccountSetup(serverUrl *url.URL) error {
// ask user to pick a name for this machine (default to current system name)
fmt.Print("Please enter a name for this machine: ")
var machineName string
// need to not use scanln https://stackoverflow.com/questions/43843477/scanln-in-golang-doesnt-accept-whitespace
if _, err := fmt.Scanln(&machineName); err != nil {
if err := utils.ReadLineFromStdin(scanner, &machineName); err != nil {
return err
}
// then the program will generate a keypair, and upload the public key to the server
Expand Down Expand Up @@ -251,9 +252,10 @@ func newAccountSetup(serverUrl *url.URL) error {
}

func existingAccountSetup(serverUrl *url.URL) error {
scanner := bufio.NewScanner(os.Stdin)
fmt.Print("Please enter a username. This will be used to identify your account on the server: ")
var username string
_, err := fmt.Scanln(&username)
err := utils.ReadLineFromStdin(scanner, &username)
if err != nil {
return err
}
Expand All @@ -266,7 +268,7 @@ func existingAccountSetup(serverUrl *url.URL) error {
}
fmt.Print("Please enter a name for this machine: ")
var machineName string
if _, err := fmt.Scanln(&machineName); err != nil {
if err := utils.ReadLineFromStdin(scanner, &machineName); err != nil {
return err
}
wsUrl := *serverUrl
Expand Down Expand Up @@ -332,6 +334,7 @@ func existingAccountSetup(serverUrl *url.URL) error {
}

func Setup(c *cli.Context) error {
scanner := bufio.NewScanner(os.Stdin)
// all files will be stored in ~/.ssh-sync
// there will be a profile.json file containing the machine name and the username
// there will also be a keypair.
Expand All @@ -349,7 +352,7 @@ func Setup(c *cli.Context) error {
// ask user if they already have an account on the ssh-sync server.
fmt.Print("Please enter your server address (http/https): ")
var serverAddress string
if _, err := fmt.Scanln(&serverAddress); err != nil {
if err := utils.ReadLineFromStdin(scanner, &serverAddress); err != nil {
return err
}
serverUrl, err := url.Parse(serverAddress)
Expand All @@ -370,7 +373,7 @@ func Setup(c *cli.Context) error {
}
fmt.Print("Do you already have an account on the ssh-sync server? (y/n): ")
var answer string
if _, err := fmt.Scanln(&answer); err != nil {
if err := utils.ReadLineFromStdin(scanner, &answer); err != nil {
return err
}
if answer == "y" {
Expand Down
17 changes: 17 additions & 0 deletions pkg/utils/io.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package utils

import (
"bufio"
"os"
)

func ReadLineFromStdin(reader *bufio.Scanner, input *string) error {
if reader == nil {
reader = bufio.NewScanner(os.Stdin)
}
if reader.Scan() {
*input = reader.Text()
return nil
}
return reader.Err()
}

0 comments on commit 99fefaf

Please sign in to comment.