()
| 25 | ) |
| 26 | |
| 27 | func main() { |
| 28 | fmt.Println("This example will download the contents of a file from a GitHub repository.") |
| 29 | |
| 30 | r := bufio.NewReader(os.Stdin) |
| 31 | |
| 32 | fmt.Print("Repository Owner: ") |
| 33 | owner, _ := r.ReadString('\n') |
| 34 | owner = strings.TrimSpace(owner) |
| 35 | |
| 36 | fmt.Print("Repository Name: ") |
| 37 | repo, _ := r.ReadString('\n') |
| 38 | repo = strings.TrimSpace(repo) |
| 39 | |
| 40 | fmt.Print("Repository Path: ") |
| 41 | repoPath, _ := r.ReadString('\n') |
| 42 | repoPath = strings.TrimSpace(repoPath) |
| 43 | |
| 44 | fmt.Print("Reference (branch, tag or commit SHA): ") |
| 45 | ref, _ := r.ReadString('\n') |
| 46 | ref = strings.TrimSpace(ref) |
| 47 | |
| 48 | fmt.Print("Output Path: ") |
| 49 | outputPath, _ := r.ReadString('\n') |
| 50 | outputPath = filepath.Clean(strings.TrimSpace(outputPath)) |
| 51 | |
| 52 | fmt.Printf("\nDownloading %v/%v/%v at ref %v to %v...\n", owner, repo, repoPath, ref, outputPath) |
| 53 | |
| 54 | client, err := github.NewClient() |
| 55 | if err != nil { |
| 56 | log.Fatalf("Error creating GitHub client: %v", err) |
| 57 | } |
| 58 | |
| 59 | rc, _, err := client.Repositories.DownloadContents(context.Background(), owner, repo, repoPath, &github.RepositoryContentGetOptions{Ref: ref}) |
| 60 | if err != nil { |
| 61 | log.Fatalf("Error downloading contents: %v", err) |
| 62 | } |
| 63 | defer rc.Close() |
| 64 | |
| 65 | f, err := os.Create(outputPath) //#nosec G703 -- path is validated above |
| 66 | if err != nil { |
| 67 | log.Fatalf("Error creating output file: %v", err) |
| 68 | } |
| 69 | defer f.Close() |
| 70 | |
| 71 | if _, err := io.Copy(f, rc); err != nil { |
| 72 | log.Fatalf("Error writing to output file: %v", err) |
| 73 | } |
| 74 | |
| 75 | fmt.Println("Download completed.") |
| 76 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…