-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
81 lines (68 loc) · 1.49 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
// The gobl command provides a command-line interface to the GOBL library.
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"os/signal"
"syscall"
"github.com/spf13/cobra"
"github.com/invopop/gobl"
)
// build data provided by goreleaser and mage setup
var (
version = "dev"
date = ""
)
var versionOutput = struct {
Version string `json:"version"`
GOBL string `json:"gobl"`
Date string `json:"date,omitempty"`
}{
Version: version,
GOBL: string(gobl.VERSION),
Date: date,
}
func main() {
if err := run(); err != nil {
printError(err)
os.Exit(1)
}
}
func run() error {
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
return root().cmd().ExecuteContext(ctx)
}
func inputFilename(args []string) string {
if len(args) > 0 && args[0] != "-" {
return args[0]
}
return ""
}
func versionCmd() *cobra.Command {
return &cobra.Command{
Use: "version",
RunE: func(cmd *cobra.Command, _ []string) error {
enc := json.NewEncoder(cmd.OutOrStdout())
enc.SetIndent("", "\t") // always indent version
return enc.Encode(versionOutput)
},
}
}
func encode(in any, out io.WriteCloser, indent bool) error {
enc := json.NewEncoder(out)
if indent {
enc.SetIndent("", "\t")
}
return enc.Encode(in)
}
func printError(err error) {
enc := json.NewEncoder(os.Stderr)
enc.SetIndent("", "\t") // always indent errors
if err = enc.Encode(err); err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
}
}