@@ -19,6 +19,7 @@ import (
1919 "time"
2020
2121 "github.com/fatih/color"
22+ "github.com/rzhade3/beaconspec"
2223 "github.com/schollz/progressbar/v3"
2324)
2425
@@ -62,7 +63,6 @@ var err error
6263var archivesPath string
6364
6465func main () {
65-
6666 var keywordFile string
6767 var dateParam string
6868 var outFile string
@@ -80,7 +80,6 @@ func main() {
8080 flag .Usage = func () { fmt .Print (usage ) }
8181 flag .Parse ()
8282
83-
8483 if keywordFile == "" || dateParam == "" || outFile == "" {
8584 crash ("You must specify all arguments." , err )
8685 return
@@ -191,7 +190,7 @@ func getArchive(body []byte, date string, keywordFile string, outfile string) {
191190 _ , err := Unzip (filepath .Join (archivesPath , fullname , item .Name ), filepath .Join (archivesPath , fullname ))
192191 if err != nil {
193192 os .Remove (filepath .Join (archivesPath , fullname , item .Name ))
194- crash (item .Name + " looks damaged. It's removed now. Run the program again to re-download." , err )
193+ crash (item .Name + " looks damaged. It's removed now. Run the program again to re-download." , err )
195194 }
196195 }
197196
@@ -227,10 +226,9 @@ func getArchive(body []byte, date string, keywordFile string, outfile string) {
227226}
228227
229228func searchFile (fileLocation string , keyword string , outfile string ) {
230-
231229 var path string
232230
233- if strings .HasPrefix (fileLocation , "archives" ){
231+ if strings .HasPrefix (fileLocation , "archives" ) {
234232 path_parts := strings .Split (fileLocation , string (os .PathSeparator ))
235233 path = filepath .Join (path_parts [1 ], path_parts [2 ])
236234 } else {
@@ -250,54 +248,71 @@ func searchFile(fileLocation string, keyword string, outfile string) {
250248 panic (err )
251249 }
252250 defer f .Close ()
251+
252+ metadata , err := beaconspec .ReadMetadata (fileLocation )
253+ if err != nil {
254+ warning (err .Error ())
255+ return
256+ }
257+
258+ var matcher func ([]byte ) bool
253259 if strings .HasPrefix (keyword , "regex" ) {
254- regexValue := strings .Split (keyword , " " )[1 ]
255- r , err := regexp .Compile (regexValue )
256- if err != nil {
257- warning ("Invalid Regex!" )
258- return
259- }
260- for scanner .Scan () {
261- if r .MatchString (scanner .Text ()) {
262- textToWrite := strings .Split (scanner .Text (), "|" )[1 ]
263- if _ , err := f .WriteString (textToWrite + "\n " ); err != nil {
264- panic (err )
265- }
266- }
267- }
260+ matcher , err = regexMatch (keyword )
261+ } else if strings .Contains (keyword , "," ) {
262+ matcher , err = multiKeywordMatcher (keyword )
268263 } else {
269- if strings .Contains (keyword , "," ) {
270- keywords := strings .Split (keyword , "," )
271- for scanner .Scan () {
272- foundFlag := true
273- for i := 0 ; i < len (keywords ); i ++ {
274- if bytes .Contains (scanner .Bytes (), []byte (keywords [i ])) {
275- continue
276- } else {
277- foundFlag = false
278- }
279- }
280- if foundFlag {
281- textToWrite := strings .Split (scanner .Text (), "|" )[1 ]
282- if _ , err := f .WriteString (textToWrite + "\n " ); err != nil {
283- panic (err )
284- }
285- }
286- }
264+ matcher , err = stringMatch (keyword )
265+ }
266+ if err != nil {
267+ warning (err .Error ())
268+ return
269+ }
270+
271+ for scanner .Scan () {
272+ if matcher (scanner .Bytes ()) {
287273
288- } else {
289- toFind := []byte (keyword )
290- for scanner .Scan () {
291- if bytes .Contains (scanner .Bytes (), toFind ) {
292- textToWrite := strings .Split (scanner .Text (), "|" )[1 ]
293- if _ , err := f .WriteString (textToWrite + "\n " ); err != nil {
294- panic (err )
295- }
296- }
274+ line , err := beaconspec .ParseLine (scanner .Text (), metadata )
275+ if err != nil {
276+ panic (err )
277+ }
278+ textToWrite := fmt .Sprintf ("%s,%s\n " , line .Source , line .Target )
279+ if _ , err := f .WriteString (textToWrite ); err != nil {
280+ panic (err )
297281 }
298282 }
299283 }
284+ }
285+
286+ func regexMatch (keyword string ) (func ([]byte ) bool , error ) {
287+ regexValue := strings .Split (keyword , " " )[1 ]
288+ r , err := regexp .Compile (regexValue )
289+ return func (b []byte ) bool {
290+ s := string (b )
291+ return r .MatchString (s )
292+ }, err
293+ }
300294
295+ func multiKeywordMatcher (keyword string ) (func ([]byte ) bool , error ) {
296+ keywords := strings .Split (keyword , "," )
297+ bytes_keywords := make ([][]byte , len (keywords ))
298+ for i , k := range keywords {
299+ bytes_keywords [i ] = []byte (k )
300+ }
301+ return func (text []byte ) bool {
302+ for _ , k := range bytes_keywords {
303+ if ! bytes .Contains (text , k ) {
304+ return false
305+ }
306+ }
307+ return true
308+ }, nil
309+ }
310+
311+ func stringMatch (keyword string ) (func ([]byte ) bool , error ) {
312+ bytes_keyword := []byte (keyword )
313+ return func (b []byte ) bool {
314+ return bytes .Contains (b , bytes_keyword )
315+ }, nil
301316}
302317
303318func ifArchiveExists (fullname string ) bool {
@@ -367,20 +382,6 @@ func downloadFile(url string) {
367382 color .Green ("Download Finished!" )
368383}
369384
370- func ByteCountSI (b int64 ) string {
371- const unit = 1000
372- if b < unit {
373- return fmt .Sprintf ("%d B" , b )
374- }
375- div , exp := int64 (unit ), 0
376- for n := b / unit ; n >= unit ; n /= unit {
377- div *= unit
378- exp ++
379- }
380- return fmt .Sprintf ("%.1f %cB" ,
381- float64 (b )/ float64 (div ), "kMGTPE" [exp ])
382- }
383-
384385func Unzip (src string , dest string ) ([]string , error ) {
385386 var filenames []string
386387 r , err := zip .OpenReader (src )
@@ -447,4 +448,4 @@ func crash(message string, err error) {
447448
448449func warning (message string ) {
449450 color .Yellow ("[WARNING]: " + message + "\n " )
450- }
451+ }
0 commit comments