-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
executable file
·47 lines (41 loc) · 940 Bytes
/
config.go
File metadata and controls
executable file
·47 lines (41 loc) · 940 Bytes
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
package main
import (
"bufio"
"fmt"
"net/url"
"os"
"strings"
)
type pair struct {
Prefix string
Target *url.URL
}
// parseConfig parses the configuration file. Its format is:
//
// /prefix http://proxy.target.host/base
//
// Empty lines and lines beginning with a "#" sign are ignored
func parseConfig(config string) ([]pair, error) {
file, err := os.Open(config)
if err != nil {
return nil, fmt.Errorf("Could not open configuration: %v\n", err)
}
defer file.Close()
pairs := make([]pair, 0)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "#") {
continue
}
tokens := strings.Split(strings.Trim(line, " "), " ")
if len(tokens) == 2 {
url, err := url.Parse(tokens[1])
if err != nil {
return nil, fmt.Errorf("Malformed URL %s: %v\n", tokens[1], err)
}
pairs = append(pairs, pair{tokens[0], url})
}
}
return pairs, scanner.Err()
}