Skip to content

Commit

Permalink
fix(merge): Check for errors on stderr
Browse files Browse the repository at this point in the history
Signed-off-by: Cezar Craciunoiu <cezar.craciunoiu@gmail.com>
  • Loading branch information
craciunoiuc committed Jan 9, 2024
1 parent f88b875 commit 8319089
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions cmd/governctl/pr/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package pr

import (
"bufio"
"bytes"
"context"
"fmt"
Expand Down Expand Up @@ -288,14 +289,24 @@ func (opts *Merge) Run(ctx context.Context, args []string) error {
var output []byte
cmd = exec.Command("gh", "auth", "token")
cmd.Stderr = log.G(ctx).WriterLevel(logrus.ErrorLevel)
stderr, _ := cmd.StderrPipe()
scanner := bufio.NewScanner(stderr)
if output, err = cmd.Output(); err != nil {
return fmt.Errorf("could not backup token: %w", err)
if scanner.Scan() {
if strings.HasPrefix(scanner.Text(), "no oauth token found") {
log.G(ctx).Warn("no token to back up, skipping")
token = ""
} else {
return fmt.Errorf("could not backup token: %w", err)
}
} else {
return fmt.Errorf("could not read error to backup token: %w", err)
}
} else {
token = string(output)
}
token = string(output)

if strings.HasPrefix(token, "no oauth token found") {
token = ""
} else if !strings.HasPrefix(token, "gh") {
if token != "" && !strings.HasPrefix(token, "gh") {
return fmt.Errorf("could not backup token, invalid format (try running `gh auth token` manually): %w", err)
}

Expand Down

0 comments on commit 8319089

Please sign in to comment.