can pull states from email and fill config file
This commit is contained in:
41
main.go
41
main.go
@@ -1,14 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"local/storage"
|
||||
"local/truckstop/broker"
|
||||
"local/truckstop/config"
|
||||
"log"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var stateFinder = regexp.MustCompile(`[ ^>][A-Z][A-Z][ $<]`)
|
||||
|
||||
func main() {
|
||||
go func() {
|
||||
email()
|
||||
@@ -22,6 +29,7 @@ func main() {
|
||||
if err := _main(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
time.Sleep(time.Minute)
|
||||
}
|
||||
|
||||
func email() error {
|
||||
@@ -29,10 +37,37 @@ func email() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
states := map[config.State]struct{}{}
|
||||
var state config.State
|
||||
for email := range ch {
|
||||
log.Printf("%+v", email)
|
||||
if len(states) > 0 {
|
||||
continue
|
||||
}
|
||||
if !strings.Contains(email.Header.Get("From"), config.Get().Client) {
|
||||
continue
|
||||
}
|
||||
b, _ := ioutil.ReadAll(email.Body)
|
||||
candidates := stateFinder.FindAll(b, -1)
|
||||
for _, candidate := range candidates {
|
||||
s := fmt.Sprintf(`"%s"`, bytes.TrimSpace(candidate))
|
||||
err := json.Unmarshal([]byte(s), &state)
|
||||
if err != nil {
|
||||
} else {
|
||||
states[state] = struct{}{}
|
||||
}
|
||||
}
|
||||
}
|
||||
return errors.New("not impl")
|
||||
if len(states) == 0 {
|
||||
return nil
|
||||
}
|
||||
conf := *config.Get()
|
||||
conf.States = []config.State{}
|
||||
for k := range states {
|
||||
conf.States = append(conf.States, k)
|
||||
}
|
||||
log.Printf("%+v, %+v", states, conf)
|
||||
config.Set(conf)
|
||||
return nil
|
||||
}
|
||||
|
||||
func _main() error {
|
||||
|
||||
Reference in New Issue
Block a user