diff --git a/main.go b/main.go index e4263ad..2289c5d 100644 --- a/main.go +++ b/main.go @@ -1,10 +1,12 @@ package main import ( + "bufio" "bytes" "errors" "flag" "fmt" + "os" "strconv" "text/template" ) @@ -72,8 +74,16 @@ func Run(args []string) { return } - coverage, _ := strconv.ParseFloat(args[0], 64) - badge, err := RenderBadge(int(coverage)) + coverage := args[0] + + if coverage == "-" { + scanner := bufio.NewScanner(os.Stdin) + scanner.Scan() + coverage = scanner.Text() + } + + coverageValue, _ := strconv.ParseFloat(coverage, 64) + badge, err := RenderBadge(int(coverageValue)) if err != nil { panic(err) diff --git a/main_test.go b/main_test.go index 1aeedb1..912310c 100644 --- a/main_test.go +++ b/main_test.go @@ -1,6 +1,8 @@ package main import ( + "io/ioutil" + "os" "strings" "testing" ) @@ -79,5 +81,18 @@ func TestCovbadger(t *testing.T) { main() Run([]string{"99"}) + + userInput := []byte("90.09") + tmpfile, _ := ioutil.TempFile("", "covbadger-stdin") + defer os.Remove(tmpfile.Name()) + tmpfile.Write(userInput) + tmpfile.Seek(0, 0) + + _stdin := os.Stdin + defer func() { os.Stdin = _stdin }() + os.Stdin = tmpfile + Run([]string{"-"}) + tmpfile.Close() + Run([]string{"-99"}) }