-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
59 lines (49 loc) · 966 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"bufio"
"flag"
"fmt"
"io"
"os"
"strings"
)
type cfg struct {
filepath string
noOpen bool
}
func main() {
var cfg cfg
flag.StringVar(&cfg.filepath, "f", "", "file path to save the qrcode image")
flag.BoolVar(&cfg.noOpen, "n", false, "don't open image when created")
flag.Parse()
var content string
if flag.NArg() > 0 {
// read data from cli
content = strings.Join(flag.Args(), " ")
} else {
// read data from stdin
reader := bufio.NewReader(os.Stdin)
bb := make([]byte, 1024)
for {
n, err := reader.Read(bb)
if err != nil {
if err == io.EOF {
break
} else {
fmt.Fprintf(os.Stderr, "read from os.Stdin error: %v", err)
return
}
}
content += string(bb[:n])
}
}
fp, err := createQRCode(cfg.filepath, content)
if err != nil {
fmt.Fprintf(os.Stderr, "create QR code fail: %v", err)
os.Exit(1)
}
// Open after created the image
if !cfg.noOpen {
open(fp)
}
}