main.go 881 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package in
  2. import (
  3. "os"
  4. "io"
  5. "bufio"
  6. "fmt"
  7. "surdeus.su/core/cli/mtool"
  8. )
  9. func Run(flagSet *mtool.Flags) {
  10. var (
  11. print bool
  12. not bool
  13. )
  14. flagSet.BoolVar(&print, "p", false, "print matching lines")
  15. flagSet.BoolVar(&not, "n", false, "find not matching lines")
  16. flagSet.Parse()
  17. args := flagSet.Args()
  18. if len(args) == 0 {
  19. //flagSet.Usage()
  20. if !not {
  21. os.Exit(1)
  22. }
  23. }
  24. mp := make(map[string] int)
  25. for _, v := range args {
  26. mp[v] = 0
  27. }
  28. status := 1
  29. r := bufio.NewReader(os.Stdin)
  30. if not {
  31. status = 0
  32. }
  33. for {
  34. l, err := r.ReadString('\n')
  35. if err == io.EOF {
  36. break
  37. }
  38. if len(l) != 0 && l[len(l)-1] == '\n' {
  39. l = l[:len(l)-1]
  40. }
  41. _, ok := mp[l]
  42. if not {
  43. if ok {
  44. status = 1
  45. ok = false
  46. } else {
  47. ok = true
  48. }
  49. } else {
  50. if ok {
  51. status = 0
  52. }
  53. }
  54. if print && ok {
  55. fmt.Println(l)
  56. }
  57. }
  58. os.Exit(status)
  59. }