forked from tellerops/teller
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
261 lines (217 loc) · 6.12 KB
/
main.go
File metadata and controls
261 lines (217 loc) · 6.12 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package main
import (
"fmt"
"io"
"os"
"github.com/alecthomas/kong"
"github.com/spectralops/teller/pkg"
)
var CLI struct {
Config string `short:"c" help:"Path to teller.yml"`
Run struct {
Redact bool `optional name:"redact" help:"Redact output of the child process"`
Cmd []string `arg name:"cmd" help:"Command to execute"`
} `cmd help:"Run a command"`
Version struct {
} `cmd short:"v" help:"Teller version"`
New struct {
} `cmd help:"Create a new teller configuration file"`
Show struct {
} `cmd help:"Print in a human friendly, secure format"`
Yaml struct {
} `cmd help:"Print values in a YAML format (suitable for GCloud)"`
JSON struct {
} `cmd help:"Print values in a JSON format"`
Sh struct {
} `cmd help:"Print ready to be eval'd exports for your shell"`
Env struct {
} `cmd help:"Print in a .env format for Docker and others"`
Template struct {
TemplateFile string `arg name:"template_file" help:"Input template file (Go template format)"`
OutFile string `arg name:"out_file" help:"Output file"`
} `cmd help:"Inject vars into a template file"`
Redact struct {
In string `optional name:"in" help:"Input file"`
Out string `optional name:"out" help:"Output file"`
} `cmd help:"Redacts secrets from a process output"`
Scan struct {
Path string `arg optional name:"path" help:"Scan root, default: '.'"`
Silent bool `optional name:"silent" help:"No text, just exit code"`
} `cmd help:"Scans your codebase for sensitive keys"`
GraphDrift struct {
Providers []string `arg optional name:"providers" help:"A list of providers to check for drift"`
} `cmd help:"Detect secret and value drift between providers"`
Put struct {
Kvs map[string]string `arg name:"kvs" help:"A list of key/value pairs, where key is from your tellerfile mapping"`
Providers []string `name:"providers" help:"A list of providers to put the new value into"`
Sync bool `optional name:"sync" help:"Sync all given k/vs to the env_sync key"`
Path string `optional name:"path" help:"Take literal path and not from config"`
} `cmd help:"Put a new value"`
Copy struct {
From string `name:"from" help:"A provider name to sync from"`
To []string `name:"to" help:"A list of provider names to copy values from the source provider to"`
Sync bool `optional name:"sync" help:"Sync all given k/vs to the env_sync key"`
} `cmd help:"Sync data from a source provider directly to multiple target providers"`
MirrorDrift struct {
Source string `name:"source" help:"A source to check drift against"`
Target string `name:"target" help:"A target to check against source"`
} `cmd help:"Check same-key (mirror) value drift between source and target"`
}
var (
version = "dev"
commit = "none"
date = "unknown"
)
//nolint
func main() {
ctx := kong.Parse(&CLI)
// below commands don't require a tellerfile
//nolint
switch ctx.Command() {
case "version":
fmt.Printf("Teller %v\n", version)
fmt.Printf("Revision %v, date: %v\n", commit, date)
os.Exit(0)
}
//
// load or create new file
//
telleryml := ".teller.yml"
if CLI.Config != "" {
telleryml = CLI.Config
}
if _, err := os.Stat(telleryml); os.IsNotExist(err) {
teller := pkg.Teller{
Porcelain: &pkg.Porcelain{Out: os.Stderr},
}
err = teller.SetupNewProject(telleryml)
if err != nil {
fmt.Printf("Error: %v", err)
os.Exit(1)
}
os.Exit(0)
}
tlrfile, err := pkg.NewTellerFile(telleryml)
if err != nil {
fmt.Printf("Error: %v", err)
os.Exit(1)
}
teller := pkg.NewTeller(tlrfile, CLI.Run.Cmd, CLI.Run.Redact)
// below commands don't require collecting
//nolint
switch ctx.Command() {
case "put <kvs>":
err := teller.Put(CLI.Put.Kvs, CLI.Put.Providers, CLI.Put.Sync, CLI.Put.Path)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
os.Exit(0)
case "copy":
err := teller.Sync(CLI.Copy.From, CLI.Copy.To, CLI.Copy.Sync)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
os.Exit(0)
case "mirror-drift":
drifts, err := teller.MirrorDrift(CLI.MirrorDrift.Source, CLI.MirrorDrift.Target)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
if len(drifts) > 0 {
teller.Porcelain.PrintDrift(drifts)
os.Exit(1)
}
os.Exit(0)
}
// collecting
err = teller.Collect()
if err != nil {
fmt.Printf("Error: %v", err)
os.Exit(1)
}
// all of the below require a tellerfile
switch ctx.Command() {
case "run <cmd>":
if len(CLI.Run.Cmd) < 1 {
fmt.Println("Error: No command given")
os.Exit(1)
}
teller.Exec()
case "graph-drift <providers>":
fallthrough
case "graph-drift":
drifts := teller.Drift(CLI.GraphDrift.Providers)
if len(drifts) > 0 {
teller.Porcelain.PrintDrift(drifts)
os.Exit(1)
}
case "redact":
// redact (stdin)
// redact --in FILE --out FOUT
// redact --in FILE (stdout)
var fin io.Reader = os.Stdin
var fout io.Writer = os.Stdout
if CLI.Redact.In != "" {
f, err := os.Open(CLI.Redact.In)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
fin = f
}
if CLI.Redact.Out != "" {
f, err := os.Create(CLI.Redact.Out)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
fout = f
}
if err := teller.RedactLines(fin, fout); err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
case "sh":
fmt.Print(teller.ExportEnv())
case "env":
fmt.Print(teller.ExportDotenv())
case "yaml":
out, err := teller.ExportYAML()
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
fmt.Print(out)
case "json":
out, err := teller.ExportJSON()
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
fmt.Print(out)
case "show":
teller.PrintEnvKeys()
case "scan":
findings, err := teller.Scan(CLI.Scan.Path, CLI.Scan.Silent)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
num := len(findings)
if num > 0 {
os.Exit(1)
}
case "template <template_file> <out_file>":
err := teller.TemplateFile(CLI.Template.TemplateFile, CLI.Template.OutFile)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
default:
println(ctx.Command())
teller.PrintEnvKeys()
}
}