-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
103 lines (84 loc) · 1.91 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
package main
import (
"bufio"
"flag"
"fmt"
"os"
)
var silent bool
func main() {
deleteFlag := flag.Bool("d", false, "Delete lines from output.txt if they are not in the current command output")
flag.BoolVar(&silent, "s", false, "Run in silent mode (suppress output)")
flag.Parse()
if len(flag.Args()) != 1 {
printError("Usage: go-trace [-d] [-s/--silent] <output_file>")
os.Exit(1)
}
outputFile := flag.Args()[0]
previousOutput, err := readLines(outputFile)
if err != nil {
printError(fmt.Sprintf("Error reading previous output: %v", err))
os.Exit(1)
}
currentOutput := readFromStdin()
currentOutputSet := make(map[string]bool)
for _, domain := range currentOutput {
currentOutputSet[domain] = true
}
var newOutput []string
for _, domain := range previousOutput {
if _, exists := currentOutputSet[domain]; exists {
newOutput = append(newOutput, domain)
} else {
printError(fmt.Sprintf("%s", domain))
if !*deleteFlag {
newOutput = append(newOutput, domain)
}
}
}
err = writeLines(outputFile, newOutput)
if err != nil {
printError(fmt.Sprintf("Error writing to output file: %v", err))
os.Exit(1)
}
}
func printError(message string) {
if !silent {
fmt.Println(message)
}
}
func readLines(file string) ([]string, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
var lines []string
scanner := bufio.NewScanner(f)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
func readFromStdin() []string {
var lines []string
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines
}
func writeLines(file string, lines []string) error {
f, err := os.Create(file)
if err != nil {
return err
}
defer f.Close()
for _, line := range lines {
_, err := f.WriteString(line + "\n")
if err != nil {
return err
}
}
return nil
}