-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitpaths.go
117 lines (98 loc) · 2.52 KB
/
gitpaths.go
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 (
"encoding/json"
"flag"
"fmt"
"net/http"
"os"
"strings"
)
const yellow = "\033[33m"
const reset = "\033[0m"
const banner = yellow + `
_ __ __ __
___ _(_) /____ ___ _/ /_/ / ___
/ _ \/ / __/ _ \/ _ \/ __/ _ \(_-<
\_, /_/\__/ .__/\_,_/\__/_//_/___/
/___/ /_/ ` + reset + ` v0.0.1
Made by https://linkedin.com/in/mllamazares
---
`
type ApiResponse struct {
Tree []struct {
Path string `json:"path"`
} `json:"tree"`
}
func main() {
repoUrl := flag.String("u", "", "GitHub repository URL")
outputFile := flag.String("o", "", "Output file (optional)")
branch := flag.String("b", "main", "Branch name (default: main)")
silent := flag.Bool("silent", false, "Omit banner and sysout printing")
help := flag.Bool("help", false, "Display help")
flag.Parse()
// Display banner if not in silent mode
if !*silent {
fmt.Print(banner)
}
// Display help if -h is provided
if *help {
flag.Usage()
return
}
// Validate input
if *repoUrl == "" {
fmt.Println("Repository URL is required.")
flag.Usage()
os.Exit(1)
}
// Extract owner and repo name from URL provided
parts := strings.Split(strings.TrimPrefix(*repoUrl, "https://github.com/"), "/")
if len(parts) != 2 {
fmt.Println("Invalid repository URL format.")
os.Exit(1)
}
owner := parts[0]
repo := parts[1]
// Get the GitHub API URL
apiUrl := fmt.Sprintf("https://api.github.com/repos/%s/%s/git/trees/%s?recursive=1", owner, repo, *branch)
resp, err := http.Get(apiUrl)
if err != nil {
fmt.Println("Error making request:", err)
os.Exit(1)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
fmt.Printf("Failed to retrieve data: %s\nVerify that the branch you want to retrieve is %s. If not, specify it with -b", resp.Status, *branch)
os.Exit(1)
}
// Parse the JSON response
var apiResponse ApiResponse
err = json.NewDecoder(resp.Body).Decode(&apiResponse)
if err != nil {
fmt.Println("Error parsing JSON:", err)
os.Exit(1)
}
// Create the output file if defined
var file *os.File
if *outputFile != "" {
file, err = os.Create(*outputFile)
if err != nil {
fmt.Println("Error creating output file:", err)
os.Exit(1)
}
defer file.Close()
}
// Print to sysout and optionally write the paths to the file
for _, item := range apiResponse.Tree {
if !*silent {
fmt.Println(item.Path)
}
if file != nil {
_, err := file.WriteString(item.Path + "\n")
if err != nil {
fmt.Println("Error writing to output file:", err)
os.Exit(1)
}
}
}
}