-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat: Refactor repositories download contents #4153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+204
−160
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e66fe34
feat: Refactor repositories download contents
stevehipwell 55c3ae6
fixup! feat: Refactor repositories download contents
stevehipwell c0dca2a
fixup! feat: Refactor repositories download contents
stevehipwell ba15a1c
fixup! feat: Refactor repositories download contents
stevehipwell 7b15366
fixup! feat: Refactor repositories download contents
stevehipwell d25a1ae
fixup! feat: Refactor repositories download contents
stevehipwell 52d75be
fixup! feat: Refactor repositories download contents
stevehipwell 77fd8f2
fixup! feat: Refactor repositories download contents
stevehipwell 45817d9
fixup! feat: Refactor repositories download contents
stevehipwell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| // Copyright 2026 The go-github AUTHORS. All rights reserved. | ||
| // | ||
| // Use of this source code is governed by a BSD-style | ||
| // license that can be found in the LICENSE file. | ||
|
|
||
| // The contents command utilizes go-github as a CLI tool for | ||
| // downloading the contents of a file in a repository. | ||
| // It takes an inputs of the repository owner, repository name, path to the | ||
| // file in the repository, reference (branch, tag or commit SHA), and output | ||
| // path for the downloaded file. It then uses the Repositories.DownloadContents | ||
| // method to download the file and saves it to the specified output path. | ||
| package main | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "context" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/google/go-github/v84/github" | ||
| ) | ||
|
|
||
| func main() { | ||
| fmt.Println("This example will download the contents of a file from a GitHub repository.") | ||
|
|
||
| r := bufio.NewReader(os.Stdin) | ||
|
|
||
| fmt.Print("Repository Owner: ") | ||
| owner, _ := r.ReadString('\n') | ||
| owner = strings.TrimSpace(owner) | ||
|
|
||
| fmt.Print("Repository Name: ") | ||
| repo, _ := r.ReadString('\n') | ||
| repo = strings.TrimSpace(repo) | ||
|
|
||
| fmt.Print("Repository Path: ") | ||
| repoPath, _ := r.ReadString('\n') | ||
| repoPath = strings.TrimSpace(repoPath) | ||
|
|
||
| fmt.Print("Reference (branch, tag or commit SHA): ") | ||
| ref, _ := r.ReadString('\n') | ||
| ref = strings.TrimSpace(ref) | ||
|
|
||
| fmt.Print("Output Path: ") | ||
| outputPath, _ := r.ReadString('\n') | ||
| outputPath = filepath.Clean(strings.TrimSpace(outputPath)) | ||
|
|
||
| fmt.Printf("\nDownloading %v/%v/%v at ref %v to %v...\n", owner, repo, repoPath, ref, outputPath) | ||
|
|
||
| client := github.NewClient(nil) | ||
|
|
||
| rc, _, err := client.Repositories.DownloadContents(context.Background(), owner, repo, repoPath, &github.RepositoryContentGetOptions{Ref: ref}) | ||
| if err != nil { | ||
| fmt.Printf("Error: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
| defer rc.Close() | ||
|
|
||
| f, err := os.Create(outputPath) //#nosec G703 -- path is validated above | ||
| if err != nil { | ||
| fmt.Printf("Error: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
| defer f.Close() | ||
|
|
||
| if _, err := io.Copy(f, rc); err != nil { | ||
| fmt.Printf("Error: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| fmt.Println("Download completed.") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.