diff --git a/cli.go b/cli.go index 80bb510..b8e2f22 100644 --- a/cli.go +++ b/cli.go @@ -3,6 +3,7 @@ package main import ( "bufio" "fmt" + "io/ioutil" "os" "strings" @@ -27,14 +28,14 @@ type List struct { } type Check struct { - File string `short:"f" long:"file" description:"input file" default:".wwhrd.yml"` + File string `short:"f" long:"file" description:"input file, use - for stdin" default:".wwhrd.yml"` NoColor bool `long:"no-color" description:"disable colored output"` CoverageThreshold float64 `short:"c" long:"coverage" description:"coverage threshold is the minimum percentage of the file that must contain license text" default:"75"` CheckTestFiles bool `short:"t" long:"check-test-files" description:"check imported dependencies for test files"` } type Graph struct { - File string `short:"o" long:"output" description:"output file" default:"wwhrd-graph.dot"` + File string `short:"o" long:"output" description:"output file, use - for stdout" default:"wwhrd-graph.dot"` CheckTestFiles bool `short:"t" long:"check-test-files" description:"check imported dependencies for test files"` } @@ -152,12 +153,44 @@ func (c *Check) Execute(args []string) error { log.SetFormatter(&log.TextFormatter{ForceColors: true}) } - t, err := ReadConfig(c.File) + var config []byte + + if c.File == "-" { + mf := bufio.NewReader(os.Stdin) + var err error + config, err = ioutil.ReadAll(mf) + if err != nil { + return err + } + } else { + if _, err := os.Stat(c.File); os.IsNotExist(err) { + return fmt.Errorf("can't read config file: %s", err) + } + + f, err := os.Open(c.File) + if err != nil { + return err + } + + config, err = ioutil.ReadAll(f) + if err != nil { + return err + } + + if err = f.Close(); err != nil { + return err + } + + } + + t, err := ReadConfig(config) if err != nil { err = fmt.Errorf("can't read config file: %s", err) return err } + log.Debugf("Loaded config: %+v", t) + root, err := rootDir() if err != nil { return err diff --git a/config.go b/config.go index 7d9e538..c814381 100644 --- a/config.go +++ b/config.go @@ -1,8 +1,7 @@ package main import ( - "os" - + "bytes" "gopkg.in/yaml.v2" ) @@ -12,20 +11,11 @@ type Config struct { Exceptions []string `yaml:"exceptions"` } -func ReadConfig(path string) (*Config, error) { - - if _, err := os.Stat(path); os.IsNotExist(err) { - return nil, err - } - - f, err := os.Open(path) - if err != nil { - return nil, err - } - defer f.Close() +func ReadConfig(config []byte) (*Config, error) { t := Config{} - if err = yaml.NewDecoder(f).Decode(&t); err != nil { + var err error + if err = yaml.NewDecoder(bytes.NewReader(config)).Decode(&t); err != nil { return nil, err }