forked from Loyalsoldier/geoip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
func.go
59 lines (50 loc) · 1.26 KB
/
func.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
package lib
import (
"fmt"
"io"
"net/http"
"strings"
)
var (
inputConverterMap = make(map[string]InputConverter)
outputConverterMap = make(map[string]OutputConverter)
)
func ListInputConverter() {
fmt.Println("All available input formats:")
for name, ic := range inputConverterMap {
fmt.Printf(" - %s (%s)\n", name, ic.GetDescription())
}
}
func RegisterInputConverter(name string, c InputConverter) error {
name = strings.TrimSpace(name)
if _, ok := inputConverterMap[name]; ok {
return ErrDuplicatedConverter
}
inputConverterMap[name] = c
return nil
}
func ListOutputConverter() {
fmt.Println("All available output formats:")
for name, oc := range outputConverterMap {
fmt.Printf(" - %s (%s)\n", name, oc.GetDescription())
}
}
func RegisterOutputConverter(name string, c OutputConverter) error {
name = strings.TrimSpace(name)
if _, ok := outputConverterMap[name]; ok {
return ErrDuplicatedConverter
}
outputConverterMap[name] = c
return nil
}
func getRemoteURLContent(url string) ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to get remote content -> %s: %s", url, resp.Status)
}
return io.ReadAll(resp.Body)
}