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