-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
50 lines (42 loc) · 1.25 KB
/
main.go
File metadata and controls
50 lines (42 loc) · 1.25 KB
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
package main
import (
"fmt"
flag "github.com/spf13/pflag"
"net/http"
"os"
"time"
)
func main() {
var flagAlias = map[string]string{
"uri": "u",
"port": "p",
"timeout": "t",
"interval": "i",
"response-code": "rc",
}
var endpoint = flag.String("uri", "http://localhost", "URI, with protocol and no port.")
var port = flag.Int("port", 80, "Port to use.")
var timeout = flag.Int("timeout", 10000, "Timeout before returning a non 0 exit code, in milliseconds.")
var interval = flag.Int("interval", 1000, "Polling interval, in milliseconds.")
var responseCode = flag.Int("response-code", 200, "Response code to wait for.")
for from, to := range flagAlias {
flagSet := flag.Lookup(from)
flag.Var(flagSet.Value, to, fmt.Sprintf("alias to %s", flagSet.Name))
}
flag.Parse()
startTime := time.Now()
timeoutDuration := time.Duration(*timeout) * time.Millisecond
sleepDuration := time.Duration(*interval) * time.Millisecond
ticker := time.NewTicker(sleepDuration)
for {
res, err := http.Head(fmt.Sprintf("%s:%d", *endpoint, *port))
if err == nil && res.StatusCode == *responseCode {
os.Exit(0)
}
timeElapsed := time.Now().Sub(startTime)
if timeElapsed > timeoutDuration {
os.Exit(1)
}
<-ticker.C
}
}