diff --git a/main.go b/main.go index 645a420..85b2197 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,8 @@ import ( "time" "github.com/bradleyfalzon/ghinstallation" + "github.com/google/go-github/v52/github" + "github.com/k0kubun/pp/v3" ) var ( @@ -27,6 +29,7 @@ func main() { instID := flag.Int64("inst-id", 0, "Installation ID") export := flag.Bool("export", false, "show token as 'export GITHUB_TOKEN=...'") showVersion := flag.Bool("version", false, "show version info") + showInsts := flag.Bool("show-insts", false, "show all of the installations for the app") origUsage := flag.Usage flag.Usage = func() { @@ -45,17 +48,29 @@ func main() { os.Exit(0) } + key := os.Getenv("GITHUB_PRIV_KEY") + if key == "" { + log.Fatal("Please populate GITHUB_PRIV_KEY environment variable with the private key for the App") + } + + if *showInsts { + if *appID == 0 { + fmt.Fprintf(os.Stderr, "App ID is required to show the installations for the app.\n\n") + flag.Usage() + os.Exit(1) + } + + showInstallations(*appID, []byte(key)) + + return + } + if *appID == 0 || *instID == 0 { fmt.Fprintf(os.Stderr, "App ID and Installation ID are required.\n\n") flag.Usage() os.Exit(1) } - key := os.Getenv("GITHUB_PRIV_KEY") - if key == "" { - log.Fatal("Please populate GITHUB_PRIV_KEY environment variable with the private key for the App") - } - // Wrap the shared transport for use with the app ID 1 authenticating with installation ID 99. itr, err := ghinstallation.New(http.DefaultTransport, *appID, *instID, []byte(key)) if err != nil { @@ -80,3 +95,34 @@ func main() { func showExport(token string) { fmt.Printf("export GITHUB_TOKEN=%s\n", token) } + +func showInstallations(appID int64, key []byte) { + atr, err := ghinstallation.NewAppsTransport(http.DefaultTransport, appID, key) + if err != nil { + log.Fatal(err) + } + + client := github.NewClient(&http.Client{Transport: atr}) + + opts := &github.ListOptions{ + PerPage: 10, + } + + ctx, cancel := context.WithTimeout(context.Background(), time.Minute) + defer cancel() + + for { + inst, resp, err := client.Apps.ListInstallations(ctx, opts) + if err != nil { + log.Fatal(err) + } + + pp.Println(inst) + + if resp.NextPage == 0 { + break + } + + opts.Page = resp.NextPage + } +}