-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
86 lines (72 loc) · 2.19 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
82
83
84
85
86
package main
import (
"flag"
"fmt"
"os"
"runtime"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/roverdotcom/snagsby/pkg"
"github.com/roverdotcom/snagsby/pkg/app"
"github.com/roverdotcom/snagsby/pkg/config"
"github.com/roverdotcom/snagsby/pkg/formatters"
)
var (
showVersion = false
setFail = false
showSummary = false
)
var format string
func main() {
flagSet := flag.NewFlagSet("snagsby", flag.ExitOnError)
flagSet.Usage = func() {
fmt.Fprintf(os.Stderr, "Example usage: snagsby s3://my-bucket/my-config.json?region=us-west-2\n")
flagSet.PrintDefaults()
}
flagSet.BoolVar(&showVersion, "v", false, "print version string")
flagSet.BoolVar(&setFail, "e", false, "fail on errors")
flagSet.BoolVar(&showSummary, "show-summary", false, "Show summary")
flagSet.StringVar(&format, "o", "env", "Output")
flagSet.StringVar(&format, "output", "env", "Output")
flagSet.Parse(os.Args[1:])
if showVersion {
fmt.Printf("snagsby version %s (aws sdk: %s golang: %s)\n", pkg.Version, aws.SDKVersion, runtime.Version())
return
}
// Make sure we were given a valid formatter
formatter, ok := formatters.Formatters[format]
if !ok {
fmt.Fprintln(os.Stderr, "No formatter found")
os.Exit(2)
}
snagsbyConfig := config.NewConfig()
err := snagsbyConfig.SetSources(flagSet.Args(), os.Getenv("SNAGSBY_SOURCE"))
if err != nil {
fmt.Printf("Error parsing sources: %s\n", err)
os.Exit(1)
}
results := app.ResolveConfigSources(snagsbyConfig)
var resultsMap []map[string]string
for _, result := range results {
if result.HasErrors() {
// Print errors to stderr
fmt.Fprintln(os.Stderr, "Error processing snagsby source:", result.Source.URL.String())
for _, err := range result.Errors {
fmt.Fprintln(os.Stderr, err)
}
// Bail if we're exiting on failure
if setFail {
os.Exit(1)
}
continue
}
if showSummary {
fmt.Fprintf(os.Stderr, "%s (%d) => (%s)\n", result.Source.URL.String(), result.LenItems(), strings.Join(result.ItemKeys(), ", "))
}
resultsMap = append(resultsMap, result.Items)
}
// Merge together our rendered sources which are listed in the order they
// were specified.
all := formatters.Merge(resultsMap)
fmt.Print(formatter(all))
}