bb/tool/in/main.go

69 lines
881 B
Go
Raw Normal View History

2022-11-14 17:15:02 +03:00
package in
import (
"os"
"io"
"bufio"
"fmt"
2024-05-15 21:07:35 +03:00
"surdeus.su/core/cli/mtool"
2022-11-14 17:15:02 +03:00
)
2023-03-24 19:47:31 +03:00
func Run(flagSet *mtool.Flags) {
2022-11-14 17:15:02 +03:00
var (
print bool
not bool
)
flagSet.BoolVar(&print, "p", false, "print matching lines")
flagSet.BoolVar(&not, "n", false, "find not matching lines")
2023-03-24 19:47:31 +03:00
flagSet.Parse()
args := flagSet.Args()
2022-11-14 17:15:02 +03:00
if len(args) == 0 {
2022-11-14 21:53:48 +03:00
//flagSet.Usage()
if !not {
os.Exit(1)
}
2022-11-14 17:15:02 +03:00
}
mp := make(map[string] int)
for _, v := range args {
mp[v] = 0
}
status := 1
r := bufio.NewReader(os.Stdin)
if not {
status = 0
}
for {
l, err := r.ReadString('\n')
if err == io.EOF {
break
}
if len(l) != 0 && l[len(l)-1] == '\n' {
l = l[:len(l)-1]
}
_, ok := mp[l]
if not {
if ok {
status = 1
ok = false
} else {
ok = true
}
} else {
if ok {
status = 0
}
}
if print && ok {
fmt.Println(l)
}
}
os.Exit(status)
}