This repository has been archived by the owner on Jul 3, 2019. It is now read-only.
/
functions.go
182 lines (160 loc) · 3.89 KB
/
functions.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package tools
import (
"bytes"
"fmt"
"go/build"
"go/format"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/huandu/xstrings"
)
type TemplateConfig struct {
OutputPath string
Template string
Parameter interface{}
PreventOverwriting bool
}
func WriteFileWithTemplate(config *TemplateConfig) bool {
buf := bytes.NewBufferString("")
t := template.New("t")
template.Must(t.Parse(config.Template))
if err := t.Execute(buf, config.Parameter); err != nil {
log.Fatalf("writing output: %s", err)
}
if config.PreventOverwriting {
if _, err := os.Stat(config.OutputPath); err == nil {
return false
}
}
// write to file
if err := ioutil.WriteFile(config.OutputPath, Sformat(buf.Bytes()), 0644); err != nil {
log.Fatalf("writing output: %s", err)
}
return true
}
// Gopath returns GOPATH
func Gopath() string {
gopath := build.Default.GOPATH
gopath = filepath.SplitList(gopath)[0]
return gopath
}
func ProjectRootPath() string {
return filepath.Join(Gopath(), "src/github.com/sacloud/libsacloud-v2")
}
// Sformat formats go source codes
func Sformat(buf []byte) []byte {
src, err := format.Source(buf)
if err != nil {
// Should never happen, but can arise when developing this code.
// The user can compile the output to see the error.
log.Printf("warning: internal error: invalid Go generated: %s", err)
log.Printf("warning: compile the package to analyze the error")
log.Printf("generated: \n%s", string(buf))
return buf
}
return src
}
var normalizationWords = map[string]string{
"IP": "ip",
}
// NormalizeResourceName
func NormalizeResourceName(name string) string {
n := name
for k, v := range normalizationWords {
if strings.HasPrefix(name, k) {
n = strings.Replace(name, k, v, -1)
break
}
}
return n
}
func ToSnakeCaseName(name string) string {
return strings.Replace(xstrings.ToSnakeCase(NormalizeResourceName(name)), "-", "_", -1)
}
func ToDashedName(name string) string {
// From "CamelCase" to "dash-case"
return strings.Replace(xstrings.ToSnakeCase(NormalizeResourceName(name)), "_", "-", -1)
}
func ToCamelCaseName(name string) string {
return xstrings.ToCamelCase(strings.Replace(NormalizeResourceName(name), "-", "_", -1))
}
func ToCamelWithFirstLower(name string) string {
return xstrings.FirstRuneToLower(xstrings.ToCamelCase(strings.Replace(NormalizeResourceName(name), "-", "_", -1)))
}
func ToCLIFlagName(name string) string {
format := "--%s"
if len(name) == 1 {
format = "-%s"
}
return fmt.Sprintf(format, ToDashedName(name))
}
func FlattenStringList(list []string) string {
if len(list) > 0 {
return fmt.Sprintf("\"%s\"", strings.Join(list, "\",\""))
}
return ""
}
func FlattenIntList(list []int) string {
if len(list) > 0 {
tmp := []string{}
for _, s := range list {
tmp = append(tmp, fmt.Sprintf("%d", s))
}
return strings.Join(tmp, ",")
}
return ""
}
func FlattenUintList(list []uint) string {
if len(list) > 0 {
tmp := []string{}
for _, s := range list {
tmp = append(tmp, fmt.Sprintf("%d", s))
}
return strings.Join(tmp, ",")
}
return ""
}
func FlattenInt64List(list []int64) string {
if len(list) > 0 {
tmp := []string{}
for _, s := range list {
tmp = append(tmp, fmt.Sprintf("%d", s))
}
return strings.Join(tmp, ",")
}
return ""
}
func FlattenUint64List(list []uint64) string {
if len(list) > 0 {
tmp := []string{}
for _, s := range list {
tmp = append(tmp, fmt.Sprintf("%d", s))
}
return strings.Join(tmp, ",")
}
return ""
}
func FlattenFloatList(list []float32) string {
if len(list) > 0 {
tmp := []string{}
for _, s := range list {
tmp = append(tmp, fmt.Sprintf("%f", s))
}
return strings.Join(tmp, ",")
}
return ""
}
func FlattenFloat64List(list []float64) string {
if len(list) > 0 {
tmp := []string{}
for _, s := range list {
tmp = append(tmp, fmt.Sprintf("%f", s))
}
return strings.Join(tmp, ",")
}
return ""
}