-
-
Notifications
You must be signed in to change notification settings - Fork 89
/
render.go
78 lines (74 loc) · 2.18 KB
/
render.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
package ecspresso
import (
"bufio"
"context"
"encoding/json"
"fmt"
"os"
"github.com/goccy/go-yaml"
"github.com/google/go-jsonnet/formatter"
)
type RenderOption struct {
Targets *[]string `arg:"" help:"target to render (config, service-definition, servicedef, task-definition, taskdef)" enum:"config,service-definition,servicedef,task-definition,taskdef"`
Jsonnet bool `help:"render as jsonnet format" default:"false"`
}
func (d *App) Render(ctx context.Context, opt RenderOption) error {
out := bufio.NewWriter(os.Stdout)
defer out.Flush()
d.Log("[DEBUG] targets %v", opt.Targets)
for _, target := range *opt.Targets {
switch target {
case "config":
if opt.Jsonnet {
b, err := json.MarshalIndent(d.config, "", " ")
if err != nil {
return fmt.Errorf("unable to marshal config to JSON: %w", err)
}
s, err := formatter.Format("", string(b), formatter.DefaultOptions())
if err != nil {
return fmt.Errorf("unable to format config as Jsonnet: %w", err)
}
if _, err := out.WriteString(s); err != nil {
return err
}
} else {
if err := yaml.NewEncoder(out).Encode(d.config); err != nil {
return err
}
}
case "service-definition", "servicedef":
sv, err := d.LoadServiceDefinition(d.config.ServiceDefinitionPath)
if err != nil {
return err
}
s := MustMarshalJSONStringForAPI(sv)
if opt.Jsonnet {
s, err = formatter.Format(d.config.ServiceDefinitionPath, s, formatter.DefaultOptions())
if err != nil {
return fmt.Errorf("unable to format service definition as Jsonnet: %w", err)
}
}
if _, err = out.WriteString(s); err != nil {
return err
}
case "task-definition", "taskdef":
td, err := d.LoadTaskDefinition(d.config.TaskDefinitionPath)
if err != nil {
return err
}
s := MustMarshalJSONStringForAPI(td)
if opt.Jsonnet {
s, err = formatter.Format(d.config.TaskDefinitionPath, s, formatter.DefaultOptions())
if err != nil {
return fmt.Errorf("unable to format task definition as Jsonnet: %w", err)
}
}
if _, err := out.WriteString(s); err != nil {
return err
}
default:
return fmt.Errorf("unknown target: %s", target)
}
}
return nil
}