-
Notifications
You must be signed in to change notification settings - Fork 1
/
qrcode.go
43 lines (34 loc) · 806 Bytes
/
qrcode.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
package main
import (
"os"
"strconv"
"time"
qrcode "github.com/skip2/go-qrcode"
)
func createQRCode(filepath string, message string) (path string, err error) {
path = resolveFilePath(filepath)
if path != "" {
err := qrcode.WriteFile(message, qrcode.Medium, 256, path)
if err != nil {
return "", err
}
} else {
png, err := qrcode.Encode(message, qrcode.Medium, 256)
if err != nil {
return "", err
}
os.Stdout.Write(png)
}
return path, nil
}
// resolve filePath if it's empty
func resolveFilePath(filepath string) string {
if filepath == "" {
tempFilepath := os.TempDir()
// append file name with time unix nano string
tempFilepath += string(os.PathSeparator) +
strconv.FormatInt(time.Now().UnixNano(), 10) + ".png"
return tempFilepath
}
return filepath
}