-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
120 lines (98 loc) · 2.63 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
package main
import (
"fmt"
"io"
"os"
"github.com/sirupsen/logrus"
"sigs.k8s.io/yaml"
"github.com/vshn/appcat-cli/internal/applications"
"github.com/vshn/appcat-cli/internal/util"
)
func init() {
logrus.SetOutput(os.Stderr)
}
func printUsage(cmd string, apps applications.AppMap) {
out := fmt.Sprintf(`usage: %s <serviceName> --kind <serviceKind> [options]
Generate AppCat YAML manifests
Known service kinds:
`, cmd)
names := apps.Names()
longest := util.Longest(names)
format := fmt.Sprintf(" %%-%ds (%%s.%%s)\n", longest+2)
for _, name := range names {
app := apps[name]
out += fmt.Sprintf(format, name, app.Kind, app.APIVersion)
}
fmt.Fprintln(os.Stderr, out)
}
func main() {
apps := applications.MakeAppMap()
code := Main(apps, os.Args, os.Stdout)
os.Exit(code)
}
// Main function
//
// Separated from `main` for testing purposes.
//
// # Errors
//
// If during executions, error occur due to user input errors, an appropriate
// error message is logged, and a non-zero exit code is returned.
//
// # Panics
//
// If during execution, an unrecoverable error occurs (usually due to a bug),
// an error message is logged and the program will panic.
func Main(apps applications.AppMap, args []string, out io.Writer) int {
if len(args) < 3 {
printUsage(args[0], apps)
return 1
}
var err error
var parameters []util.Input
// args[0] is the binary
resourceName := args[1]
parameters, err = util.ParseArgs(args[2:])
if err != nil {
logrus.Errorf("Failed parsing arguments: %s", err)
return 1
}
serviceKind, err := util.FilterServiceKind(parameters)
if err != nil {
logrus.Errorf("%s", err)
printUsage(args[0], apps)
return 1
}
serviceKind = util.NormalizeName(serviceKind)
app, ok := apps[serviceKind]
if !ok {
logrus.Errorf("service type '%s' is not supported", serviceKind)
printUsage(args[0], apps)
return 1
}
service := app.GetDefault()
parameters = append(parameters, util.Input{ParameterHierarchy: []string{"ObjectMeta", "Name"}, Value: resourceName, Unset: false})
parameters = append([]util.Input{{ParameterHierarchy: []string{"Spec", "WriteConnectionSecretToRef", "Name"}, Value: resourceName + "-creds", Unset: false}}, parameters...)
_, err = util.DecorateType(service, parameters)
if err != nil {
logrus.Errorf("failed setting parameters: %s", err)
return 1
}
err = writeYAML(service, out)
if err != nil {
logrus.Panicf("failed writing YAML: %s", err)
return 1
}
return 0
}
func writeYAML(service interface{}, out io.Writer) error {
outYaml, err := yaml.Marshal(service)
if err != nil {
return err
}
_, err = out.Write(outYaml)
if err != nil {
return err
}
return nil
}