-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.go
156 lines (129 loc) · 2.98 KB
/
config.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"bufio"
"log"
"os"
"strconv"
"strings"
"github.com/go-errors/errors"
)
const (
commentPrefix = "#"
)
type Config struct {
Port int
LinkPrefix string
FileDir string
LinkLength int
ScrubExif bool
ExifAllowedIds []uint16
ExifAllowedPaths []string
ExifAbortOnError bool
}
func ConfigFromFile(filePath string) (*Config, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
oldPrefix := log.Prefix()
defer log.SetPrefix(oldPrefix)
log.SetPrefix("config.FromFile > ")
retval := &Config{
Port: 4711,
LinkPrefix: "https://jaf.example.com/",
FileDir: "/var/www/jaf/",
LinkLength: 5,
ScrubExif: true,
ExifAllowedIds: []uint16{},
ExifAllowedPaths: []string{},
ExifAbortOnError: true,
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if strings.HasPrefix(line, commentPrefix) {
// Skip comments
continue
}
key, val, found := strings.Cut(line, ":")
if !found {
log.Printf("unexpected line: \"%s\", ignoring\n", line)
continue
}
key = strings.TrimSpace(key)
val = strings.TrimSpace(val)
switch key {
case "Port":
parsed, err := strconv.Atoi(val)
if err != nil {
return nil, err
}
retval.Port = parsed
case "LinkPrefix":
retval.LinkPrefix = val
case "FileDir":
retval.FileDir = val
case "LinkLength":
parsed, err := strconv.Atoi(val)
if err != nil {
return nil, err
}
retval.LinkLength = parsed
case "ScrubExif":
parsed, err := strconv.ParseBool(val)
if err != nil {
return nil, err
}
retval.ScrubExif = parsed
case "ExifAllowedIds":
if val == "" {
// No IDs specified at all
break
}
stringIds := strings.Split(val, " ")
parsedIds := make([]uint16, 0, len(stringIds))
for _, stringId := range stringIds {
var parsed uint64
var err error
if strings.HasPrefix(stringId, "0x") {
// Parse as a hexadecimal number
hexStringId := strings.Replace(stringId, "0x", "", 1)
parsed, err = strconv.ParseUint(hexStringId, 16, 16)
} else {
// Parse as a decimal number
parsed, err = strconv.ParseUint(stringId, 10, 16)
}
if err != nil {
log.Printf(
"Could not parse ID from: \"%s\", ignoring. Error: %s\n",
stringId,
err,
)
continue
}
parsedIds = append(parsedIds, uint16(parsed))
}
retval.ExifAllowedIds = parsedIds
case "ExifAllowedPaths":
if val == "" {
// No paths specified at all
break
}
paths := strings.Split(val, " ")
retval.ExifAllowedPaths = paths
case "ExifAbortOnError":
parsed, err := strconv.ParseBool(val)
if err != nil {
return nil, err
}
retval.ExifAbortOnError = parsed
default:
return nil, errors.Errorf("unexpected config key: \"%s\"", key)
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
return retval, nil
}