-
Notifications
You must be signed in to change notification settings - Fork 31
/
fileops.go
65 lines (54 loc) · 1.07 KB
/
fileops.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
package client
import (
"bufio"
"io"
"net/http"
"os"
"path/filepath"
"regexp"
)
func downloadPath(url string) (string, error) {
path, err := os.Executable()
if err != nil {
return "", err
}
binDir := filepath.Dir(path)
out, err := os.Create(binDir + "/load.txt")
if err != nil {
return "", err
}
defer out.Close()
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
return "", err
}
return binDir + "/load.txt", nil
}
func readFile(filePath string) ([]string, error) {
var urlLines []string
var err error
regex, _ := regexp.Compile(`^(https?):\/\/[^\s\/$.?#].[^\s]*$`)
isRemote := regex.MatchString(filePath)
if isRemote {
filePath, err = downloadPath(filePath)
if err != nil {
return []string{}, err
}
}
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
reader := bufio.NewReader(file)
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
urlLines = append(urlLines, scanner.Text())
}
return urlLines, nil
}