This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
/
root.go
175 lines (155 loc) · 4.49 KB
/
root.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
169
170
171
172
173
174
175
package cmd
import (
"errors"
"fmt"
"os"
"path"
"github.com/k14s/starlark-go/starlark"
"github.com/k14s/ytt/pkg/yttlibrary"
"github.com/k14s/ytt/pkg/yttlibrary/overlay"
"github.com/sap/kubernetes-deployment-orchestrator/pkg/extensions"
"github.com/sap/kubernetes-deployment-orchestrator/pkg/k8s"
"github.com/sap/kubernetes-deployment-orchestrator/pkg/kdo"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
var repoConfigFile string
var repoConfigFileDefault string
var rootOsbConfig = extensions.OsbConfig{}
func init() {
homedir, err := os.UserHomeDir()
if err != nil {
panic(err)
}
repoConfigFileDefault = path.Join(homedir, ".kdo", "config")
rootCmd.AddCommand(applyCmd)
rootCmd.AddCommand(templateCmd)
rootCmd.AddCommand(deleteCmd)
rootCmd.AddCommand(packageCmd)
rootCmd.AddCommand(controllerCmd)
rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(testCmd)
rootCmd.AddCommand(listCmd)
rootCmd.PersistentFlags().StringVar(&repoConfigFile, "config", repoConfigFileDefault, "kdo configuration file (e.g. credentials)")
}
func repo() (kdo.Repo, error) {
if repoConfigFile == repoConfigFileDefault {
if _, err := os.Stat(repoConfigFile); err != nil {
return kdo.NewRepo()
}
}
return kdo.NewRepo(kdo.WithConfigFile(repoConfigFile))
}
var rootCmd = &cobra.Command{
Use: "kdo",
Short: "Kubernete deployment orchestrator brings the starlark language to helm charts",
Long: ``,
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
return fmt.Errorf("Invalid command %s", args[0])
},
}
var rootExecuteOptions = ExecuteOptions{load: defaultLoad}
// ExecuteOptions -
type ExecuteOptions struct {
load func(thread *starlark.Thread, module string) (dict starlark.StringDict, err error)
}
func defaultLoad(thread *starlark.Thread, module string) (starlark.StringDict, error) {
switch module {
case "@ytt:base64":
return yttlibrary.Base64API, nil
case "@ytt:json":
return yttlibrary.JSONAPI, nil
case "@ytt:md5":
return yttlibrary.MD5API, nil
case "@ytt:regexp":
return yttlibrary.RegexpAPI, nil
case "@ytt:sha256":
return yttlibrary.SHA256API, nil
case "@ytt:url":
return yttlibrary.URLAPI, nil
case "@ytt:yaml":
return yttlibrary.YAMLAPI, nil
case "@ytt:overlay":
return overlay.API, nil
case "@ytt:struct":
return yttlibrary.StructAPI, nil
case "@ytt:module":
return yttlibrary.ModuleAPI, nil
case "@kdo:bcrypt":
return BcryptAPI, nil
case "@kdo:osb":
return extensions.OsbAPI(rootOsbConfig), nil
}
return nil, fmt.Errorf("Unknown module '%s'", module)
}
// ExecuteOption -
type ExecuteOption func(e *ExecuteOptions)
// WithModules add new modules to kdo, which can be loaded using load inside starlark scripts
func WithModules(load func(thread *starlark.Thread, module string) (starlark.StringDict, error)) ExecuteOption {
return func(e *ExecuteOptions) {
oldLoad := e.load
e.load = func(thread *starlark.Thread, module string) (starlark.StringDict, error) {
dict, err := load(thread, module)
if err != nil {
return oldLoad(thread, module)
}
return dict, nil
}
}
}
// WithFlags adds global command line options
func WithFlags(flags func(flagsSet *pflag.FlagSet)) ExecuteOption {
return func(e *ExecuteOptions) {
flags(rootCmd.PersistentFlags())
}
}
// WithApplyFlags adds global command line options to apply, delete and test
func WithApplyFlags(flags func(flagsSet *pflag.FlagSet)) ExecuteOption {
return func(e *ExecuteOptions) {
flags(applyCmd.Flags())
flags(deleteCmd.Flags())
}
}
// WithTestFlags adds global command line options to apply, delete and test
func WithTestFlags(flags func(flagsSet *pflag.FlagSet)) ExecuteOption {
return func(e *ExecuteOptions) {
flags(testCmd.Flags())
}
}
// WithK8s overrides constructor for k8s
func WithK8s(k func(configs ...k8s.Config) (k8s.K8s, error)) ExecuteOption {
return func(e *ExecuteOptions) {
newK8s = k
}
}
// WithTestK8s overrides constructor for k8s
func WithTestK8s(k func(configs ...k8s.Config) (k8s.K8s, error)) ExecuteOption {
return func(e *ExecuteOptions) {
testK8s = k
}
}
// Execute executes the root command.
func Execute(executeOptions ...ExecuteOption) error {
for _, eo := range executeOptions {
eo(&rootExecuteOptions)
}
return rootCmd.Execute()
}
func unwrapEvalError(err error) error {
if err == nil {
return nil
}
evalError, ok := err.(*starlark.EvalError)
if ok {
return errors.New(evalError.Backtrace())
}
return err
}
func exit(err error) {
if err != nil {
fmt.Println(unwrapEvalError(err).Error())
os.Exit(1)
}
os.Exit(0)
}