forked from thanos-io/thanos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
111 lines (93 loc) · 2.9 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
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
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"strings"
"github.com/fatih/structtag"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/improbable-eng/thanos/pkg/objstore/azure"
"github.com/improbable-eng/thanos/pkg/objstore/client"
"github.com/improbable-eng/thanos/pkg/objstore/cos"
"github.com/improbable-eng/thanos/pkg/objstore/gcs"
"github.com/improbable-eng/thanos/pkg/objstore/s3"
"github.com/improbable-eng/thanos/pkg/objstore/swift"
"github.com/pkg/errors"
kingpin "gopkg.in/alecthomas/kingpin.v2"
yaml "gopkg.in/yaml.v2"
)
var (
configs = map[client.ObjProvider]interface{}{
client.AZURE: azure.Config{},
client.GCS: gcs.Config{},
client.S3: s3.Config{},
client.SWIFT: swift.SwiftConfig{},
client.COS: cos.Config{},
}
)
func main() {
app := kingpin.New(filepath.Base(os.Args[0]), "Thanos bucket configs examples generator.")
app.HelpFlag.Short('h')
outputDir := app.Flag("output-dir", "Output directory for generated examples.").String()
logger := log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
_, err := app.Parse(os.Args[1:])
if err != nil {
level.Error(logger).Log("err", err)
os.Exit(1)
}
for typ, config := range configs {
fullCfg := client.BucketConfig{Type: typ, Config: config}
// We forbid omitempty option. This is for simplification for doc generation.
if err := checkForOmitEmptyTagOption(fullCfg); err != nil {
level.Error(logger).Log("type", typ, "err", err)
os.Exit(1)
}
out, err := yaml.Marshal(fullCfg)
if err != nil {
level.Error(logger).Log("msg", "failed to marshal config", "type", typ, "err", err)
os.Exit(1)
}
if err := ioutil.WriteFile(filepath.Join(*outputDir, fmt.Sprintf("config_%s.txt", strings.ToLower(string(typ)))), out, os.ModePerm); err != nil {
level.Error(logger).Log("msg", "failed to write", "type", typ, "err", err)
os.Exit(1)
}
}
logger.Log("msg", "success")
}
func checkForOmitEmptyTagOption(obj interface{}) error {
return checkForOmitEmptyTagOptionRec(reflect.ValueOf(obj))
}
func checkForOmitEmptyTagOptionRec(v reflect.Value) error {
switch v.Kind() {
case reflect.Struct:
for i := 0; i < v.NumField(); i += 1 {
tags, err := structtag.Parse(string(v.Type().Field(i).Tag))
if err != nil {
return err
}
tag, err := tags.Get("yaml")
if err != nil {
return err
}
for _, opts := range tag.Options {
if opts == "omitempty" {
return errors.Errorf("omitempty is forbidden for bucketConfig, but spotted on field '%s'", v.Type().Field(i).Name)
}
}
if err := checkForOmitEmptyTagOptionRec(v.Field(i)); err != nil {
return err
}
}
case reflect.Ptr:
if !v.IsValid() {
return errors.New("nil pointers are not allowed in configuration.")
}
return errors.New("nil pointers are not allowed in configuration.")
case reflect.Interface:
return checkForOmitEmptyTagOptionRec(v.Elem())
}
return nil
}