From 0f349315fe9417f9b8acd0c100796c901d73282f Mon Sep 17 00:00:00 2001 From: Vit Listik Date: Wed, 2 Nov 2022 00:30:39 +0100 Subject: [PATCH] check the remote and add records --- internal/cfg/cfg.go | 6 ++-- internal/syncconnector/reader.go | 43 ++++++++++++++++++++++++- internal/syncconnector/syncconnector.go | 33 +++++++++++++------ 3 files changed, 70 insertions(+), 12 deletions(-) diff --git a/internal/cfg/cfg.go b/internal/cfg/cfg.go index e9a47cb..6193320 100644 --- a/internal/cfg/cfg.go +++ b/internal/cfg/cfg.go @@ -61,9 +61,9 @@ type Config struct { // SyncConnectorAddress used by the daemon to connect to the Sync Connector // examples: - // - 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/resh SyncConnectorAddress *string @@ -130,6 +130,8 @@ const headerComment = `## ` +// TODO: Add description for the new options. + func getConfigPath() (string, error) { fname := "resh.toml" xdgDir, found := os.LookupEnv("XDG_CONFIG_HOME") diff --git a/internal/syncconnector/reader.go b/internal/syncconnector/reader.go index 6c0cc9f..f278189 100644 --- a/internal/syncconnector/reader.go +++ b/internal/syncconnector/reader.go @@ -1,6 +1,15 @@ 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) { 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) { 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 } diff --git a/internal/syncconnector/syncconnector.go b/internal/syncconnector/syncconnector.go index f3ae344..3d55962 100644 --- a/internal/syncconnector/syncconnector.go +++ b/internal/syncconnector/syncconnector.go @@ -2,13 +2,17 @@ package syncconnector import ( "github.com/curusarn/resh/internal/histcli" - "github.com/curusarn/resh/internal/record" "github.com/curusarn/resh/internal/recordint" "go.uber.org/zap" "net/url" + "path" "time" ) +const storeEndpoint = "/store" +const historyEndpoint = "/history" +const latestEndpoint = "/latest" + type SyncConnector struct { sugar *zap.SugaredLogger @@ -37,18 +41,29 @@ func New(sugar *zap.SugaredLogger, address string, authToken string, pullPeriodS // TODO: propagate signals go func(sc *SyncConnector) { 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) - sc.history.AddRecord(&recordint.Indexed{ - Rec: record.V1{ - CmdLine: "__fake_test__", - DeviceID: "__test__", - }, - }) + for _, rec := range recs { + sc.history.AddRecord(&recordint.Indexed{ + Rec: rec, + }) + } } }(sc) return sc, nil } + +func (sc SyncConnector) getAddressWithPath(endpoint string) string { + address := *sc.address + address.Path = path.Join(address.Path, endpoint) + return address.String() +}