-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitHubCli.go
53 lines (44 loc) · 1.17 KB
/
gitHubCli.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
)
// getGitHubToken returns value of GITHUB_TOKEN environment variable
func getGitHubToken() string {
token := os.Getenv("GITHUB_TOKEN")
if token != "" {
return token
}
// retrieve GITHUB_TOKEN using "gh config" command
cmd := exec.Command("gh", "config", "get", "oauth_token", "-h", "github.com")
output, err := cmd.Output()
if err != nil {
fmt.Println("Error retrieving GITHUB_TOKEN:", err)
os.Exit(1)
}
return strings.TrimSuffix(string(output), "\n")
}
type repoView struct {
Name string `json:"name"`
Owner owner `json:"owner"`
}
type owner struct {
Id string `json:"id"`
Login string `json:"login"`
}
// getActiveRepoInfo returns Name and Owner of the active git repository
func getActiveRepoInfo() (*repoView, error) {
cmd := exec.Command("gh", "repo", "view", "--json", "name,owner")
ghRepoJson, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("error executing 'gh repo view --json name,owner: %v", err)
}
var repo *repoView
if err := json.Unmarshal(ghRepoJson, &repo); err != nil {
return nil, fmt.Errorf("can not unmarshal JSON: %v", err)
}
return repo, nil
}