This repository has been archived by the owner on May 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qr.go
109 lines (94 loc) · 2.93 KB
/
qr.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// This file is part of the ego-cli distribution.
// Copyright (c) Next.e.GO Mobile SE, Aachen, Germany (https://e-go-mobile.com/)
//
// ego-cli is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, version 3.
//
// ego-cli is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"strconv"
"github.com/briandowns/spinner"
"github.com/skip2/go-qrcode"
"github.com/thatisuday/commando"
utils "github.com/egomobile/ego-cli/utils"
)
func qr_generate(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
recoveryLevel := qrcode.Medium
extraHigh, err := flags["extra-high"].GetBool()
if err == nil && extraHigh {
recoveryLevel = qrcode.Highest
} else {
high, err := flags["high"].GetBool()
if err == nil && high {
recoveryLevel = qrcode.High
} else {
medium, err := flags["medium"].GetBool()
if err == nil && medium {
recoveryLevel = qrcode.Medium
} else {
low, err := flags["low"].GetBool()
if err == nil && low {
recoveryLevel = qrcode.Low
}
}
}
}
size, err := flags["size"].GetInt()
if err != nil {
size = 1024
}
cwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
text := args["text"].Value
png, err := qrcode.Encode(text, recoveryLevel, size)
if err != nil {
log.Fatal(err)
}
var pngFile = filepath.Join(cwd, "qrcode.png")
var i = 0
var tryFindNextFilename func()
tryFindNextFilename = func() {
if _, err := os.Stat(pngFile); !os.IsNotExist(err) {
i = i + 1
pngFile = filepath.Join(cwd, "qrcode-"+strconv.Itoa(i)+".png")
tryFindNextFilename()
}
}
utils.WithSpinner("Try find unique name for output file", func(s *spinner.Spinner) {
tryFindNextFilename()
})
utils.WithSpinner("Write QR code to "+path.Base(pngFile)+" ...", func(s *spinner.Spinner) {
err = ioutil.WriteFile(pngFile, png, 0644)
if err != nil {
log.Fatal(err)
}
})
}
func Setup_qr_Command() {
commando.
Register("qr").
SetShortDescription("generate QR code").
SetDescription("Generates a QR code image in the current directory").
AddFlag("size,s", "The size in pixels", commando.Int, 1024).
AddFlag("medium,m", "Medium recovery level", commando.Bool, nil).
AddFlag("high,h", "High recovery level", commando.Bool, nil).
AddFlag("low,l", "Low recovery level", commando.Bool, nil).
AddFlag("extra-high,x", "Highest recovery level", commando.Bool, nil).
AddArgument("text", "The text for the QR code", "https://e-go-mobile.com").
SetAction(qr_generate)
}