-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
152 lines (127 loc) · 3.36 KB
/
main.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"net/http"
"os"
"strings"
)
// github api to get the list of gitignore files
const contentURL = "https://api.github.com/repos/github/gitignore/contents"
func main() {
// parse flags
flag.Parse()
// check if there are too many arguments
if flag.NArg() > 1 {
fmt.Println("Error: too many arguments")
return
}
// get the latest list of files
files, err := getList()
if err != nil {
fmt.Println("Error fetching file list:", err)
return
}
// filter the list of files
filteredFiles := filterList(files)
// if the argument is 1, check if the file exists on the list
usingParam := false
if flag.NArg() == 1 {
usingParam = true
for _, filteredFile := range filteredFiles {
if strings.EqualFold(strings.TrimSpace(filteredFile.Name), strings.TrimSpace(flag.Arg(0))) {
// download and save the file
if err := downloadFile(filteredFile); err != nil {
fmt.Println("Error downloading file:", err)
return
}
// print success message
fmt.Printf("Successfully downloaded .gitignore for \"%v\"", filteredFile.Name)
return
}
}
}
// print the list of files
fmt.Println("Available gitignore files:")
for i, filteredFile := range filteredFiles {
// print the index and the name of the file
fmt.Printf("%d. %s\n", i+1, filteredFile.Name)
}
// when the argument is 1, the file was not found, warn the user
if usingParam {
fmt.Printf("\nWarning: gitignore file for \"%v\" not found\n", flag.Arg(0))
}
// wait for user input
var selected int
fmt.Print("Please enter the number of your choice: ")
fmt.Scanln(&selected)
// check if the choice is valid
if selected < 1 || selected > len(filteredFiles) {
fmt.Println("Error: invalid choice")
return
}
// download and save the file
if err := downloadFile(filteredFiles[selected-1]); err != nil {
fmt.Println("Error downloading file:", err)
return
}
// print success message
fmt.Printf("Successfully downloaded .gitignore for %v", filteredFiles[selected-1].Name)
}
// struct for github api response
type File struct {
Name string `json:"name"`
Type string `json:"type"`
DownloadURL string `json:"download_url"`
}
func getList() ([]File, error) {
// get the list of files from github
resp, err := http.Get(contentURL)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var files []File
if err = json.Unmarshal(body, &files); err != nil {
return nil, err
}
return files, nil
}
func filterList(files []File) []File {
// filter the list of files
var filteredFiles []File
for _, file := range files {
// only include files that are not directories and end with .gitignore
if file.Type != "dir" && strings.HasSuffix(file.Name, ".gitignore") {
// remove the .gitignore extension
file.Name = strings.TrimSuffix(file.Name, ".gitignore")
filteredFiles = append(filteredFiles, file)
}
}
return filteredFiles
}
func downloadFile(file File) error {
// get the file from github
resp, err := http.Get(file.DownloadURL)
if err != nil {
return err
}
defer resp.Body.Close()
// create the file if it doesn't exist
newFile, err := os.Create(".gitignore")
if err != nil {
return err
}
defer newFile.Close()
// write the file
if _, err = io.Copy(newFile, resp.Body); err != nil {
return err
}
return nil
}