-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkstat.go
119 lines (102 loc) · 2.89 KB
/
linkstat.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
package main
import (
"flag"
"fmt"
"log"
"os"
"time"
"github.com/httpreserve/httpreserve"
)
var (
//version
vers bool
//individual links
link string
label string
//output methods
boltdb bool
jsonout bool
csvout bool
//list processing
list string
)
func init() {
// Return version information.
flag.BoolVar(&vers, "version", false, "Return httpreserve version.")
flag.BoolVar(&vers, "v", false, "Return httpreserve version.")
// Flags to return a single result.
flag.StringVar(&link, "link", "", "Seek the status of a single URL: JSON")
flag.StringVar(&label, "label", "", "Annotate single URL check response with label.")
// Flags to batch results.
flag.StringVar(&list, "list", "", "Provide a list of URLs to test against in CSV format.")
// Output method flags.
flag.BoolVar(&boltdb, "bolt", false, "Output to static BoltDB.")
flag.BoolVar(&jsonout, "json", false, "Output to JSON.")
flag.BoolVar(&csvout, "csv", false, "Output to CSV.")
}
func getJSONFromLocal(link string, label string) string {
ls, err := httpreserve.GenerateLinkStats(link, label, true)
if err != nil {
log.Println("Error retrieving linkstat JSON may be incorrect:", err)
}
js := httpreserve.MakeLinkStatsJSON(ls)
// throttle requests to the server somehow...
time.Sleep(500 * time.Millisecond)
// return json...
return js
}
func getLocalLink() {
js := getJSONFromLocal(link, label)
fmt.Fprintln(os.Stderr, "Using httpreserve libs to retrieve data.")
fmt.Fprintf(os.Stdout, "%s", js)
}
var htmcomplete bool
var starttime time.Time
var elapsedtime time.Duration
func programrunner() {
if jsonout {
fmt.Fprintf(os.Stdout, "%s", outputJSONHeader())
listHandler(jsonHandler)
outputjsonpool()
fmt.Fprintf(os.Stdout, "%s", outputJSONFooter())
return
}
if csvout {
//output JSON header
fmt.Fprintf(os.Stdout, "%s", outputCSVHeader())
listHandler(csvHandler)
return
}
if boltdb {
openKVALBolt()
defer closeKVALBolt()
listHandler(boltdbHandler)
return
}
if link != "" {
getLocalLink()
}
}
func main() {
flag.Parse()
if vers {
fmt.Fprintf(os.Stderr, "%s\n", getVersion())
fmt.Fprintf(os.Stderr, "%s\n", httpreserve.VersionText())
os.Exit(0)
} else if flag.NFlag() <= 0 {
fmt.Fprintln(os.Stderr, "Usage: linkstat [Optional -link] [Optional -label]")
fmt.Fprintln(os.Stderr, " [Optional -list] [Optional -json]")
fmt.Fprintln(os.Stderr, " [Optional -bolt]")
fmt.Fprintln(os.Stderr, " [Optional -csv]")
fmt.Fprintln(os.Stderr, " [Optional -version -v]")
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, "Output: [Json]")
fmt.Fprintln(os.Stderr, "Output: [CSV]")
fmt.Fprintln(os.Stderr, "Output: [BoltDB]")
fmt.Fprintf(os.Stderr, "Output: [Version] '%s ...'\n", httpreserve.VersionText())
fmt.Fprintln(os.Stderr, "")
flag.Usage()
os.Exit(0)
}
programrunner()
}