-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
117 lines (95 loc) · 2.47 KB
/
main.go
File metadata and controls
117 lines (95 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"path"
"strings"
"github.com/google/go-github/v53/github"
)
func main() {
flag.Parse()
args := flag.Args()
if len(args) != 1 {
fmt.Println("Usage: go run main.go <GitHub_Folder_Link>")
os.Exit(1)
}
folderLink := args[0]
if !strings.Contains(folderLink, "/tree/") {
fmt.Println("Invalid GitHub folder link.")
os.Exit(1)
}
// Initialize GitHub client
ctx := context.Background()
client := newGitHubClient()
// Parse owner, repository, and branch from the folder link
parts := strings.Split(strings.TrimPrefix(folderLink, "https://github.com/"), "/")
if len(parts) < 4 {
fmt.Println("Invalid GitHub folder link format.")
os.Exit(1)
}
owner := parts[0]
repo := parts[1]
branch := parts[3]
lastPathComponent := parts[len(parts)-1]
// Get the list of files in the folder
fileLinks, err := getFileLinksFromGitHub(ctx, client, owner, repo, branch, strings.Join(parts[4:], "/"))
if err != nil {
fmt.Printf("Error getting file links: %s\n", err)
os.Exit(1)
}
fmt.Printf("Found %d file(s) in the folder.\n", len(fileLinks))
// Create a folder with the last path component name
err = os.MkdirAll(lastPathComponent, os.ModePerm)
if err != nil {
fmt.Printf("Error creating folder: %s\n", err)
os.Exit(1)
}
err = downloadFiles(lastPathComponent, fileLinks)
if err != nil {
fmt.Printf("Error downloading files: %s\n", err)
os.Exit(1)
}
fmt.Println("Download successful!")
}
func newGitHubClient() *github.Client {
return github.NewClient(nil)
}
func getFileLinksFromGitHub(ctx context.Context, client *github.Client, owner, repo, branch, path string) ([]string, error) {
_, contents, _, err := client.Repositories.GetContents(ctx, owner, repo, path, &github.RepositoryContentGetOptions{
Ref: branch,
})
if err != nil {
return nil, err
}
var fileLinks []string
for _, content := range contents {
if *content.Type == "file" {
fileLinks = append(fileLinks, *content.DownloadURL)
}
}
return fileLinks, nil
}
func downloadFiles(folderName string, fileLinks []string) error {
for _, link := range fileLinks {
resp, err := http.Get(link)
if err != nil {
return err
}
defer resp.Body.Close()
fileName := path.Base(link)
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
err = ioutil.WriteFile(path.Join(folderName, fileName), data, 0644)
if err != nil {
return err
}
fmt.Printf("Downloaded: %s\n", fileName)
}
return nil
}