Skip to content

Commit

Permalink
Fix issue when STDIN has multi-line content
Browse files Browse the repository at this point in the history
  • Loading branch information
enkodr committed May 11, 2023
1 parent 89b2ede commit 3092a90
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"flag"
"fmt"
"os"
"strings"

"github.com/kununu/go-github"
)
Expand All @@ -23,6 +24,7 @@ func init() {
}

func main() {
keyBytes := []byte{}
// Get the values from the environment variables if they are set with parameters
if appId == "" {
appId = os.Getenv("GITHUB_APP_ID")
Expand All @@ -43,29 +45,39 @@ func main() {
fmt.Printf("you need to pass the private key either with `-k` parameter or by setting GITHUB_KEY_PATH or even passing through STDIN\n")
os.Exit(1)
}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
key = scanner.Text()
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "reading standard input:", err)
var lines []string
reader := bufio.NewReader(os.Stdin)
for {
// read line from stdin using newline as separator
line, _ := reader.ReadString('\n')
// if line is empty, break the loop
if len(strings.TrimSpace(line)) == 0 {
break
}
//append the line to a slice
lines = append(lines, line)
keyBytes = append(keyBytes, []byte(line)...)
}
key = "stdin"
}
}

// Verify if the necessary information is set
if appId == "" || key == "" || instId == "" {
if appId == "" || instId == "" {
fmt.Println("You need to define the App ID and the path to the key file")
fmt.Println("by passing the values the -a, -i and -k options or")
fmt.Println("by setting GITHUB_APP_ID, GITHUB_INST_ID and GITHUB_KEY_PATH environment variables.")
os.Exit(0)
}

// Read the key from the file
keyBytes, err := os.ReadFile(key)
if err != nil {
fmt.Println("error reading the key file")
os.Exit(0)
if key != "stdin" {
var err error
keyBytes, err = os.ReadFile(key)
if err != nil {
fmt.Println("error reading the key file")
os.Exit(1)
}
}

// Create a new GitHubApp
Expand All @@ -76,14 +88,14 @@ func main() {
})
if err != nil {
fmt.Println(err.Error())
os.Exit(0)
os.Exit(1)
}

// Get GitHub auth token for the specified installation
token, err := ghApp.GetAccessToken()
if err != nil {
fmt.Println(err.Error())
os.Exit(0)
os.Exit(1)
}

// Printout GitHub Token
Expand Down

0 comments on commit 3092a90

Please sign in to comment.