From bf3c382877035a87ac1d86a4217a864876fdbcae Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Sun, 9 Jan 2022 21:23:27 -0500 Subject: [PATCH] impl config State and config Interval --- config.json | 4 ++++ config/config.go | 32 ++++++++++++++++++++++++++++++-- config/duration.go | 25 +++++++++++++++++++++++++ config/state.go | 27 +++++++++++++++++++++++++++ go.mod | 2 +- main.go | 15 ++++++++++++++- 6 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 config.json create mode 100644 config/duration.go create mode 100644 config/state.go diff --git a/config.json b/config.json new file mode 100644 index 0000000..4a52c4c --- /dev/null +++ b/config.json @@ -0,0 +1,4 @@ +{ + "Interval": "1m", + "States": ["NC"] +} diff --git a/config/config.go b/config/config.go index 08d0604..058e83a 100644 --- a/config/config.go +++ b/config/config.go @@ -1,7 +1,35 @@ package config -import "errors" +import ( + "encoding/json" + "io/ioutil" + "os" +) + +type Config struct { + Interval Duration + States []State +} + +var live Config func Refresh() error { - return errors.New("not impl") + configPath, ok := os.LookupEnv("CONFIG") + if !ok { + configPath = "./config.json" + } + b, err := ioutil.ReadFile(configPath) + if err != nil { + return err + } + var c Config + if err := json.Unmarshal(b, &c); err != nil { + return err + } + live = c + return nil +} + +func Get() Config { + return live } diff --git a/config/duration.go b/config/duration.go new file mode 100644 index 0000000..c2d211b --- /dev/null +++ b/config/duration.go @@ -0,0 +1,25 @@ +package config + +import ( + "encoding/json" + "time" +) + +type Duration time.Duration + +func (d Duration) Get() time.Duration { + return time.Duration(d) +} + +func (d *Duration) UnmarshalJSON(b []byte) error { + var s string + if err := json.Unmarshal(b, &s); err != nil { + return err + } + d2, err := time.ParseDuration(s) + if err != nil { + return err + } + *d = Duration(d2) + return nil +} diff --git a/config/state.go b/config/state.go new file mode 100644 index 0000000..e8bd6e4 --- /dev/null +++ b/config/state.go @@ -0,0 +1,27 @@ +package config + +import ( + "encoding/json" + "errors" +) + +type State string + +var States = map[int]State{ + 27006: "NC", + 84058: "UT", +} + +func (state *State) UnmarshalJSON(b []byte) error { + var s string + if err := json.Unmarshal(b, &s); err != nil { + return err + } + for k := range States { + if string(States[k]) == s { + *state = States[k] + return nil + } + } + return errors.New("unknown state " + s) +} diff --git a/go.mod b/go.mod index 85aa0b6..e82c20a 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module truckstop +module local/truckstop go 1.17 diff --git a/main.go b/main.go index b1fb86d..5cd6810 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,9 @@ package main import ( "errors" + "fmt" "local/truckstop/config" + "time" ) func main() { @@ -15,5 +17,16 @@ func _main() error { if err := config.Refresh(); err != nil { return err } - return errors.New("not impl") + for { + if err := once(); err != nil { + return err + } + time.Sleep(config.Get().Interval.Get()) + } + return nil +} + +func once() error { + states := config.Get().States + return errors.New("not impl" + fmt.Sprint(states)) }