Skip to content

Commit d25a1ae

Browse files
committed
fixup! feat: Refactor repositories download contents
1 parent 7b15366 commit d25a1ae

File tree

1 file changed

+48
-43
lines changed

1 file changed

+48
-43
lines changed

example/contents/main.go

Lines changed: 48 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,67 +3,72 @@
33
// Use of this source code is governed by a BSD-style
44
// license that can be found in the LICENSE file.
55

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.
2112
package main
2213

2314
import (
15+
"bufio"
2416
"context"
2517
"fmt"
2618
"io"
2719
"os"
20+
"strings"
2821

2922
"github.com/google/go-github/v84/github"
3023
)
3124

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})
3555
if err != nil {
36-
return nil, err
56+
fmt.Printf("Error: %v\n", err)
57+
os.Exit(1)
3758
}
3859
defer rc.Close()
3960

40-
by, err := io.ReadAll(rc)
61+
f, err := os.Create(outputPath)
4162
if err != nil {
42-
return nil, err
63+
fmt.Printf("Error: %v\n", err)
64+
os.Exit(1)
4365
}
66+
defer f.Close()
4467

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)
6171
}
6272

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.")
6974
}

0 commit comments

Comments
 (0)