-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
144 lines (129 loc) · 2.98 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
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
package main
import (
"context"
"fmt"
"os"
"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
"github.com/spf13/cobra"
"github.com/giantswarm/konfigure/cmd/fetchkeys"
"github.com/giantswarm/konfigure/cmd/generate"
"github.com/giantswarm/konfigure/cmd/kustomizepatch"
"github.com/giantswarm/konfigure/cmd/lint"
"github.com/giantswarm/konfigure/pkg/project"
)
const (
konfigureModeEnvVar = "KONFIGURE_MODE"
)
func main() {
err := mainE(context.Background())
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", microerror.Pretty(microerror.Mask(err), true))
os.Exit(1)
}
}
func mainE(ctx context.Context) error {
var err error
var logger micrologger.Logger
{
c := micrologger.Config{}
logger, err = micrologger.New(c)
if err != nil {
return microerror.Mask(err)
}
}
// Create a new microkit command which manages our custom microservice.
newCommand := &cobra.Command{
Use: project.Name(),
Long: project.Description(),
Version: commandVersion(),
}
// Add sub-commands
subcommands := []*cobra.Command{}
{
c := fetchkeys.Config{
Logger: logger,
}
cmd, err := fetchkeys.New(c)
if err != nil {
return microerror.Mask(err)
}
subcommands = append(subcommands, cmd)
}
{
c := generate.Config{
Logger: logger,
}
cmd, err := generate.New(c)
if err != nil {
return microerror.Mask(err)
}
subcommands = append(subcommands, cmd)
}
{
c := kustomizepatch.Config{
Logger: logger,
}
cmd, err := kustomizepatch.New(c)
if err != nil {
return microerror.Mask(err)
}
subcommands = append(subcommands, cmd)
// Make kustomizepatch the main command if konfigure is running in
// container as a kustomize plugin. Kustomize does not know how to call
// sub-commands. This is enabled by setting KONFIGURE_MODE:
// "kustomizepatch" environment variable.
if v := os.Getenv(konfigureModeEnvVar); v == "kustomizepatch" {
cmd.SilenceErrors = true
cmd.SilenceUsage = true
err = cmd.Execute()
if err != nil {
_, err := fmt.Fprint(os.Stderr, microerror.Pretty(err, false))
if err != nil {
return err
}
os.Exit(1)
}
return nil
}
}
{
c := lint.Config{
Logger: logger,
}
cmd, err := lint.New(c)
if err != nil {
return microerror.Mask(err)
}
subcommands = append(subcommands, cmd)
}
{
cmd := &cobra.Command{
Use: "version",
Short: "Display version information.",
Long: "Display version information.",
Run: func(_ *cobra.Command, _ []string) {
fmt.Println(commandVersion())
},
}
subcommands = append(subcommands, cmd)
}
newCommand.SilenceErrors = true
newCommand.SilenceUsage = true
newCommand.AddCommand(subcommands...)
err = newCommand.Execute()
if err != nil {
return microerror.Mask(err)
}
return nil
}
func commandVersion() string {
return fmt.Sprintf(
"Description: %s\nGitCommit: %s\nName: %s\nSource: %s\nVersion: %s",
project.Description(),
project.GitSHA(),
project.Name(),
project.Source(),
project.Version(),
)
}