|
3 | 3 | // Use of this source code is governed by a BSD-style |
4 | 4 | // license that can be found in the LICENSE file. |
5 | 5 |
|
6 | | -// The commitpr command utilizes go-github as a CLI tool for |
7 | | -// pushing files to a branch and creating a pull request from it. |
8 | | -// It takes an auth token as an environment variable and creates |
9 | | -// the commit and the PR under the account affiliated with that token. |
10 | | -// |
11 | | -// The purpose of this example is to show how to use refs, trees and commits to |
12 | | -// create commits and pull requests. |
13 | | -// |
14 | | -// Note, if you want to push a single file, you probably prefer to use the |
15 | | -// content API. An example is available here: |
16 | | -// https://pkg.go.dev/github.com/google/go-github/v84/github#example-RepositoriesService-CreateFile |
17 | | -// |
18 | | -// Note, for this to work at least 1 commit is needed, so you if you use this |
19 | | -// after creating a repository you might want to make sure you set `AutoInit` to |
20 | | -// `true`. |
| 6 | +// The contents command utilizes go-github as a CLI tool for |
| 7 | +// downloading the contents of a file in a repository. |
| 8 | +// It takes an inputs of the repository owner, repository name, path to the |
| 9 | +// file in the repository, reference (branch, tag or commit SHA), and output |
| 10 | +// path for the downloaded file. It then uses the Repositories.DownloadContents |
| 11 | +// method to download the file and saves it to the specified output path. |
21 | 12 | package main |
22 | 13 |
|
23 | 14 | import ( |
| 15 | + "bufio" |
24 | 16 | "context" |
25 | 17 | "fmt" |
26 | 18 | "io" |
27 | 19 | "os" |
| 20 | + "strings" |
28 | 21 |
|
29 | 22 | "github.com/google/go-github/v84/github" |
30 | 23 | ) |
31 | 24 |
|
32 | | -// downloadContents downloads the contents of a file in a repository and returns it as a byte slice. |
33 | | -func downloadContents(ctx context.Context, client *github.Client, owner, repo, path, ref string) ([]byte, error) { |
34 | | - rc, _, err := client.Repositories.DownloadContents(ctx, owner, repo, path, &github.RepositoryContentGetOptions{Ref: ref}) |
| 25 | +func main() { |
| 26 | + fmt.Println("This example will download the contents of a file from a GitHub repository.") |
| 27 | + |
| 28 | + r := bufio.NewReader(os.Stdin) |
| 29 | + |
| 30 | + fmt.Print("Repository Owner: ") |
| 31 | + owner, _ := r.ReadString('\n') |
| 32 | + owner = strings.TrimSpace(owner) |
| 33 | + |
| 34 | + fmt.Print("Repository Name: ") |
| 35 | + repo, _ := r.ReadString('\n') |
| 36 | + repo = strings.TrimSpace(repo) |
| 37 | + |
| 38 | + fmt.Print("Repository Path: ") |
| 39 | + repoPath, _ := r.ReadString('\n') |
| 40 | + repoPath = strings.TrimSpace(repoPath) |
| 41 | + |
| 42 | + fmt.Print("Reference (branch, tag or commit SHA): ") |
| 43 | + ref, _ := r.ReadString('\n') |
| 44 | + ref = strings.TrimSpace(ref) |
| 45 | + |
| 46 | + fmt.Print("Output Path: ") |
| 47 | + outputPath, _ := r.ReadString('\n') |
| 48 | + outputPath = strings.TrimSpace(outputPath) |
| 49 | + |
| 50 | + fmt.Printf("\nDownloading %v/%v/%v at ref %v to %v...\n", owner, repo, repoPath, ref, outputPath) |
| 51 | + |
| 52 | + client := github.NewClient(nil) |
| 53 | + |
| 54 | + rc, _, err := client.Repositories.DownloadContents(context.Background(), owner, repo, repoPath, &github.RepositoryContentGetOptions{Ref: ref}) |
35 | 55 | if err != nil { |
36 | | - return nil, err |
| 56 | + fmt.Printf("Error: %v\n", err) |
| 57 | + os.Exit(1) |
37 | 58 | } |
38 | 59 | defer rc.Close() |
39 | 60 |
|
40 | | - by, err := io.ReadAll(rc) |
| 61 | + f, err := os.Create(outputPath) |
41 | 62 | if err != nil { |
42 | | - return nil, err |
| 63 | + fmt.Printf("Error: %v\n", err) |
| 64 | + os.Exit(1) |
43 | 65 | } |
| 66 | + defer f.Close() |
44 | 67 |
|
45 | | - fmt.Printf("Downloaded %v/%v/%v as %d bytes\n", owner, repo, path, len(by)) |
46 | | - return by, nil |
47 | | -} |
48 | | - |
49 | | -func main() { |
50 | | - client := github.NewClient(nil) |
51 | | - |
52 | | - t := []struct { |
53 | | - owner string |
54 | | - repo string |
55 | | - path string |
56 | | - ref string |
57 | | - }{ |
58 | | - {"google", "go-github", "README.md", "master"}, |
59 | | - {"github", "rest-api-description", "descriptions/api.github.com/api.github.com.2026-03-10.yaml", "main"}, |
60 | | - {"ScoopInstaller", "Main", "bucket/yq.json", "master"}, |
| 68 | + if _, err := io.Copy(f, rc); err != nil { |
| 69 | + fmt.Printf("Error: %v\n", err) |
| 70 | + os.Exit(1) |
61 | 71 | } |
62 | 72 |
|
63 | | - for _, v := range t { |
64 | | - if _, err := downloadContents(context.Background(), client, v.owner, v.repo, v.path, v.ref); err != nil { |
65 | | - fmt.Printf("Error: %v\n", err) |
66 | | - os.Exit(1) |
67 | | - } |
68 | | - } |
| 73 | + fmt.Println("Download completed.") |
69 | 74 | } |
0 commit comments