This repository has been archived by the owner on Jun 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
functions.go
149 lines (127 loc) · 3.32 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
package main
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"fmt"
"hash"
"io"
"os"
"regexp"
"strings"
"text/template"
v "github.com/hashicorp/go-version"
"github.com/skratchdot/open-golang/open"
)
var Version string
var GitRevision string
func version() (v string) {
if GitRevision == "" {
v = Version
} else {
v = fmt.Sprintf("%s-%s", Version, GitRevision)
}
return
}
func fatal(msg string) {
println("!!", msg)
os.Exit(2)
}
func BinVersion(args []string) {
fmt.Println(version())
}
func VersionString() string {
return version()
}
func OpenBrowser(args []string) {
err := open.Start(args[0])
if err != nil {
fatal("Can't open browser: '" + err.Error())
}
}
func VersionCompare(args []string) {
v0, err := v.NewVersion(args[0])
if err != nil {
fatal("Can't parse version string" + err.Error())
}
v1, err := v.NewVersion(args[1])
if err != nil {
fatal("Can't parse version string" + err.Error())
}
fmt.Println(v0.Compare(v1))
}
func Checksum(args []string) {
if len(args) < 1 {
fatal("No algorithm specified")
}
var h hash.Hash
switch args[0] {
case "md5":
h = md5.New()
case "sha1":
h = sha1.New()
case "sha256":
h = sha256.New()
default:
fatal("Algorithm '" + args[0] + "' is unsupported")
}
io.Copy(h, os.Stdin)
fmt.Printf("%x\n", h.Sum(nil))
}
func ServiceURL(args []string) {
serviceName, bridgeAddress, localDevList, protocol, localDevPort, servicePort := unpackServiceURLArgs(args)
if checkIfServiceInLocal(serviceName, localDevList) {
printServiceURL(bridgeAddress, protocol, localDevPort)
} else {
printServiceURL(serviceName, protocol, servicePort)
}
}
func printServiceURL(serviceName string, protocol string, port string) {
if len(port) > 0 {
fmt.Printf("%s%s:%s", protocol, serviceName, port)
} else {
fmt.Printf("%s%s", protocol, serviceName)
}
}
func unpackServiceURLArgs(args []string) (string, string, string, string, string, string) {
return args[0], args[1], args[2], args[3], args[4], args[5]
}
// Checks if the service is in the local dev string by assuming the local dev string
// is comma-separated and matching against the 4 possible ways a service may be present:
// - "service"
// - "other_services,service"
// - "service,other_services"
// - "other_services1,service,other_services2"
// With any number of whitespace in between the services and the commas.
func checkIfServiceInLocal(serviceName string, localDevList string) bool {
matched, _ := regexp.MatchString(`(,|^)\s*` + serviceName + `\s*(,|$)`, localDevList)
return matched
}
type caddyFileParams struct {
IngressUrls []string
}
func GenerateCaddyFile(args []string) {
params := caddyFileParams{strings.Split(args[0], ",")}
tmpl, err := Asset("templates/Caddyfile.tmpl")
if err != nil {
fatal("Can't read Caddyfile template" + err.Error())
}
t := template.Must(template.New("caddy").Delims("{{{", "}}}").Parse(string(tmpl)))
t.Execute(os.Stdout, params)
}
func HostFromURL(args []string) {
HostOrPortFromURL(args, 1)
}
func PortFromURL(args []string) {
HostOrPortFromURL(args, 2)
}
func HostOrPortFromURL(args []string, component int) {
url := args[0]
pattern := regexp.MustCompile(`(?:[^:]+://)?([^:]+):([0-9]+)(?:/.*)?`)
submatch := pattern.FindStringSubmatch(url)
if submatch == nil {
fatal("Can't parse URL '" + url + "'")
} else {
fmt.Printf("%s", submatch[component])
}
}