-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
126 lines (108 loc) · 3.37 KB
/
Copy pathmain.go
File metadata and controls
126 lines (108 loc) · 3.37 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
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
package main
import (
"flag"
"fmt"
"os"
"strings"
"textindexer/internals"
)
// main is the entry point of the application. It parses command-line arguments
// and executes the appropriate command based on the provided options.
//
// Usage: textindex [options]
//
// Commands:
//
// -c index : Indexes the input file and generates an output index file.
// Options:
// -i string : Input file path (required)
// -s int : Chunk size in bytes (default: 4096)
// -o string : Output index file path (required)
//
// -c lookup : Looks up a SimHash value in the specified index file.
// Options:
// -i string : Index file path (required)
// -h string : SimHash value to lookup (required)
//
// -c fuzzy : Performs a fuzzy search for a SimHash value in the specified index file.
// Options:
// -i string : Index file path (required)
// -h string : SimHash value for fuzzy search (required)
//
// If an unknown command or invalid options are provided, the program will print an error message and exit.
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: textindex [options]")
os.Exit(1)
}
command := ""
if os.Args[1] == "-c" {
command = os.Args[2]
} else {
fmt.Println("Unknown command option")
os.Exit(1)
}
args := os.Args[3:]
switch command {
case "index":
indexFlags := flag.NewFlagSet("index", flag.ExitOnError)
inputFile := indexFlags.String("i", "", "Input file path (required)")
chunkSize := indexFlags.Int("s", 4096, "Chunk size in bytes")
outputFile := indexFlags.String("o", "", "Output index file path (required)")
indexFlags.Parse(args)
if *inputFile == "" || *outputFile == "" {
fmt.Println("Error: -i and -o are required for index command")
os.Exit(1)
}
if !strings.HasSuffix(*outputFile, ".idx") {
fmt.Println("Error: please provide an index file (with .idx extension)")
os.Exit(1)
}
if *chunkSize <= 0 {
fmt.Println("Error: invalid chunk size")
os.Exit(1)
}
if err := internals.RunIndex(*inputFile, *chunkSize, *outputFile); err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
case "lookup":
lookupFlags := flag.NewFlagSet("lookup", flag.ExitOnError)
indexFile := lookupFlags.String("i", "", "Index file path")
simHashStr := lookupFlags.String("h", "", "SimHash value to lookup")
lookupFlags.Parse(args)
if *indexFile == "" || *simHashStr == "" {
fmt.Println("Error: -i and -h are required for lookup command")
os.Exit(1)
}
if !strings.HasSuffix(*indexFile, ".idx") {
fmt.Println("Error: please input an index file")
os.Exit(1)
}
if err := internals.RunLookup(*indexFile, *simHashStr); err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
case "fuzzy":
// New fuzzy command logic
fuzzyFlags := flag.NewFlagSet("fuzzy", flag.ExitOnError)
indexFile := fuzzyFlags.String("i", "", "Index file path")
simHashStr := fuzzyFlags.String("h", "", "SimHash value for fuzzy search")
fuzzyFlags.Parse(args)
if *indexFile == "" || *simHashStr == "" {
fmt.Println("Error: -i and -h are required for fuzzy command")
os.Exit(1)
}
if !strings.HasSuffix(*indexFile, ".idx") {
fmt.Println("Error: please input an index file")
os.Exit(1)
}
if err := internals.RunFuzzy(*indexFile, *simHashStr); err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
default:
fmt.Println("Invalid command. Use 'index' or 'lookup'.")
os.Exit(1)
}
}