forked from fujiwara/lambroll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
125 lines (112 loc) · 2.86 KB
/
utils.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
package lambroll
import (
"bytes"
"encoding/json"
"fmt"
"log"
"os"
"strings"
"github.com/Songmu/prompter"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/google/go-jsonnet/formatter"
)
func (app *App) saveFile(path string, b []byte, mode os.FileMode, force bool) error {
if _, err := os.Stat(path); err == nil {
ok := force || prompter.YN(fmt.Sprintf("Overwrite existing file %s?", path), false)
if !ok {
return nil
}
}
return os.WriteFile(path, b, mode)
}
func toGeneralMap(s any, omitEmpty bool) (any, error) {
b, err := json.Marshal(s)
if err != nil {
return nil, err
}
x := make(map[string]any)
if err := json.Unmarshal(b, &x); err != nil {
return nil, err
}
if omitEmpty {
return omitEmptyValues(x), nil
}
return x, nil
}
func jsonStr(s any) string {
b, err := json.MarshalIndent(s, "", " ")
if err != nil {
log.Printf("[warn] failed to marshal json: %s", err)
}
return string(b)
}
func marshalJSON(s interface{}) ([]byte, error) {
x, err := toGeneralMap(s, true)
if err != nil {
return nil, err
}
if b, err := json.MarshalIndent(x, "", " "); err != nil {
return nil, err
} else {
return append(b, '\n'), nil
}
}
func marshalAny(s interface{}) (interface{}, error) {
b, err := marshalJSON(s)
if err != nil {
return nil, err
}
var res interface{}
if err := json.NewDecoder(bytes.NewReader(b)).Decode(&res); err != nil {
return nil, err
}
return res, nil
}
func unmarshalJSON(src []byte, v interface{}, path string) error {
strict := json.NewDecoder(bytes.NewReader(src))
strict.DisallowUnknownFields()
if err := strict.Decode(&v); err != nil {
if !strings.Contains(err.Error(), "unknown field") {
return err
}
log.Printf("[warn] %s in %s", err, path)
// unknown field -> try lax decoder
lax := json.NewDecoder(bytes.NewReader(src))
return lax.Decode(&v)
}
return nil
}
func findDefinitionFile(preferred string, defaults []string) (string, error) {
if preferred != "" {
if _, err := os.Stat(preferred); err == nil {
return preferred, nil
} else {
return "", err
}
}
for _, name := range defaults {
if _, err := os.Stat(name); err == nil {
return name, nil
}
}
return "", fmt.Errorf("function file (%s) not found", strings.Join(DefaultFunctionFilenames, " or "))
}
func jsonToJsonnet(src []byte, filepath string) ([]byte, error) {
s, err := formatter.Format(filepath, string(src), formatter.DefaultOptions())
if err != nil {
return nil, fmt.Errorf("failed to format jsonnet: %w", err)
}
return []byte(s), nil
}
func resolveLogGroup(fn *Function) string {
if fn.LoggingConfig != nil && fn.LoggingConfig.LogGroup != nil {
return *fn.LoggingConfig.LogGroup
}
return fmt.Sprintf("/aws/lambda/%s", aws.ToString(fn.FunctionName))
}
func fullQualifiedFunctionName(name string, qualifier *string) string {
if qualifier != nil {
return name + ":" + *qualifier
}
return name + ":" + versionLatest
}