Skip to content

Commit

Permalink
add stdin support
Browse files Browse the repository at this point in the history
  • Loading branch information
imsky committed Feb 5, 2018
1 parent 63273bb commit d5f9d79
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
14 changes: 12 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package main

import (
"bufio"
"bytes"
"errors"
"flag"
"fmt"
"os"
"strconv"
"text/template"
)
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"io/ioutil"
"os"
"strings"
"testing"
)
Expand Down Expand Up @@ -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"})
}

0 comments on commit d5f9d79

Please sign in to comment.