-
Notifications
You must be signed in to change notification settings - Fork 0
/
correct.go
77 lines (63 loc) · 1.64 KB
/
correct.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
package main
import (
"encoding/json"
"github.com/invopop/gobl.cli/internal"
"github.com/spf13/cobra"
)
type correctOpts struct {
*rootOpts
options bool // Present the JSON options object
data string // JSON Correction Options data
credit bool
debit bool
}
func correct(root *rootOpts) *correctOpts {
return &correctOpts{
rootOpts: root,
}
}
func (o *correctOpts) cmd() *cobra.Command {
cmd := &cobra.Command{
Args: cobra.MaximumNArgs(2),
RunE: o.runE,
Use: "correct [infile] [outfile]",
Short: "Build a corrective document from the provided input",
}
f := cmd.Flags()
f.BoolVarP(&o.options, "options", "", false, "Present the JSON correction options object.")
f.BoolVarP(&o.credit, "credit", "", false, "Generate a credit note or negative corrective document.")
f.BoolVarP(&o.debit, "debit", "", false, "Generate a debit note.")
f.StringVarP(&o.data, "data", "d", "", "JSON data for the correction options.")
return cmd
}
func (o *correctOpts) runE(cmd *cobra.Command, args []string) error {
ctx := commandContext(cmd)
input, err := openInput(cmd, args)
if err != nil {
return err
}
defer input.Close() // nolint:errcheck
out, err := o.openOutput(cmd, args)
if err != nil {
return err
}
defer out.Close() // nolint:errcheck
cOpts := &internal.CorrectOptions{
ParseOptions: &internal.ParseOptions{
Input: input,
},
OptionsSchema: o.options,
Credit: o.credit,
Debit: o.debit,
Data: []byte(o.data),
}
obj, err := internal.Correct(ctx, cOpts)
if err != nil {
return err
}
enc := json.NewEncoder(out)
if o.indent {
enc.SetIndent("", "\t")
}
return enc.Encode(obj)
}