-
Notifications
You must be signed in to change notification settings - Fork 29
/
util.go
60 lines (46 loc) · 1.04 KB
/
util.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
package chartify
import (
"fmt"
"regexp"
"strings"
)
const semVerRegex string = `v?([0-9]+)(\.[0-9]+)?(\.[0-9]+)?` +
`(-([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?` +
`(\+([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?`
func createFlagChain(flag string, input []string) string {
chain := ""
dashes := "--"
if len(flag) == 1 {
dashes = "-"
}
for _, i := range input {
if i != "" {
i = " " + i
}
chain = fmt.Sprintf("%s %s%s%s", chain, dashes, flag, i)
}
return chain
}
// indents a block of text with an indent string
func indent(text, indent string) string {
var b strings.Builder
b.Grow(len(text) * 2)
lines := strings.Split(text, "\n")
last := len(lines) - 1
for i, j := range lines {
if i > 0 && i < last && j != "" {
b.WriteString("\n")
}
if j != "" {
b.WriteString(indent + j)
}
}
return b.String()
}
func FindSemVerInfo(version string) (string, error) {
v := regexp.MustCompile(semVerRegex).FindString(version)
if v == "" {
return "", fmt.Errorf("unable to find semver info in %s", version)
}
return v, nil
}