-
-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathrender.go
More file actions
92 lines (88 loc) · 2.57 KB
/
Copy pathrender.go
File metadata and controls
92 lines (88 loc) · 2.57 KB
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
package ecspresso
import (
"bufio"
"context"
"encoding/json"
"fmt"
"os"
"github.com/goccy/go-yaml"
)
type RenderOption struct {
Targets *[]string `arg:"" help:"target to render (config, service-definition, servicedef, task-definition, taskdef, express-definition, expressdef)" enum:"config,service-definition,servicedef,task-definition,taskdef,express-definition,expressdef"`
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.LogDebug("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 := toJsonnetString(string(b), "")
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 = toJsonnetString(s, d.config.ServiceDefinitionPath)
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 = toJsonnetString(s, d.config.TaskDefinitionPath)
if err != nil {
return fmt.Errorf("unable to format task definition as Jsonnet: %w", err)
}
}
if _, err := out.WriteString(s); err != nil {
return err
}
case "express-definition", "expressdef":
sv, err := d.LoadExpressDefinition(d.config.ExpressDefinitionPath)
if err != nil {
return err
}
s := MustMarshalJSONStringForAPI(sv)
if opt.Jsonnet {
s, err = toJsonnetString(s, d.config.ExpressDefinitionPath)
if err != nil {
return fmt.Errorf("unable to format express gateway service definition as Jsonnet: %w", err)
}
}
if _, err := out.WriteString(s); err != nil {
return err
}
default:
return fmt.Errorf("unknown target: %s", target)
}
}
return nil
}