-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.go
95 lines (80 loc) · 2.07 KB
/
build.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
package main
import (
"io"
"os"
"github.com/spf13/cobra"
"github.com/invopop/gobl.cli/internal"
)
type buildOpts struct {
*rootOpts
set map[string]string
setFiles map[string]string
setStrings map[string]string
template string
docType string
envelop bool
// Command options
use string
short string
}
func build(root *rootOpts) *buildOpts {
return &buildOpts{
rootOpts: root,
use: "build [infile] [outfile]",
short: "Calculate and validate a document, wrapping it in envelope if needed",
}
}
func (b *buildOpts) cmd() *cobra.Command {
cmd := &cobra.Command{
Args: cobra.MaximumNArgs(2),
RunE: b.runE,
Use: b.use,
Short: b.short,
}
f := cmd.Flags()
f.StringToStringVar(&b.set, "set", nil, "set value from the command line")
f.StringToStringVar(&b.setFiles, "set-file", nil, "set value from the specified YAML or JSON file")
f.StringToStringVar(&b.setStrings, "set-string", nil, "set STRING value from the command line")
f.StringVarP(&b.template, "template", "T", "", "template YAML/JSON file into which data is merged")
f.StringVarP(&b.docType, "type", "t", "", "specify the document type")
f.BoolVarP(&b.envelop, "envelop", "e", false, "insert document into an envelope")
return cmd
}
func (b *buildOpts) runE(cmd *cobra.Command, args []string) error {
ctx := commandContext(cmd)
var template io.Reader
if b.template != "" {
f, err := os.Open(b.template)
if err != nil {
return err
}
defer f.Close() // nolint:errcheck
template = f
}
input, err := openInput(cmd, args)
if err != nil {
return err
}
defer input.Close() // nolint:errcheck
out, err := b.openOutput(cmd, args)
if err != nil {
return err
}
defer out.Close() // nolint:errcheck
buildOpts := &internal.BuildOptions{
ParseOptions: &internal.ParseOptions{
Template: template,
Input: input,
SetFile: b.setFiles,
SetYAML: b.set,
SetString: b.setStrings,
DocType: b.docType,
Envelop: b.envelop,
},
}
res, err := internal.Build(ctx, buildOpts)
if err != nil {
return err
}
return encode(res, out, b.indent)
}