-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
125 lines (117 loc) · 2.78 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
package main
import (
"bufio"
"fmt"
"io"
"net/http"
"os"
"regexp"
"strconv"
"os/exec"
)
func readlink(master string, loc string) {
inputFile, err := os.Open(master)
if err != nil {
fmt.Println(err)
}
defer inputFile.Close()
scanner := bufio.NewScanner(inputFile)
for scanner.Scan() {
// Get the current line
download(scanner.Text(), loc)
}
return
}
func download(link string, loc string) {
// Make an HTTP GET request to the URL of the file you want to download
response, err := http.Get(link)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("download:" + link + " completed!")
}
defer response.Body.Close()
// Create a local file to save the downloaded file
file, err := os.OpenFile(loc, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
fmt.Println(err)
}
defer file.Close()
// Use io.Copy to copy the response body to the local file
_, err = io.Copy(file, response.Body)
if err != nil {
fmt.Println(err)
}
return
}
func processing(loc string, output string, custom string) {
i := 0 // Number of duplicated items
j := 0 // Number of valid items
outputFile, err := os.Create(output)
if err != nil {
fmt.Println(err)
}
linesWritten := make(map[string]bool)
inputFile, err := os.Open(loc)
if err != nil {
fmt.Println(err)
}
defer inputFile.Close()
scanner := bufio.NewScanner(inputFile)
println("Creating " + output + " ......")
for scanner.Scan() {
// Get the current line
line := scanner.Text()
// Check if the line is started from "[blank] or [space] or # or !"
match, _ := regexp.MatchString("^[ #!]|^$|^\\[", line)
if match {
linesWritten[line] = true
}
if !linesWritten[line] {
// Write the line to the output file
outputFile.WriteString(line + "\n")
j++
// Mark the line as written
linesWritten[line] = true
} else {
i++
fmt.Println("removed: "+line)
}
}
customFile, err := os.Open(custom)
if err != nil {
fmt.Println(err)
}
defer customFile.Close()
scanner1 := bufio.NewScanner(customFile)
for scanner1.Scan() {
outputFile.WriteString(scanner1.Text() + "\n")
j++
}
defer outputFile.Close()
println(strconv.Itoa(i) + " of items are deleted")
println(strconv.Itoa(j) + " of items are included")
println(output + " is created with addition rules from " + custom)
return
}
func remove(loc string) {
err := os.Remove(loc)
if err != nil {
fmt.Println(err)
}
return
}
func main() {
readlink(os.Args[1], "zjc")
cmd := exec.Command("hostlist-compiler", "-c", "./setting/c.json", "-o", "ckc")
out, err := cmd.CombinedOutput()
if err != nil {
fmt.Println("Error: ", err)
} else {
fmt.Println(string(out))
fmt.Println("Data Cleansing completed")
}
processing("ckc", os.Args[2], os.Args[3])
remove("ckc")
remove("zjc")
}