-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
168 lines (154 loc) · 4.48 KB
/
main.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
flag "github.com/spf13/pflag"
)
func main() {
var version = flag.BoolP("version", "v", false, "Prints out version info")
var JSONout = flag.BoolP("json", "j", false, "Prints link output in JSON format")
var all = flag.BoolP("all", "a", false, "Prints out all types of responses")
var good = flag.BoolP("good", "g", false, "Prints out only good responses")
var bad = flag.BoolP("bad", "b", false, "Prints out only bad responses")
var ignore = flag.BoolP("ignore", "i", false, "Ignore certain url patterns")
var telescope = flag.BoolP("telescope", "t", false, "Read data from telescope local host")
var JSONchoice = false
var typeLink int = 1
var result []string
var temp = "test"
var k = 0
var total = 0
var tele = false
flag.Parse()
if *version == true {
fmt.Println("LinkStatus version 1.0.5")
return
}
if *JSONout == true {
fmt.Println("JSON output selected")
JSONchoice = true
}
if *all == true {
fmt.Println("Outputting all types of links")
typeLink = 1
}
if *good == true {
fmt.Println("Outputting only good types of links")
typeLink = 2
}
if *bad == true {
fmt.Println("Outputting only bad types of links")
typeLink = 3
}
if *telescope == true {
fmt.Println("Reading telescope post output")
tele = true
}
if len(os.Args) == 1 {
fmt.Println(`
==================================================================================
|| LINK STATUS ||
==================================================================================
Default: LinkStatus [filenames]
Example: LinkStatus urls.txt
Version: LinkStatus -v or --version to check version.
JSON: LinkStatus -j or --json to output as JSON format
All: LinkStatus -a or --all to output all types of responses
Good: LinkStatus -g or --good to output only good types of responses
Bad: LinkStatus -b or --bad to output only bad types of responses
Ignore: LinkStatus test.txt -i or --ignore to ignore certain url patterns
==================================================================================
`)
os.Exit(0)
}
fmt.Println("==================================================================================")
fmt.Println("Command List: ", os.Args[1:])
fmt.Println("----------------------------------------------------------------------------------")
for _, file := range os.Args[1:] {
if file[0] != '-' {
if tele == true {
telescopeParse(file)
file = "tData.txt"
}
result = readFile(file, JSONchoice, typeLink, *ignore, tele)
if JSONchoice == true {
fmt.Print("[")
}
for i := range result {
if temp == result[i] {
//This is to ignore dupes
} else {
checkStatus(result[i], k, JSONchoice, typeLink)
k++
total++
}
temp = result[i]
}
if JSONchoice == true {
fmt.Println(" ]")
}
}
}
fmt.Println("----------------------------------------------------------------------------------")
fmt.Println("Total URL Count: ", total)
fmt.Println("==================================================================================")
//Remove the temp files
err := os.Remove("telescopeData.json")
if err != nil {
log.Fatal(err)
}
e := os.Remove("tData.txt")
if e != nil {
log.Fatal(e)
}
os.Exit(0)
}
//Telescope struct which lays out the json data for easy storage
type Telescope struct {
ID string `json:"id"`
URL string `json:"url"`
}
func telescopeParse(file string) {
fmt.Println("Telescope Parsing")
fmt.Println(file)
fmt.Println("----------------------------------------------------------------------------------")
//Get the json data in a file
out, _ := os.OpenFile("telescopeData.json", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
defer out.Close()
resp, err := http.Get(file)
if err != nil {
log.Println(err)
}
defer resp.Body.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
log.Println(err)
}
jsonFile, err := os.Open("telescopeData.json")
if err != nil {
fmt.Println(err)
}
byteValue, err := ioutil.ReadAll(jsonFile)
if err != nil {
fmt.Println(err)
}
var teleData []Telescope
err = json.Unmarshal(byteValue, &teleData)
if err != nil {
fmt.Println(err)
}
telescopeFile, err := os.OpenFile("tData.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
for i := 0; i < len(teleData); i++ {
telescopeFile.Write([]byte("http://localhost:3000" + teleData[i].URL + " "))
}
telescopeFile.Close()
jsonFile.Close()
}