Skip to content

Commit

Permalink
use bufio to read from console
Browse files Browse the repository at this point in the history
  • Loading branch information
reugn committed Aug 27, 2021
1 parent 56b662b commit 774fcba
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions cmd/wifiqr/main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package main

import (
"bufio"
"flag"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/reugn/wifiqr"
)

const version = "0.2.0"
const version = "0.2.1"

var (
versionParam = flag.Bool("version", false, "Show version.")
Expand All @@ -36,7 +38,7 @@ func main() {
config := wifiqr.NewConfig(*ssidParam, *keyParam, *encParam, *hiddenParam)
q, err := wifiqr.InitCode(config)
if err != nil {
fmt.Println(err)
fmt.Fprintf(os.Stderr, "error: %v\n", err)
return
}

Expand All @@ -46,12 +48,11 @@ func main() {
fileName := validateAndGetFileName()
err := q.WriteFile(*sizeParam, fileName)
if err != nil {
fmt.Println(err)
fmt.Fprintf(os.Stderr, "error: %v\n", err)
} else {
fmt.Println("QR Code was successfully saved to " + fileName + ".")
}
}

}

func validateAndGetFileName() string {
Expand All @@ -64,14 +65,26 @@ func validateAndGetFileName() string {
func validateArguments() {
if *ssidParam == "" {
fmt.Println("Enter the name of the wireless network (SSID):")
fmt.Scan(ssidParam)
*ssidParam = readLine()
}
if *keyParam == "" {
fmt.Println("Enter the network key (password):")
fmt.Scan(keyParam)
*keyParam = readLine()
}
if *ssidParam == "" || *keyParam == "" {
flag.Usage()
os.Exit(1)
}
}

func readLine() string {
reader := bufio.NewReader(os.Stdin)
line, err := reader.ReadString('\n')
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
// convert CRLF to LF
line = strings.Replace(line, "\n", "", -1)
return line
}

0 comments on commit 774fcba

Please sign in to comment.