Skip to content

Commit

Permalink
add reading configuration from stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
frapposelli committed Jan 18, 2021
1 parent 254bfb1 commit e7528bc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
39 changes: 36 additions & 3 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"strings"

Expand All @@ -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"`
}

Expand Down Expand Up @@ -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
Expand Down
18 changes: 4 additions & 14 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package main

import (
"os"

"bytes"
"gopkg.in/yaml.v2"
)

Expand All @@ -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
}

Expand Down

0 comments on commit e7528bc

Please sign in to comment.