|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "os" |
| 9 | + "strings" |
| 10 | +) |
| 11 | +const yellow = "\033[33m" |
| 12 | +const reset = "\033[0m" |
| 13 | + |
| 14 | +const banner = yellow + ` |
| 15 | + _ __ __ __ |
| 16 | + ___ _(_) /____ ___ _/ /_/ / ___ |
| 17 | + / _ \/ / __/ _ \/ _ \/ __/ _ \(_-< |
| 18 | + \_, /_/\__/ .__/\_,_/\__/_//_/___/ |
| 19 | +/___/ /_/ ` + reset + ` v0.0.1 |
| 20 | +
|
| 21 | +Made by https://linkedin.com/in/mllamazares |
| 22 | +
|
| 23 | +--- |
| 24 | +
|
| 25 | +` |
| 26 | + |
| 27 | +type ApiResponse struct { |
| 28 | + Tree []struct { |
| 29 | + Path string `json:"path"` |
| 30 | + } `json:"tree"` |
| 31 | +} |
| 32 | + |
| 33 | +func main() { |
| 34 | + repoUrl := flag.String("u", "", "GitHub repository URL") |
| 35 | + outputFile := flag.String("o", "", "Output file (optional)") |
| 36 | + branch := flag.String("b", "master", "Branch name (optional, default: master)") |
| 37 | + silent := flag.Bool("silent", false, "Omit banner and sysout printing") |
| 38 | + help := flag.Bool("help", false, "Display help") |
| 39 | + |
| 40 | + flag.Parse() |
| 41 | + |
| 42 | + fmt.Print(banner) |
| 43 | + |
| 44 | + // Display help if -h is provided |
| 45 | + if *help { |
| 46 | + flag.Usage() |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + // Validate input |
| 51 | + if *repoUrl == "" { |
| 52 | + fmt.Println("Repository URL is required.") |
| 53 | + flag.Usage() |
| 54 | + os.Exit(1) |
| 55 | + } |
| 56 | + |
| 57 | + // Extract owner and repo name from URL provided |
| 58 | + parts := strings.Split(strings.TrimPrefix(*repoUrl, "https://github.com/"), "/") |
| 59 | + if len(parts) != 2 { |
| 60 | + fmt.Println("Invalid repository URL format.") |
| 61 | + os.Exit(1) |
| 62 | + } |
| 63 | + owner := parts[0] |
| 64 | + repo := parts[1] |
| 65 | + |
| 66 | + // Get the GitHub API URL |
| 67 | + apiUrl := fmt.Sprintf("https://api.github.com/repos/%s/%s/git/trees/%s?recursive=1", owner, repo, *branch) |
| 68 | + |
| 69 | + resp, err := http.Get(apiUrl) |
| 70 | + if err != nil { |
| 71 | + fmt.Println("Error making request:", err) |
| 72 | + os.Exit(1) |
| 73 | + } |
| 74 | + defer resp.Body.Close() |
| 75 | + |
| 76 | + if resp.StatusCode != http.StatusOK { |
| 77 | + fmt.Printf("Failed to retrieve data: %s\n", resp.Status) |
| 78 | + os.Exit(1) |
| 79 | + } |
| 80 | + |
| 81 | + // Parse the JSON response |
| 82 | + var apiResponse ApiResponse |
| 83 | + err = json.NewDecoder(resp.Body).Decode(&apiResponse) |
| 84 | + if err != nil { |
| 85 | + fmt.Println("Error parsing JSON:", err) |
| 86 | + os.Exit(1) |
| 87 | + } |
| 88 | + |
| 89 | + // Create the output file if defined |
| 90 | + var file *os.File |
| 91 | + if *outputFile != "" { |
| 92 | + file, err = os.Create(*outputFile) |
| 93 | + if err != nil { |
| 94 | + fmt.Println("Error creating output file:", err) |
| 95 | + os.Exit(1) |
| 96 | + } |
| 97 | + defer file.Close() |
| 98 | + } |
| 99 | + |
| 100 | + // Print to sysout and optionally write the paths to the file |
| 101 | + for _, item := range apiResponse.Tree { |
| 102 | + if !*silent { |
| 103 | + fmt.Println(item.Path) |
| 104 | + } |
| 105 | + if file != nil { |
| 106 | + _, err := file.WriteString(item.Path + "\n") |
| 107 | + if err != nil { |
| 108 | + fmt.Println("Error writing to output file:", err) |
| 109 | + os.Exit(1) |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments