check the remote and add records

pull/179/head
Vit Listik 3 years ago
parent ad4784de7d
commit 0f349315fe
No known key found for this signature in database
GPG Key ID: DDDF5138DA46D76B
  1. 6
      internal/cfg/cfg.go
  2. 43
      internal/syncconnector/reader.go
  3. 33
      internal/syncconnector/syncconnector.go

@ -61,9 +61,9 @@ type Config struct {
// SyncConnectorAddress used by the daemon to connect to the Sync Connector // SyncConnectorAddress used by the daemon to connect to the Sync Connector
// examples: // examples:
// - localhost:1234
// - http://localhost:1234 // - http://localhost:1234
// - 192.168.1.1:1324 // - http://localhost:1234
// - http://192.168.1.1:1324
// - https://domain.tld // - https://domain.tld
// - https://domain.tld/resh // - https://domain.tld/resh
SyncConnectorAddress *string SyncConnectorAddress *string
@ -130,6 +130,8 @@ const headerComment = `##
` `
// TODO: Add description for the new options.
func getConfigPath() (string, error) { func getConfigPath() (string, error) {
fname := "resh.toml" fname := "resh.toml"
xdgDir, found := os.LookupEnv("XDG_CONFIG_HOME") xdgDir, found := os.LookupEnv("XDG_CONFIG_HOME")

@ -1,6 +1,15 @@
package syncconnector package syncconnector
import "github.com/curusarn/resh/internal/record" import (
"bytes"
"encoding/json"
"github.com/curusarn/resh/internal/record"
"io"
"io/ioutil"
"log"
"net/http"
"time"
)
func (sc SyncConnector) getLatestRecord(machineId *string) (map[string]string, error) { func (sc SyncConnector) getLatestRecord(machineId *string) (map[string]string, error) {
return map[string]string{}, nil return map[string]string{}, nil
@ -8,5 +17,37 @@ func (sc SyncConnector) getLatestRecord(machineId *string) (map[string]string, e
func (sc SyncConnector) downloadRecords(lastRecords map[string]string) ([]record.V1, error) { func (sc SyncConnector) downloadRecords(lastRecords map[string]string) ([]record.V1, error) {
var records []record.V1 var records []record.V1
client := http.Client{
Timeout: 3 * time.Second,
}
// TODO: create request based on the local last records
responseBody := bytes.NewBuffer([]byte("{}"))
address := sc.getAddressWithPath(historyEndpoint)
resp, err := client.Post(address, "application/json", responseBody)
if err != nil {
sc.sugar.Errorw("history request failed", "address", address, "err", err)
return nil, err
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
sc.sugar.Errorw("reader close failed", "err", err)
}
}(resp.Body)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
err = json.Unmarshal(body, &records)
if err != nil {
sc.sugar.Errorw("Unmarshalling failed", "err", err)
return nil, err
}
return records, nil return records, nil
} }

@ -2,13 +2,17 @@ package syncconnector
import ( import (
"github.com/curusarn/resh/internal/histcli" "github.com/curusarn/resh/internal/histcli"
"github.com/curusarn/resh/internal/record"
"github.com/curusarn/resh/internal/recordint" "github.com/curusarn/resh/internal/recordint"
"go.uber.org/zap" "go.uber.org/zap"
"net/url" "net/url"
"path"
"time" "time"
) )
const storeEndpoint = "/store"
const historyEndpoint = "/history"
const latestEndpoint = "/latest"
type SyncConnector struct { type SyncConnector struct {
sugar *zap.SugaredLogger sugar *zap.SugaredLogger
@ -37,18 +41,29 @@ func New(sugar *zap.SugaredLogger, address string, authToken string, pullPeriodS
// TODO: propagate signals // TODO: propagate signals
go func(sc *SyncConnector) { go func(sc *SyncConnector) {
for _ = range time.Tick(time.Second * time.Duration(pullPeriodSeconds)) { for _ = range time.Tick(time.Second * time.Duration(pullPeriodSeconds)) {
sc.sugar.Infow("checking remote") sc.sugar.Debug("checking remote")
recs, err := sc.downloadRecords(map[string]string{})
if err != nil {
continue
}
sc.sugar.Debugf("Got %d records", len(recs))
// Add fake record (this will be produced by the sync connector) for _, rec := range recs {
sc.history.AddRecord(&recordint.Indexed{ sc.history.AddRecord(&recordint.Indexed{
Rec: record.V1{ Rec: rec,
CmdLine: "__fake_test__", })
DeviceID: "__test__", }
},
})
} }
}(sc) }(sc)
return sc, nil return sc, nil
} }
func (sc SyncConnector) getAddressWithPath(endpoint string) string {
address := *sc.address
address.Path = path.Join(address.Path, endpoint)
return address.String()
}

Loading…
Cancel
Save