-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.go
168 lines (140 loc) · 3.81 KB
/
cli.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"fmt"
"os"
"strings"
"traductio/internal/inputreader"
"traductio/internal/sink"
_ "traductio/internal/sink/influx"
_ "traductio/internal/sink/timestream"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)
type App struct {
cfgFile string
cfg struct {
vars []string
run struct {
stopAfter string
}
}
// entry point
Execute func() error
}
func NewApp() *App {
a := &App{}
appName := "traductio"
// root
rootCmd := &cobra.Command{
Use: appName,
Short: "Read data from JSON, process it and store the results in a time series database",
}
rootCmd.PersistentFlags().StringSliceVarP(&a.cfg.vars, "vars", "v", []string{}, "key:value pairs of variables to be used in the input templates")
rootCmd.PersistentFlags().StringVarP(&a.cfgFile, "cfg", "c", fmt.Sprintf("$HOME/%s.yaml", appName), "configuration file path")
a.Execute = rootCmd.Execute
// config
runCmd := &cobra.Command{
Use: "run",
Short: "Performs all steps",
Long: ``,
Run: a.runCmd,
}
runCmd.PersistentFlags().StringVar(&a.cfg.run.stopAfter, "stop-after", "", fmt.Sprintf("name of the step to stop afterwards, can be one of: %s", strings.Join(GetSteps(), ", ")))
rootCmd.AddCommand(runCmd)
// version
versionCmd := &cobra.Command{
Use: "version",
Short: "Print version info",
Run: a.versionCmd,
}
rootCmd.AddCommand(versionCmd)
return a
}
func (a *App) runCmd(cmd *cobra.Command, args []string) {
// validating the 'stopAfter' flag
if a.cfg.run.stopAfter != "" {
found := false
for _, stepName := range GetSteps() {
if stepName == a.cfg.run.stopAfter {
found = true
break
}
}
if found {
info(fmt.Sprintf("Running until step '%s'", a.cfg.run.stopAfter))
} else {
steps := strings.Join(GetSteps(), ", ")
err := fmt.Errorf("there is no step called '%s', should be one of the following: %s", a.cfg.run.stopAfter, steps)
exitOnErr(err)
}
}
// STEP ReadConfig
c, err := ReadConfig(a.cfgFile)
exitOnErr(err)
if a.cfg.run.stopAfter == StepReadConfig.String() {
info("Printing configuration file as read to STDOUT and exiting...")
fmt.Println(c)
return
}
// STEP PreFetch
vars, err := sliceToMap(a.cfg.vars, ":")
exitOnErr(err)
i, err := inputreader.NewInput(c.Input, vars)
exitOnErr(err)
if a.cfg.run.stopAfter == StepPreFetch.String() {
info("Printing rendered input data to STDOUT and exiting...")
b, _ := yaml.Marshal(i)
fmt.Println(string(b))
return
}
// STEP Fetch
data, err := i.Fetch()
exitOnErr(err)
if a.cfg.run.stopAfter == StepFetch.String() {
info("Printing fetched data to STDOUT and exiting...")
fmt.Println(string(data))
return
}
// STEP Validate
_, errs := c.Validators.ValidateContent(data)
exitOnErr(errs...)
if a.cfg.run.stopAfter == StepValidate.String() {
info("Validation was successful, exiting...")
return
}
// STEP Process
//points, _, fragment, err := Process(data, c.Process.Iterator, sink.Point{}, true)
points, _, _, err := Process(data, c.Process.Iterator, sink.Point{}, false)
exitOnErr(err)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
if a.cfg.run.stopAfter == StepProcess.String() {
info("Printing extracted points to STDOUT and last iterator fragment to STDERR and exiting...")
if !c.Process.NoTrim {
points, _, _ = sink.TrimPoints(points)
}
table, err := sink.PointsAsCSV(points, ",")
exitOnErr(err)
fmt.Println(string(table))
//info(fragment)
return
}
// STEP Store
fmt.Println("Going to create sink")
t, err := sink.New(c.Output)
exitOnErr(err)
if len(points) < 1 {
fmt.Println("No data points to save")
os.Exit(0)
}
fmt.Printf("Saving %d data points to sink\n", len(points))
err = t.Write(points)
defer t.Close()
exitOnErr(err)
fmt.Println("Data points saved")
}
func (a *App) versionCmd(cmd *cobra.Command, args []string) {
fmt.Println(versionInfo())
}