main.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* Copyright (C) 2016 Nate Dobbins
  2. *
  3. * This program is free software: you can redistribute it and/or modify it under the terms of the
  4. * GNU General Public License as published by the Free Software Foundation version 3 of the
  5. * License.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  8. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  9. * the GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License along with this program. If
  12. * not, see <http://www.gnu.org/licenses/>.
  13. */
  14. package main
  15. import (
  16. "flag"
  17. "fmt"
  18. "github.com/blevesearch/bleve"
  19. "io/ioutil" // for temp dir/files to store index
  20. "os"
  21. "strings"
  22. // "net/http" // for DetectContentType to skip bins
  23. )
  24. type data struct {
  25. Filename string
  26. Content string
  27. }
  28. var indexLoc = os.TempDir() + string(os.PathSeparator) + "sift"
  29. var mapping = bleve.NewIndexMapping()
  30. var idx, _ = bleve.New(indexLoc, mapping)
  31. func flags() (bool, bool) {
  32. exact := flag.Bool("e", false, "search exact strings")
  33. recursive := flag.Bool("r", false, "search within contained directories")
  34. flag.Parse()
  35. return *exact, *recursive
  36. }
  37. func args() (string, string) {
  38. args := flag.Args()
  39. // not enough or too many args
  40. if len(args) < 1 || len(args) > 2 {
  41. flag.Usage() // todo: exit with err
  42. }
  43. q := args[0]
  44. var path string
  45. if len(args) < 2 {
  46. path = "."
  47. } else {
  48. path = args[1]
  49. }
  50. // add trailing path separator if it's not there
  51. if strings.HasSuffix(path, string(os.PathSeparator)) == false {
  52. path += string(os.PathSeparator)
  53. }
  54. return q, path
  55. }
  56. func indexFiles(path string, recursive bool) {
  57. files, err := ioutil.ReadDir(path)
  58. if err != nil {
  59. fmt.Println(err)
  60. return
  61. }
  62. for _, file := range files {
  63. if file.IsDir() {
  64. if recursive {
  65. indexFiles(path+file.Name()+string(os.PathSeparator), true)
  66. }
  67. } else {
  68. content, err := ioutil.ReadFile(path + string(os.PathSeparator) + file.Name())
  69. if err != nil {
  70. fmt.Println(err)
  71. }
  72. idx.Index(file.Name(), data{Filename: file.Name(), Content: string(content)})
  73. }
  74. }
  75. }
  76. func search(q string) {
  77. // delete prior index if it exists
  78. if _, err := os.Stat(indexLoc); err == nil {
  79. err = os.RemoveAll(indexLoc)
  80. if err != nil {
  81. fmt.Println(err)
  82. }
  83. }
  84. query := bleve.NewMatchQuery(q)
  85. search := bleve.NewSearchRequest(query)
  86. searchResults, err := idx.Search(search)
  87. if err != nil {
  88. fmt.Println(err)
  89. return
  90. }
  91. fmt.Println(searchResults)
  92. }
  93. func main() {
  94. exact, recursive := flags()
  95. fmt.Println(exact, recursive) // todo
  96. q, path := args()
  97. indexFiles(path, recursive)
  98. search(q)
  99. }