@@ -128,17 +128,14 @@ func handleFilePull(client *http.Client, language string) (int64, error) {
128128 fmt .Sprintf ("%s%s" , language , ".gitignore" ),
129129 )
130130
131- // todo: check if available first, then prompt, only the do download
132- fmt .Printf ("Pulling '%s'...\n " , langUrl )
133- body , err := downLoadFile (client , langUrl )
134- if err != nil {
135- return 0 , fmt .Errorf ("failed to download file %s: %w" , langUrl , err )
131+ ok , _ := resourceAvailable (client , langUrl )
132+ if ! ok {
133+ return 0 , ErrGitignoreNotFound
136134 }
137- defer body .Close ()
138135
139136 var shouldAppend bool
140137 var userAction choice = "overwrite" // Default to overwrite if no file exists
141- _ , err = os .Stat (GIT_IGNORE )
138+ _ , err : = os .Stat (GIT_IGNORE )
142139 if err == nil {
143140 // file exists
144141 choice , err := promptForOverwrite ()
@@ -172,6 +169,13 @@ func handleFilePull(client *http.Client, language string) (int64, error) {
172169 fileMode = os .O_TRUNC | os .O_CREATE | os .O_WRONLY
173170 }
174171
172+ fmt .Printf ("Pulling '%s'...\n " , langUrl )
173+ body , err := downLoadFile (client , langUrl )
174+ if err != nil {
175+ return 0 , fmt .Errorf ("failed to download file %s: %w" , langUrl , err )
176+ }
177+ defer body .Close ()
178+
175179 out , err := os .OpenFile (GIT_IGNORE , fileMode , 0644 )
176180 if err != nil {
177181 return 0 , fmt .Errorf ("failed to open/create file %s: %w" , GIT_IGNORE , err )
@@ -310,3 +314,20 @@ func promptForOverwrite() (choice, error) {
310314 }
311315 }
312316}
317+
318+ func resourceAvailable (client * http.Client , url string ) (bool , error ) {
319+
320+ resp , err := client .Head (url )
321+ if err != nil {
322+ return false , fmt .Errorf ("failed to make HTTP request to %s: %w" , url , err )
323+ }
324+ defer resp .Body .Close ()
325+ if resp .StatusCode == http .StatusNotFound {
326+ return false , ErrGitignoreNotFound
327+ }
328+
329+ if resp .StatusCode != http .StatusOK {
330+ return false , fmt .Errorf ("received non-OK HTTP status for %s: %s" , url , resp .Status )
331+ }
332+ return true , nil
333+ }
0 commit comments