Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tooling/templatize: use typed options, functional core #725

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
232 changes: 230 additions & 2 deletions go.work.sum

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions tooling/templatize/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package config

import (
"bytes"
"context"
"text/template"

"gopkg.in/yaml.v3"
)

type Provider interface {
GetVariables(cloud, deployEnv string) (Variables, error)
}

func NewConfigProvider(config, region, user string) *configProviderImpl {
return &configProviderImpl{
config: config,
Expand All @@ -17,7 +20,7 @@ func NewConfigProvider(config, region, user string) *configProviderImpl {
}

// get the variables toke effect finally for cloud/deployEnv/region
func (cp *configProviderImpl) GetVariables(ctx context.Context, cloud, deployEnv string) (Variables, error) {
func (cp *configProviderImpl) GetVariables(cloud, deployEnv string) (Variables, error) {
variableOverrides, err := cp.loadConfig()
variables := Variables{}

Expand Down
4 changes: 1 addition & 3 deletions tooling/templatize/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package config

import (
"context"
"fmt"
"testing"

Expand All @@ -13,9 +12,8 @@ func TestConfigProvider(t *testing.T) {
user := "chiac"

configProvider := NewConfigProvider("../testdata/config.yaml", region, user)
ctx := context.Background()

variables, err := configProvider.GetVariables(ctx, "public", "int")
variables, err := configProvider.GetVariables("public", "int")
assert.NoError(t, err)
assert.NotNil(t, variables)

Expand Down
183 changes: 104 additions & 79 deletions tooling/templatize/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,41 @@ package main

import (
"fmt"
"io"
"io/fs"
"log"
"os"
"path/filepath"

"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/sets"

"github.com/Azure/ARO-HCP/tooling/templatize/config"
)

func DefaultGenerationOptions() *GenerationOptions {
return &GenerationOptions{}
func DefaultGenerationOptions() *RawGenerationOptions {
return &RawGenerationOptions{}
}

type GenerationOptions struct {
func BindGenerationOptions(opts *RawGenerationOptions, cmd *cobra.Command) error {
cmd.Flags().StringVar(&opts.ConfigFile, "config-file", opts.ConfigFile, "config file path")
cmd.Flags().StringVar(&opts.Input, "input", opts.Input, "input file path")
cmd.Flags().StringVar(&opts.Output, "output", opts.Output, "output file directory")
cmd.Flags().StringVar(&opts.Cloud, "cloud", opts.Cloud, "the cloud (public, fairfax)")
cmd.Flags().StringVar(&opts.DeployEnv, "deploy-env", opts.DeployEnv, "the deploy environment")
cmd.Flags().StringVar(&opts.Region, "region", opts.Region, "resources location")
cmd.Flags().StringVar(&opts.User, "user", opts.User, "unique user name")

for _, flag := range []string{"config-file", "input", "output"} {
if err := cmd.MarkFlagFilename("config-file"); err != nil {
return fmt.Errorf("failed to mark flag %q as a file: %w", flag, err)
}
}
return nil
}

// RawGenerationOptions holds input values.
type RawGenerationOptions struct {
ConfigFile string
Input string
Output string
Expand All @@ -23,44 +46,70 @@ type GenerationOptions struct {
User string
}

func (opts *GenerationOptions) Validate() error {
var errs []error
err := opts.validateFileAvailability("config-file", opts.ConfigFile)
if err != nil {
errs = append(errs, err)
func (o *RawGenerationOptions) Validate() (*ValidatedGenerationOptions, error) {
validClouds := sets.NewString("public", "fairfax")
if !validClouds.Has(o.Cloud) {
return nil, fmt.Errorf("invalid cloud %s, must be one of %v", o.Cloud, validClouds.List())
}
err = opts.validateFileAvailability("input", opts.Input)

// TODO: validate the environments, ensure a user is not passed for prod, etc

return &ValidatedGenerationOptions{
validatedGenerationOptions: &validatedGenerationOptions{
RawGenerationOptions: o,
},
}, nil
}

// validatedGenerationOptions is a private wrapper that enforces a call of Validate() before Complete() can be invoked.
type validatedGenerationOptions struct {
*RawGenerationOptions
}

type ValidatedGenerationOptions struct {
// Embed a private pointer that cannot be instantiated outside of this package.
*validatedGenerationOptions
}

func (o *ValidatedGenerationOptions) Complete() (*GenerationOptions, error) {
cfg := config.NewConfigProvider(o.ConfigFile, o.Region, o.User)
vars, err := cfg.GetVariables(o.Cloud, o.DeployEnv)
if err != nil {
errs = append(errs, err)
return nil, fmt.Errorf("failed to get variables for cloud %s: %w", o.Cloud, err)
}

// validate cloud
clouds := []string{"public", "fairfax"}
found := false
for _, c := range clouds {
if c == opts.Cloud {
found = true
break
}
inputFile := filepath.Base(o.Input)

if err := os.MkdirAll(o.Output, os.ModePerm); err != nil {
return nil, fmt.Errorf("failed to create output directory %s: %w", o.Output, err)
}
if !found {
errs = append(errs, fmt.Errorf("parameter cloud must be one of %v", clouds))

output, err := os.Create(filepath.Join(o.Output, inputFile))
if err != nil {
return nil, fmt.Errorf("failed to create output file %s: %w", o.Input, err)
}

return errors.NewAggregate(errs)
return &GenerationOptions{
completedGenerationOptions: &completedGenerationOptions{
Config: vars,
Input: os.DirFS(filepath.Dir(o.Input)),
InputFile: inputFile,
Output: output,
},
}, nil
}

// completedGenerationOptions is a private wrapper that enforces a call of Complete() before config generation can be invoked.
type completedGenerationOptions struct {
Config config.Variables
Input fs.FS
InputFile string
Output io.WriteCloser
}

func (opts *GenerationOptions) validateFileAvailability(param, path string) error {
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("file %s for parameter %s does not exist", path, param)
} else if os.IsPermission(err) {
return fmt.Errorf("no read permission for file %s", path)
} else {
return err
}
}
return nil
type GenerationOptions struct {
// Embed a private pointer that cannot be instantiated outside of this package.
*completedGenerationOptions
}

func main() {
Expand All @@ -70,57 +119,33 @@ func main() {
Short: "templatize",
Long: "templatize",
RunE: func(cmd *cobra.Command, args []string) error {
if err := opts.Validate(); err != nil {
return err
}

println("Config:", opts.ConfigFile)
println("Input:", opts.Input)
println("Cloud:", opts.Cloud)
println("Deployment Env:", opts.DeployEnv)
println("Region:", opts.Region)
println("User:", opts.User)

return opts.ExecuteTemplate(cmd.Context())
return executeTemplate(opts)
},
}
cmd.Flags().StringVar(&opts.ConfigFile, "config-file", opts.ConfigFile, "config file path")
cmd.Flags().StringVar(&opts.Input, "input", opts.Input, "input file path")
cmd.Flags().StringVar(&opts.Output, "output", opts.Output, "output file path")
cmd.Flags().StringVar(&opts.Cloud, "cloud", opts.Cloud, "the cloud (public, fairfax)")
cmd.Flags().StringVar(&opts.DeployEnv, "deploy-env", opts.DeployEnv, "the deploy environment")
cmd.Flags().StringVar(&opts.Region, "region", opts.Region, "resources location")
cmd.Flags().StringVar(&opts.User, "user", opts.User, "unique user name")

if err := cmd.MarkFlagFilename("config-file"); err != nil {
log.Fatalf("Error marking flag 'config-file': %v", err)
}
if err := cmd.MarkFlagRequired("config-file"); err != nil {
log.Fatalf("Error marking flag 'config-file' as required: %v", err)
}
if err := cmd.MarkFlagFilename("input"); err != nil {
log.Fatalf("Error marking flag 'input': %v", err)
}
if err := cmd.MarkFlagRequired("input"); err != nil {
log.Fatalf("Error marking flag 'input' as required: %v", err)
}
if err := cmd.MarkFlagFilename("output"); err != nil {
log.Fatalf("Error marking flag 'input': %v", err)
}
if err := cmd.MarkFlagRequired("output"); err != nil {
log.Fatalf("Error marking flag 'output' as required: %v", err)
}
if err := cmd.MarkFlagRequired("cloud"); err != nil {
log.Fatalf("Error marking flag 'cloud' as required: %v", err)
}
if err := cmd.MarkFlagRequired("deploy-env"); err != nil {
log.Fatalf("Error marking flag 'deploy-env' as required: %v", err)
}
if err := cmd.MarkFlagRequired("region"); err != nil {
log.Fatalf("Error marking flag 'region' as required: %v", err)
if err := BindGenerationOptions(opts, cmd); err != nil {
log.Fatal(err)
}

if err := cmd.Execute(); err != nil {
log.Fatal(err)
}
}

func executeTemplate(opts *RawGenerationOptions) error {
println("Config:", opts.ConfigFile)
println("Input:", opts.Input)
println("Cloud:", opts.Cloud)
println("Deployment Env:", opts.DeployEnv)
println("Region:", opts.Region)
println("User:", opts.User)

validated, err := opts.Validate()
if err != nil {
return err
}
completed, err := validated.Complete()
if err != nil {
return err
}
return completed.ExecuteTemplate()
}
38 changes: 10 additions & 28 deletions tooling/templatize/templatize.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,26 @@
package main

import (
"context"
"fmt"
"os"
"path"
"path/filepath"
"log"
"text/template"

"github.com/Azure/ARO-HCP/tooling/templatize/config"
)

func (opts *GenerationOptions) ExecuteTemplate(ctx context.Context) error {
cfg := config.NewConfigProvider(opts.ConfigFile, opts.Region, opts.User)
vars, err := cfg.GetVariables(ctx, opts.Cloud, opts.DeployEnv)
if err != nil {
return err
}

func (opts *GenerationOptions) ExecuteTemplate() error {
// print the vars
for k, v := range vars {
for k, v := range opts.Config {
fmt.Println(k, v)
}

fileName := filepath.Base(opts.Input)

if err := os.MkdirAll(opts.Output, os.ModePerm); err != nil {
return err
}

output, err := os.Create(path.Join(opts.Output, fileName))
if err != nil {
return err
}
defer output.Close()

tmpl, err := template.New(fileName).ParseFiles(opts.Input)
tmpl, err := template.New(opts.InputFile).ParseFS(opts.Input, opts.InputFile)
if err != nil {
return err
}

return tmpl.ExecuteTemplate(output, fileName, vars)
defer func() {
if err := opts.Output.Close(); err != nil {
log.Printf("error closing output: %v\n", err)
}
}()
return tmpl.ExecuteTemplate(opts.Output, opts.InputFile, opts.Config)
}
Loading
Loading