mirror of
https://codeberg.org/scip/tablizer.git
synced 2025-12-16 20:20:57 +01:00
109 lines
2.4 KiB
Go
109 lines
2.4 KiB
Go
/*
|
|
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"
|
|
"io"
|
|
"os"
|
|
|
|
"github.com/tlinden/tablizer/cfg"
|
|
)
|
|
|
|
func ProcessFiles(conf *cfg.Config, args []string) error {
|
|
fds, pattern, err := determineIO(conf, args)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := conf.PreparePattern(pattern); err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, fd := range fds {
|
|
data, err := Parse(*conf, fd)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = PrepareColumns(conf, &data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
printData(os.Stdout, *conf, &data)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func determineIO(conf *cfg.Config, args []string) ([]io.Reader, string, error) {
|
|
var pattern string
|
|
var fds []io.Reader
|
|
var haveio bool
|
|
|
|
stat, _ := os.Stdin.Stat()
|
|
if (stat.Mode() & os.ModeCharDevice) == 0 {
|
|
// we're reading from STDIN, which takes precedence over file args
|
|
fds = append(fds, os.Stdin)
|
|
if len(args) > 0 {
|
|
// ignore any args > 1
|
|
pattern = args[0]
|
|
conf.Pattern = args[0] // used for colorization by printData()
|
|
}
|
|
haveio = true
|
|
} else if len(args) > 0 {
|
|
// threre were args left, take a look
|
|
if args[0] == "-" {
|
|
// in traditional unix programs a dash denotes STDIN (forced)
|
|
fds = append(fds, os.Stdin)
|
|
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:]
|
|
}
|
|
|
|
if len(args) > 0 {
|
|
// consider any other args as files
|
|
for _, file := range args {
|
|
|
|
fd, err := os.OpenFile(file, os.O_RDONLY, 0755)
|
|
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
|
|
fds = append(fds, fd)
|
|
haveio = true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if !haveio {
|
|
return nil, "", errors.New("no file specified and nothing to read on stdin")
|
|
}
|
|
|
|
return fds, pattern, nil
|
|
}
|