/
generateConfig.go
134 lines (116 loc) · 3.52 KB
/
generateConfig.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
package cmd
import (
"embed"
"fmt"
"net/url"
"os"
"path"
"github.com/MakeNowJust/heredoc/v2"
"github.com/bcgov/gwa-cli/pkg"
"github.com/spf13/cobra"
)
//go:embed templates/*.go.tmpl
var templates embed.FS
type GenerateConfigOptions struct {
Namespace string
Template string
Service string
Upstream string
UpstreamUrl *url.URL
UpstreamPort string
Organization string
OrganizationUnit string
Out string
}
func (o *GenerateConfigOptions) ValidateTemplate() error {
if o.Template == "kong-httpbin" || o.Template == "client-credentials-shared-idp" {
return nil
}
return fmt.Errorf("%s is not a valid template", o.Template)
}
func (o *GenerateConfigOptions) Exec() error {
err := o.ValidateTemplate()
if err != nil {
return err
}
err = o.ParseUpstream()
if err != nil {
return err
}
return nil
}
func (o *GenerateConfigOptions) ParseUpstream() error {
upstreamUrl, err := url.Parse(o.Upstream)
if err != nil {
return err
}
o.UpstreamUrl = upstreamUrl
if upstreamUrl.Port() == "" {
if upstreamUrl.Scheme == "https" {
o.UpstreamPort = "443"
} else {
o.UpstreamPort = "80"
}
} else {
o.UpstreamPort = upstreamUrl.Port()
}
return nil
}
func NewGenerateConfigCmd(ctx *pkg.AppContext) *cobra.Command {
opts := &GenerateConfigOptions{}
var generateConfigCmd = &cobra.Command{
Use: "generate-config",
Short: "Generate gateway resources based on pre-defined templates",
Args: cobra.OnlyValidArgs,
Example: heredoc.Doc(`
$ gwa generate-config --template kong-httpbin --service my-service --upstream https://httpbin.org
$ gwa generate-config --template client-credentials-shared-idp --service my-service --upstream https://www.boredapi.com/api/activity
`),
RunE: func(_ *cobra.Command, _ []string) error {
opts.Namespace = ctx.Namespace
err := opts.Exec()
if err != nil {
return err
}
err = GenerateConfig(ctx, opts)
if err != nil {
return err
}
output := fmt.Sprintf("File %s created", opts.Out)
fmt.Println(pkg.PrintSuccess(output))
return nil
},
}
generateConfigCmd.Flags().StringVarP(&opts.Template, "template", "t", "", "Name of a pre-defined template (kong-httpbin, client-credentials-shared-idp)")
generateConfigCmd.Flags().StringVarP(&opts.Service, "service", "s", "", "A unique service subdomain for your vanity url: https://<service>.api.gov.bc.ca")
generateConfigCmd.Flags().StringVarP(&opts.Upstream, "upstream", "u", "", "The upstream implementation of the API")
generateConfigCmd.Flags().StringVar(&opts.Organization, "org", "ministry-of-citizens-services", "Set the organization")
generateConfigCmd.Flags().StringVar(&opts.OrganizationUnit, "org-unit", "databc", "Set the organization unit")
generateConfigCmd.Flags().StringVarP(&opts.Out, "out", "o", "gw-config.yml", "The file to output the generate config to")
generateConfigCmd.MarkFlagRequired("template")
generateConfigCmd.MarkFlagRequired("service")
generateConfigCmd.MarkFlagRequired("upstream")
return generateConfigCmd
}
func GenerateConfig(ctx *pkg.AppContext, opts *GenerateConfigOptions) error {
tmpl := pkg.NewTemplate()
tplContent, err := templates.ReadFile("templates/" + opts.Template + ".go.tmpl")
if err != nil {
return err
}
tmpl, err = tmpl.Parse(string(tplContent))
if err != nil {
return err
}
file, err := os.Create(path.Join(ctx.Cwd, opts.Out))
if err != nil {
return err
}
defer file.Close()
// Execute the template with the data.
err = tmpl.Execute(file, opts)
if err != nil {
return err
}
return nil
}