Files
tablizer/lib/io.go

115 lines
2.6 KiB
Go
Raw Permalink Normal View History

/*
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/>.
*/
package lib
import (
"errors"
2024-05-07 18:01:12 +02:00
"fmt"
"io"
"os"
2024-05-07 15:19:54 +02:00
"github.com/tlinden/tablizer/cfg"
)
2024-05-07 18:01:12 +02:00
const RWRR = 0755
2024-05-07 15:19:54 +02:00
func ProcessFiles(conf *cfg.Config, args []string) error {
fds, pattern, err := determineIO(conf, args)
2022-10-10 20:14:51 +02:00
if err != nil {
return err
}
2024-05-07 15:19:54 +02:00
if err := conf.PreparePattern(pattern); err != nil {
2022-10-25 18:34:28 +02:00
return err
}
for _, fd := range fds {
2024-05-07 15:19:54 +02:00
data, err := Parse(*conf, fd)
if err != nil {
return err
}
2024-05-07 15:19:54 +02:00
err = PrepareColumns(conf, &data)
if err != nil {
return err
}
2024-05-07 15:19:54 +02:00
printData(os.Stdout, *conf, &data)
}
return nil
}
2024-05-07 15:19:54 +02:00
func determineIO(conf *cfg.Config, args []string) ([]io.Reader, string, error) {
2024-05-07 18:01:12 +02:00
var filehandles []io.Reader
var pattern string
2024-05-07 18:01:12 +02:00
var haveio bool
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
// we're reading from STDIN, which takes precedence over file args
2024-05-07 18:01:12 +02:00
filehandles = append(filehandles, os.Stdin)
if len(args) > 0 {
// ignore any args > 1
pattern = args[0]
2024-05-07 15:19:54 +02:00
conf.Pattern = args[0] // used for colorization by printData()
}
2024-05-07 18:01:12 +02:00
haveio = true
2024-05-07 15:19:54 +02:00
} else if len(args) > 0 {
// threre were args left, take a look
if args[0] == "-" {
// in traditional unix programs a dash denotes STDIN (forced)
2024-05-07 18:01:12 +02:00
filehandles = append(filehandles, os.Stdin)
2024-05-07 15:19:54 +02:00
haveio = true
} else {
if _, err := os.Stat(args[0]); err != nil {
// first one is not a file, consider it as regexp and
// shift arg list
pattern = args[0]
conf.Pattern = args[0] // used for colorization by printData()
args = args[1:]
}
2024-05-07 15:19:54 +02:00
if len(args) > 0 {
// consider any other args as files
for _, file := range args {
2024-05-07 18:01:12 +02:00
filehandle, err := os.OpenFile(file, os.O_RDONLY, RWRR)
2024-05-07 15:19:54 +02:00
if err != nil {
2024-05-07 18:01:12 +02:00
return nil, "", fmt.Errorf("failed to read input file %s: %w", file, err)
}
2024-05-07 15:19:54 +02:00
2024-05-07 18:01:12 +02:00
filehandles = append(filehandles, filehandle)
2024-05-07 15:19:54 +02:00
haveio = true
}
}
}
}
if !haveio {
2024-05-07 15:19:54 +02:00
return nil, "", errors.New("no file specified and nothing to read on stdin")
}
2024-05-07 18:01:12 +02:00
return filehandles, pattern, nil
}