/
bash_arg.go
executable file
·68 lines (60 loc) · 1.71 KB
/
bash_arg.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
package graalsystems
import "strings"
var customBashNames = map[string]string{
"aclid": "acl-id",
"ipid": "ip-id",
"lbid": "lb-id",
"dhcpid": "dhcp-id",
}
// ToBashArg returns the Bash public name of the given string.
func ToBashArg(s string) string {
s = ToPublicGoName(s)
if customBashName, exists := customBashNames[strings.ToLower(s)]; exists {
return customBashName
}
for _, initialism := range customInitialisms {
// catch this kind of pattern: ExampleIDs ==> ExampleIds ==> example-ids
s = strings.Replace(s, initialism[0], strings.Title(strings.ToLower(initialism[0])), -1)
}
return toKebab(s)
}
// toKebab converts a string to kebab-case.
func toKebab(s string) string {
return toDelimited(s, '-')
}
// toDelimited converts a string to delimited lowercase.
func toDelimited(s string, del uint8) string {
s = strings.Trim(s, " ")
n := ""
for i, v := range s {
// treat acronyms as words, eg for JSONData -> JSON is a whole word
nextCaseIsChanged := false
if i+1 < len(s) {
next := s[i+1]
if (isUpperLetter(v) && isLowerLetter(int32(next))) || (isLowerLetter(v) && isUpperLetter(int32(next))) {
nextCaseIsChanged = true
}
}
if i > 0 && n[len(n)-1] != del && nextCaseIsChanged {
// add delimiter if next letter case type is changed
if isUpperLetter(v) {
n += string(del) + string(v)
} else if isLowerLetter(v) {
n += string(v) + string(del)
}
} else if v == ' ' || v == '-' || v == '_' {
// replace spaces and dashes with delimiter
n += string(del)
} else {
n = n + string(v)
}
}
n = strings.ToLower(n)
return n
}
func isUpperLetter(c int32) bool {
return c >= 'A' && c <= 'Z'
}
func isLowerLetter(c int32) bool {
return c >= 'a' && c <= 'z'
}