MCPcopy Create free account
hub / github.com/google/go-github / main

Function main

example/contents/main.go:27–76  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

25)
26
27func 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}

Callers

nothing calls this directly

Calls 4

NewClientFunction · 0.92
DownloadContentsMethod · 0.80
CloseMethod · 0.80
CreateMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…