Skip to content

Commit

Permalink
Merge pull request #6 from kununu/get-key-from-stdin
Browse files Browse the repository at this point in the history
Get key from stdin
  • Loading branch information
enkodr committed May 11, 2023
2 parents 0dd6856 + 3092a90 commit 9c6e117
Showing 1 changed file with 39 additions and 7 deletions.
46 changes: 39 additions & 7 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main

import (
"bufio"
"flag"
"fmt"
"os"
"strings"

"github.com/kununu/go-github"
)
Expand All @@ -22,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 @@ -31,21 +34,50 @@ func main() {
}
if key == "" {
key = os.Getenv("GITHUB_KEY_PATH")
if key == "" {
// Read the key from STDIN
stat, err := os.Stdin.Stat()
if err != nil {
fmt.Printf("error in stdin: %s", err)
os.Exit(1)
}
if (stat.Mode() & os.ModeNamedPipe) == 0 {
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)
}
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 @@ -56,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 9c6e117

Please sign in to comment.