Skip to content

Commit 50eeb30

Browse files
prompt user for overwrite only in case of successful file download
1 parent 050ae62 commit 50eeb30

File tree

1 file changed

+47
-54
lines changed

1 file changed

+47
-54
lines changed

main.go

Lines changed: 47 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ func main() {
6969
if doList {
7070
err := handleDoList(client)
7171
if err != nil {
72-
fmt.Println(err)
72+
fmt.Fprintf(os.Stderr, "Error fetich list: %v", err)
73+
os.Exit(1)
7374
}
7475
os.Exit(0)
7576
}
@@ -83,77 +84,43 @@ func main() {
8384

8485
language := args[0]
8586
bytesWritten, err := handleFilePull(client, language)
87+
8688
if err != nil {
8789
if errors.Is(err, ErrGitignoreNotFound) {
8890
fmt.Fprintf(os.Stderr, "Error: No .gitignore file found for '%s'.\n", language)
8991
fmt.Fprintf(os.Stderr, "Please ensure you have typed the language name correctly.\n")
9092
fmt.Fprintf(os.Stderr, "For a full list of available languages, use: %s --list\n", os.Args[0])
9193
os.Exit(1)
92-
} else {
93-
fmt.Fprintf(os.Stderr, "Error downloading .gitignore file: %v\n", err)
9494
}
95+
fmt.Fprintf(os.Stderr, "Error downloading .gitignore file: %v\n", err)
9596
os.Exit(1)
9697
}
9798

98-
fmt.Printf("%d bytes written to %s\n", bytesWritten, GIT_IGNORE)
9999
fmt.Println("File downloaded successfully!")
100-
}
101-
102-
func pullGitIgnore(
103-
client *http.Client,
104-
language string,
105-
append bool,
106-
) (int64, error) {
107-
108-
langUrl, _ := url.JoinPath(
109-
RAW_PREFIX,
110-
fmt.Sprintf("%s%s", language, ".gitignore"),
111-
)
112-
113-
body, err := downLoadFile(client, langUrl)
114-
if err != nil {
115-
return 0, fmt.Errorf("failed to download file %s: %w", langUrl, err)
116-
}
117-
118-
var fileMode int
119-
if append {
120-
fileMode = os.O_APPEND | os.O_CREATE | os.O_WRONLY
121-
} else {
122-
fileMode = os.O_TRUNC | os.O_CREATE | os.O_WRONLY
123-
}
124-
125-
n, err := writeGitIgnore(body, fileMode)
126-
if err != nil {
127-
return n, fmt.Errorf("failed to write file: %w", err)
128-
}
129-
130-
return n, nil
100+
fmt.Printf("%d bytes written to %s\n", bytesWritten, GIT_IGNORE)
131101
}
132102

133103
var ErrGitignoreNotFound = errors.New(
134104
".gitignore file not found for the specified language",
135105
)
136106

137107
func downLoadFile(client *http.Client, langUrl string) (io.ReadCloser, error) {
138-
139-
var r io.ReadCloser
140108
resp, err := client.Get(langUrl)
141109
if err != nil {
142-
return r, fmt.Errorf("failed to make HTTP request to %s: %w", langUrl, err)
110+
return nil, fmt.Errorf("failed to make HTTP request to %s: %w", langUrl, err)
143111
}
144112
if resp.StatusCode == http.StatusNotFound {
145-
return r, ErrGitignoreNotFound
113+
return nil, ErrGitignoreNotFound
146114
}
147115

148116
if resp.StatusCode != http.StatusOK {
149-
return r, fmt.Errorf("received non-OK HTTP status for %s: %s", langUrl, resp.Status)
117+
return nil, fmt.Errorf("received non-OK HTTP status for %s: %s", langUrl, resp.Status)
150118
}
151119

152120
return resp.Body, nil
153121
}
154122

155123
func writeGitIgnore(source io.ReadCloser, mode int) (int64, error) {
156-
157124
defer source.Close()
158125
out, err := os.OpenFile(GIT_IGNORE, mode, 0644)
159126
if err != nil {
@@ -238,20 +205,31 @@ func displayFileList(files []string) {
238205
}
239206
}
240207

241-
func promptForOverwrite() (string, error) {
208+
type choice string
209+
210+
const (
211+
ChoiceOverwrite choice = "overwrite"
212+
ChoiceAppend choice = "append"
213+
ChoiceCancel choice = "cancel "
214+
)
215+
216+
func promptForOverwrite() (choice, error) {
242217
reader := bufio.NewReader(os.Stdin)
243218
for {
244219
fmt.Printf("A '%s' file already exists. What would you like to do? (o)verwrite / (a)ppend / (c)ancel: ", GIT_IGNORE)
245-
input, _ := reader.ReadString('\n')
220+
input, err := reader.ReadString('\n')
221+
if err != nil {
222+
return "", fmt.Errorf("failed to read user input: %w", err)
223+
}
246224
input = strings.TrimSpace(strings.ToLower(input))
247225

248226
switch input {
249227
case "o", "overwrite":
250-
return "overwrite", nil
228+
return ChoiceOverwrite, nil
251229
case "a", "append":
252-
return "append", nil
230+
return ChoiceAppend, nil
253231
case "c", "cancel":
254-
return "cancel", nil
232+
return ChoiceCancel, nil
255233
default:
256234
fmt.Println("Invalid choice. Please enter 'o', 'a', or 'c'.")
257235
}
@@ -269,11 +247,19 @@ func handleDoList(client *http.Client) error {
269247
}
270248

271249
func handleFilePull(client *http.Client, language string) (int64, error) {
250+
langUrl, _ := url.JoinPath(
251+
RAW_PREFIX,
252+
fmt.Sprintf("%s%s", language, ".gitignore"),
253+
)
272254

273-
var shouldAppend bool
274-
var userAction string = "overwrite" // Default to overwrite if no file exists
255+
body, err := downLoadFile(client, langUrl)
256+
if err != nil {
257+
return 0, fmt.Errorf("failed to download file %s: %w", langUrl, err)
258+
}
275259

276-
_, err := os.Stat(GIT_IGNORE)
260+
var shouldAppend bool
261+
var userAction choice = "overwrite" // Default to overwrite if no file exists
262+
_, err = os.Stat(GIT_IGNORE)
277263
if err == nil {
278264
// file exists
279265
choice, err := promptForOverwrite()
@@ -282,21 +268,28 @@ func handleFilePull(client *http.Client, language string) (int64, error) {
282268
}
283269
userAction = choice
284270
switch userAction {
285-
case "cancel":
271+
case ChoiceCancel:
286272
fmt.Println("Operation cancelled by user.")
287273
os.Exit(0)
288-
case "append":
274+
case ChoiceAppend:
289275
fmt.Printf("Appending to '%s'...\n", GIT_IGNORE)
290276
shouldAppend = true
291-
case "overwrite":
277+
case ChoiceOverwrite:
292278
fmt.Printf("Overwriting '%s'...\n", GIT_IGNORE)
293279
shouldAppend = false
294280
}
295281
}
296282

297-
bytesWritten, err := pullGitIgnore(client, language, shouldAppend)
283+
var fileMode int
284+
if shouldAppend {
285+
fileMode = os.O_APPEND | os.O_CREATE | os.O_WRONLY
286+
} else {
287+
fileMode = os.O_TRUNC | os.O_CREATE | os.O_WRONLY
288+
}
289+
290+
bytesWritten, err := writeGitIgnore(body, fileMode)
298291
if err != nil {
299-
return 0, err
292+
return bytesWritten, fmt.Errorf("failed to write file: %w", err)
300293
}
301294

302295
return bytesWritten, nil

0 commit comments

Comments
 (0)