Skip to content

Commit c6acb91

Browse files
committed
Refactor: instance
1 parent b1baee0 commit c6acb91

File tree

2 files changed

+73
-21
lines changed

2 files changed

+73
-21
lines changed

lib/instance.go

Lines changed: 72 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,62 +4,101 @@ import (
44
"encoding/json"
55
"errors"
66
"os"
7+
"strings"
78

89
"github.com/tailscale/hujson"
910
)
1011

11-
type Instance struct {
12-
config *config
12+
type Instance interface {
13+
InitConfig(configFile string) error
14+
InitConfigFromBytes(content []byte) error
15+
AddInput(InputConverter)
16+
AddOutput(OutputConverter)
17+
ResetInput()
18+
ResetOutput()
19+
RunInput(Container) error
20+
RunOutput(Container) error
21+
Run() error
22+
}
23+
24+
type instance struct {
1325
input []InputConverter
1426
output []OutputConverter
1527
}
1628

17-
func NewInstance() (*Instance, error) {
18-
return &Instance{
19-
config: new(config),
29+
func NewInstance() (Instance, error) {
30+
return &instance{
2031
input: make([]InputConverter, 0),
2132
output: make([]OutputConverter, 0),
2233
}, nil
2334
}
2435

25-
func (i *Instance) Init(configFile string) error {
26-
content, err := os.ReadFile(configFile)
36+
func (i *instance) InitConfig(configFile string) error {
37+
var content []byte
38+
var err error
39+
configFile = strings.TrimSpace(configFile)
40+
if strings.HasPrefix(strings.ToLower(configFile), "http://") || strings.HasPrefix(strings.ToLower(configFile), "https://") {
41+
content, err = GetRemoteURLContent(configFile)
42+
} else {
43+
content, err = os.ReadFile(configFile)
44+
}
2745
if err != nil {
2846
return err
2947
}
3048

49+
return i.InitConfigFromBytes(content)
50+
}
51+
52+
func (i *instance) InitConfigFromBytes(content []byte) error {
53+
config := new(config)
54+
3155
// Support JSON with comments and trailing commas
3256
content, _ = hujson.Standardize(content)
3357

34-
if err := json.Unmarshal(content, &i.config); err != nil {
58+
if err := json.Unmarshal(content, &config); err != nil {
3559
return err
3660
}
3761

38-
for _, input := range i.config.Input {
62+
for _, input := range config.Input {
3963
i.input = append(i.input, input.converter)
4064
}
4165

42-
for _, output := range i.config.Output {
66+
for _, output := range config.Output {
4367
i.output = append(i.output, output.converter)
4468
}
4569

4670
return nil
4771
}
4872

49-
func (i *Instance) Run() error {
50-
if len(i.input) == 0 || len(i.output) == 0 {
51-
return errors.New("input type and output type must be specified")
52-
}
73+
func (i *instance) AddInput(ic InputConverter) {
74+
i.input = append(i.input, ic)
75+
}
5376

77+
func (i *instance) AddOutput(oc OutputConverter) {
78+
i.output = append(i.output, oc)
79+
}
80+
81+
func (i *instance) ResetInput() {
82+
i.input = make([]InputConverter, 0)
83+
}
84+
85+
func (i *instance) ResetOutput() {
86+
i.output = make([]OutputConverter, 0)
87+
}
88+
89+
func (i *instance) RunInput(container Container) error {
5490
var err error
55-
container := NewContainer()
5691
for _, ic := range i.input {
5792
container, err = ic.Input(container)
5893
if err != nil {
5994
return err
6095
}
6196
}
6297

98+
return nil
99+
}
100+
101+
func (i *instance) RunOutput(container Container) error {
63102
for _, oc := range i.output {
64103
if err := oc.Output(container); err != nil {
65104
return err
@@ -68,3 +107,21 @@ func (i *Instance) Run() error {
68107

69108
return nil
70109
}
110+
111+
func (i *instance) Run() error {
112+
if len(i.input) == 0 || len(i.output) == 0 {
113+
return errors.New("input type and output type must be specified")
114+
}
115+
116+
container := NewContainer()
117+
118+
if err := i.RunInput(container); err != nil {
119+
return err
120+
}
121+
122+
if err := i.RunOutput(container); err != nil {
123+
return err
124+
}
125+
126+
return nil
127+
}

main.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
// GeoIP generator
2-
//
3-
// Before running this file, the GeoIP database must be downloaded and present.
4-
// To download GeoIP database: https://dev.maxmind.com/geoip/geoip2/geolite2/
5-
// Inside you will find block files for IPv4 and IPv6 and country code mapping.
61
package main
72

83
import (
@@ -33,7 +28,7 @@ func main() {
3328
log.Fatal(err)
3429
}
3530

36-
if err := instance.Init(*configFile); err != nil {
31+
if err := instance.InitConfig(*configFile); err != nil {
3732
log.Fatal(err)
3833
}
3934

0 commit comments

Comments
 (0)