Skip to content

Commit 3c58f27

Browse files
add time out
1 parent 63c62e9 commit 3c58f27

File tree

1 file changed

+41
-11
lines changed

1 file changed

+41
-11
lines changed

main.go

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,18 @@ import (
88
"fmt"
99
"io"
1010
"io/fs"
11+
"net"
1112
"net/http"
1213
"net/url"
1314
"os"
1415
"path/filepath"
1516
"slices"
1617
"strings"
18+
"time"
1719
"unicode"
1820
"unicode/utf8"
1921
)
2022

21-
const (
22-
CONTENTS_URL = "https://api.github.com/repos/github/gitignore/contents/"
23-
RAW_REFIX = "https://raw.githubusercontent.com/github/gitignore/main/"
24-
)
25-
2623
var doList bool
2724

2825
func init() {
@@ -50,8 +47,12 @@ func init() {
5047
func main() {
5148
flag.Parse()
5249

50+
client := &http.Client{
51+
Timeout: 10 * time.Second,
52+
}
53+
5354
if doList {
54-
fileData, err := fetchList(CONTENTS_URL)
55+
fileData, err := fetchList(client, CONTENTS_URL)
5556
if err != nil {
5657
fmt.Printf("could not fetch file list from %s\n", CONTENTS_URL)
5758
os.Exit(1)
@@ -80,6 +81,7 @@ func main() {
8081
var userAction string = "overwrite" // Default to overwrite if no file exists
8182
_, err := os.Stat(outputFileName)
8283
if err == nil {
84+
// file exists
8385
choice, promptErr := promptForOverwrite(outputFileName)
8486
if promptErr != nil {
8587
fmt.Fprintf(os.Stderr, "Error reading user input: %v\n", promptErr)
@@ -101,7 +103,8 @@ func main() {
101103
fmt.Fprintf(os.Stderr, "Error checking file status: %v\n", err)
102104
os.Exit(1)
103105
}
104-
err = downLoadFile(langUrl, outputFileName, shouldAppend)
106+
107+
err = downLoadFile(client, langUrl, outputFileName, shouldAppend)
105108
if err != nil {
106109
if errors.Is(err, ErrGitignoreNotFound) {
107110
fmt.Fprintf(os.Stderr, "Error: No .gitignore file found for '%s'.\n", language)
@@ -112,6 +115,7 @@ func main() {
112115
}
113116
os.Exit(1)
114117
}
118+
115119
fmt.Println("File downloaded successfully!")
116120
}
117121

@@ -121,6 +125,11 @@ const (
121125
EXT = ".gitignore"
122126
)
123127

128+
const (
129+
CONTENTS_URL = "https://api.github.com/repos/github/gitignore/contents/"
130+
RAW_REFIX = "https://raw.githubusercontent.com/github/gitignore/main/"
131+
)
132+
124133
type Content []ContentEntry
125134

126135
type ContentEntry struct {
@@ -130,10 +139,13 @@ type ContentEntry struct {
130139

131140
var ErrGitignoreNotFound = errors.New("gitignore file not found for the specified language")
132141

133-
func downLoadFile(langUrl string, filePath string, appendMode bool) error {
142+
func downLoadFile(client *http.Client, langUrl string, filePath string, appendMode bool) error {
134143

135-
resp, err := http.Get(langUrl)
144+
resp, err := client.Get(langUrl)
136145
if err != nil {
146+
if isNetWorkErr(err) {
147+
os.Exit(1)
148+
}
137149
return fmt.Errorf("failed to make HTTP request to %s: %w", langUrl, err)
138150
}
139151
defer resp.Body.Close()
@@ -185,10 +197,15 @@ func loadFiles(content Content) []string {
185197
return fileList
186198
}
187199

188-
func fetchList(url string) (Content, error) {
200+
var b = errors.Is(nil, os.ErrDeadlineExceeded)
201+
202+
func fetchList(client *http.Client, url string) (Content, error) {
189203
var content Content
190-
resp, err := http.Get(url)
204+
resp, err := client.Get(url)
191205
if err != nil {
206+
if isNetWorkErr(err) {
207+
os.Exit(1)
208+
}
192209
return content, err
193210
}
194211
defer resp.Body.Close()
@@ -245,3 +262,16 @@ func promptForOverwrite(filePath string) (string, error) {
245262
}
246263
}
247264
}
265+
266+
func isNetWorkErr(err error) bool {
267+
if err != nil {
268+
// Check if the error is a timeout error
269+
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
270+
fmt.Printf("Request timed out: %v\n", err)
271+
} else if uerr, ok := err.(*url.Error); ok && uerr.Timeout() {
272+
fmt.Printf("Request timed out (url.Error): %v\n", err)
273+
}
274+
return true
275+
}
276+
return false
277+
}

0 commit comments

Comments
 (0)