2022-09-30 14:08:59 +02:00
|
|
|
/*
|
|
|
|
|
Copyright © 2022 Thomas von Dein
|
|
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-09-30 19:14:58 +02:00
|
|
|
package lib
|
2022-09-30 14:08:59 +02:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
2022-10-02 14:22:31 +02:00
|
|
|
"io"
|
2022-09-30 14:08:59 +02:00
|
|
|
"os"
|
|
|
|
|
)
|
|
|
|
|
|
2022-09-30 19:14:58 +02:00
|
|
|
func ProcessFiles(args []string) error {
|
2022-10-02 14:22:31 +02:00
|
|
|
fds, pattern, err := determineIO(args)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2022-09-30 14:08:59 +02:00
|
|
|
|
2022-10-02 14:22:31 +02:00
|
|
|
for _, fd := range fds {
|
2022-10-03 12:52:26 +02:00
|
|
|
data, err := parseFile(fd, pattern)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2022-10-03 13:10:23 +02:00
|
|
|
printData(&data)
|
2022-10-02 14:22:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func determineIO(args []string) ([]io.Reader, string, error) {
|
|
|
|
|
var pattern string
|
|
|
|
|
var fds []io.Reader
|
|
|
|
|
var havefiles bool
|
2022-09-30 14:08:59 +02:00
|
|
|
|
|
|
|
|
if len(args) > 0 {
|
2022-10-02 14:22:31 +02:00
|
|
|
// threre were args left, take a look
|
2022-09-30 14:08:59 +02:00
|
|
|
if _, err := os.Stat(args[0]); err != nil {
|
2022-10-02 14:22:31 +02:00
|
|
|
// first one is not a file, consider it as regexp and
|
|
|
|
|
// shift arg list
|
2022-09-30 14:08:59 +02:00
|
|
|
pattern = args[0]
|
|
|
|
|
args = args[1:]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(args) > 0 {
|
2022-10-02 14:22:31 +02:00
|
|
|
// only files
|
2022-09-30 14:08:59 +02:00
|
|
|
for _, file := range args {
|
|
|
|
|
fd, err := os.OpenFile(file, os.O_RDONLY, 0755)
|
2022-10-02 14:22:31 +02:00
|
|
|
|
2022-09-30 14:08:59 +02:00
|
|
|
if err != nil {
|
2022-10-02 14:22:31 +02:00
|
|
|
return nil, "", err
|
2022-09-30 14:08:59 +02:00
|
|
|
}
|
|
|
|
|
|
2022-10-02 14:22:31 +02:00
|
|
|
fds = append(fds, fd)
|
2022-09-30 14:08:59 +02:00
|
|
|
}
|
|
|
|
|
havefiles = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !havefiles {
|
|
|
|
|
stat, _ := os.Stdin.Stat()
|
|
|
|
|
if (stat.Mode() & os.ModeCharDevice) == 0 {
|
2022-10-02 14:22:31 +02:00
|
|
|
fds = append(fds, os.Stdin)
|
2022-09-30 14:08:59 +02:00
|
|
|
} else {
|
2022-10-02 14:22:31 +02:00
|
|
|
return nil, "", errors.New("No file specified and nothing to read on stdin!")
|
2022-09-30 14:08:59 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-02 14:22:31 +02:00
|
|
|
return fds, pattern, nil
|
2022-09-30 14:08:59 +02:00
|
|
|
}
|