From 99fefaf8bba8236761814a356552aa7796cdaa87 Mon Sep 17 00:00:00 2001 From: therealpaulgg Date: Wed, 26 Apr 2023 14:35:13 -0700 Subject: [PATCH] use bufio scanner instead of fmt.scanln --- pkg/actions/challenge-response.go | 4 +++- pkg/actions/remove-machine.go | 4 +++- pkg/actions/reset.go | 4 +++- pkg/actions/setup.go | 17 ++++++++++------- pkg/utils/io.go | 17 +++++++++++++++++ 5 files changed, 36 insertions(+), 10 deletions(-) create mode 100644 pkg/utils/io.go diff --git a/pkg/actions/challenge-response.go b/pkg/actions/challenge-response.go index f0794b4..983324b 100644 --- a/pkg/actions/challenge-response.go +++ b/pkg/actions/challenge-response.go @@ -1,6 +1,7 @@ package actions import ( + "bufio" "context" "fmt" "net/http" @@ -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{ diff --git a/pkg/actions/remove-machine.go b/pkg/actions/remove-machine.go index 399de7e..c04d78a 100644 --- a/pkg/actions/remove-machine.go +++ b/pkg/actions/remove-machine.go @@ -1,6 +1,7 @@ package actions import ( + "bufio" "bytes" "encoding/json" "fmt" @@ -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) diff --git a/pkg/actions/reset.go b/pkg/actions/reset.go index 014f66b..e9b348e 100644 --- a/pkg/actions/reset.go +++ b/pkg/actions/reset.go @@ -1,6 +1,7 @@ package actions import ( + "bufio" "bytes" "encoding/json" "fmt" @@ -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" { diff --git a/pkg/actions/setup.go b/pkg/actions/setup.go index 6f6998c..ced9d30 100644 --- a/pkg/actions/setup.go +++ b/pkg/actions/setup.go @@ -1,6 +1,7 @@ package actions import ( + "bufio" "bytes" "context" "crypto/ecdsa" @@ -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 } @@ -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 @@ -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 } @@ -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 @@ -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. @@ -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) @@ -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" { diff --git a/pkg/utils/io.go b/pkg/utils/io.go new file mode 100644 index 0000000..f9a5d88 --- /dev/null +++ b/pkg/utils/io.go @@ -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() +}