feat: stdin default

Signed-off-by: jolheiser <john.olheiser@gmail.com>
main
jolheiser 2024-06-06 20:59:01 -05:00
parent 48b98f65aa
commit ae439db5e5
Signed by: jolheiser
GPG Key ID: B853ADA5DA7BBF7A
1 changed files with 17 additions and 9 deletions

View File

@ -1,9 +1,9 @@
package main
import (
"errors"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"strings"
@ -40,19 +40,27 @@ func maine() error {
return err
}
if fs.NArg() < 1 {
return errors.New("cfg requires at least an input file")
}
cleanArg := func(arg string) string {
return strings.TrimPrefix(filepath.Ext(arg), ".")
}
in := fs.Arg(0)
if unmarshal == nil {
if err := fromFunc(cleanArg(fs.Arg(0))); err != nil {
in := os.Stdin
if fs.NArg() > 0 {
fi, err := os.Open(fs.Arg(0))
if err != nil {
return err
}
defer fi.Close()
in = fi
if unmarshal == nil {
if err := fromFunc(cleanArg(fs.Arg(0))); err != nil {
return err
}
}
} else if unmarshal == nil {
unmarshal = cfg.JSON.Unmarshal
}
out := os.Stdout
@ -75,7 +83,7 @@ func maine() error {
var data any
inData, err := os.ReadFile(in)
inData, err := io.ReadAll(in)
if err != nil {
return err
}