-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
153 lines (121 loc) · 3.29 KB
/
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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"fmt"
"log"
"os"
"github.com/jovandeginste/payme/payment"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// CLI to generate SEPA payment QR codes, either as ASCII or PNG
const qrSize = 300
var (
// gitRef = "0.0.0-dev"
// gitRefType = "local"
gitRefName = "local"
gitCommit = "local"
buildTime = "manually"
)
type qrParams struct {
Payment *payment.Payment
OutputType string
OutputFile string
Debug bool
}
func main() {
q := qrParams{
Payment: payment.New(),
}
cmdRoot, err := newCommand(&q)
if err != nil {
log.Fatal(err)
}
if err := cmdRoot.Execute(); err != nil {
log.Fatal(err)
}
}
func newCommand(q *qrParams) (*cobra.Command, error) {
cmdRoot := &cobra.Command{
Use: "payme",
Version: fmt.Sprintf("%s (%s), built %s\n", gitRefName, gitCommit, buildTime),
Short: "Generate SEPA payment QR code",
Args: cobra.NoArgs,
Run: func(_ *cobra.Command, _ []string) {
q.generate()
},
}
cmdRoot.AddCommand(completionCmd(cmdRoot))
if err := q.init(cmdRoot); err != nil {
return nil, err
}
return cmdRoot, nil
}
func (q *qrParams) init(cmdRoot *cobra.Command) error {
viper.SetEnvPrefix("PAYME")
for _, e := range []string{"name", "bic", "iban"} {
if err := viper.BindEnv(e); err != nil {
return err
}
}
cmdRoot.Flags().StringVar(&q.OutputType, "output", "stdout", "output type: png or stdout")
cmdRoot.Flags().StringVar(&q.OutputFile, "file", "", "write code to file, leave empty for stdout")
cmdRoot.Flags().BoolVar(&q.Debug, "debug", false, "print debug output")
cmdRoot.Flags().IntVar(&q.Payment.CharacterSet, "character-set", 2, "QR code character set")
cmdRoot.Flags().IntVar(&q.Payment.Version, "qr-version", 2, "QR code version")
cmdRoot.Flags().StringVar(&q.Payment.NameBeneficiary, "name", viper.GetString("name"), "Name of the beneficiary")
cmdRoot.Flags().StringVar(&q.Payment.BICBeneficiary, "bic", viper.GetString("bic"), "BIC of the beneficiary")
cmdRoot.Flags().StringVar(&q.Payment.IBANBeneficiary, "iban", viper.GetString("iban"), "IBAN of the beneficiary")
cmdRoot.Flags().Float64Var(&q.Payment.EuroAmount, "amount", 0, "Amount of the transaction")
cmdRoot.Flags().StringVar(&q.Payment.Remittance, "remittance", "", "Remittance (message)")
cmdRoot.Flags().StringVar(&q.Payment.Purpose, "purpose", "", "Purpose of the transaction")
cmdRoot.Flags().BoolVar(&q.Payment.RemittanceIsStructured, "structured", false, "Make the remittance (message) structured")
return nil
}
func (q *qrParams) generate() {
var (
qr []byte
err error
)
if q.Debug {
log.Printf("%#v\n", q)
}
switch q.OutputType {
case "png":
qr, err = q.generateQRPNG()
case "stdout":
qr, err = q.generateQRStdout()
}
if err != nil {
log.Fatal(err)
}
if q.OutputFile == "" {
fmt.Fprintf(os.Stdout, "%s", qr)
return
}
err = os.WriteFile(q.OutputFile, qr, 0o600)
if err != nil {
log.Fatal(err)
}
}
func (q *qrParams) generateQRStdout() ([]byte, error) {
p := q.Payment
if q.Debug {
s, err := p.ToString()
if err != nil {
return nil, err
}
log.Print("Data: ", s)
}
return p.ToQRBytes()
}
func (q *qrParams) generateQRPNG() ([]byte, error) {
p := q.Payment
if q.Debug {
s, err := p.ToString()
if err != nil {
return nil, err
}
log.Print("Data: ", s)
}
return p.ToQRPNG(qrSize)
}