Skip to content

Commit

Permalink
Add dry-run and show flags
Browse files Browse the repository at this point in the history
  • Loading branch information
heppu committed Jul 12, 2019
1 parent f107b34 commit a58d155
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
*.out

target/
dist/
1 change: 1 addition & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ before:
builds:
- env:
- CGO_ENABLED=0
- GO111MODULE=on
main: ./cmd/go-review/main.go
goos:
- linux
Expand Down
28 changes: 21 additions & 7 deletions cmd/go-review/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"flag"
"fmt"
"io"
"os"

"github.com/heppu/go-review"
Expand All @@ -14,13 +15,17 @@ var (
version = "dev"
commit = "none"
date = "unknown"

ver = flag.Bool("version", false, "print versions details and exit")
dry = flag.Bool("dry-run", false, "parse env vars and input but do not publish review")
show = flag.Bool("show", false, "print lines while parsing")
)

func main() {
v := flag.Bool("version", false, "print versions details and exit")
flag.Parse()
if *v {
fmt.Printf("%v, commit %v, built at %v", version, commit, date)

if *ver {
fmt.Fprintf(os.Stderr, "%v, commit %v, built at %v\n", version, commit, date)
os.Exit(0)
}

Expand All @@ -35,11 +40,16 @@ func main() {
auth = gerrit.BasicAuth(username, password)
}

comments, err := review.LinesToReviewComments(os.Stdin)
var input io.Reader = os.Stdin
if *show {
input = io.TeeReader(os.Stdin, os.Stdout)
}

comments, err := review.LinesToReviewComments(input)
if err != nil {
fmt.Println(err)
fmt.Fprintln(os.Stderr, err)
if err == review.ErrNoProblemsFound {
os.Exit(0)
return
}
os.Exit(1)
}
Expand All @@ -48,6 +58,10 @@ func main() {
Comments: comments,
}

if *dry {
return
}

c := gerrit.NewClient(reviewURL, auth)
if err := c.SetReview(context.Background(), changeID, revision, r); err != nil {
fmt.Println(err)
Expand All @@ -59,7 +73,7 @@ func main() {
func parseEnv(name string, must bool) string {
val := os.Getenv(name)
if must && val == "" {
fmt.Printf("%s must be set", name)
fmt.Fprintf(os.Stderr, "%s must be set\n", name)
os.Exit(1)
}
return val
Expand Down

0 comments on commit a58d155

Please sign in to comment.