Skip to content

Commit 9ac8851

Browse files
committed
check the remote and add records
1 parent 8479678 commit 9ac8851

File tree

3 files changed

+70
-12
lines changed

3 files changed

+70
-12
lines changed

internal/cfg/cfg.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ type Config struct {
6161

6262
// SyncConnectorAddress used by the daemon to connect to the Sync Connector
6363
// examples:
64-
// - localhost:1234
6564
// - http://localhost:1234
66-
// - 192.168.1.1:1324
65+
// - http://localhost:1234
66+
// - http://192.168.1.1:1324
6767
// - https://domain.tld
6868
// - https://domain.tld/resh
6969
SyncConnectorAddress *string
@@ -130,6 +130,8 @@ const headerComment = `##
130130
131131
`
132132

133+
// TODO: Add description for the new options.
134+
133135
func getConfigPath() (string, error) {
134136
fname := "resh.toml"
135137
xdgDir, found := os.LookupEnv("XDG_CONFIG_HOME")

internal/syncconnector/reader.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,53 @@
11
package syncconnector
22

3-
import "github.com/curusarn/resh/internal/record"
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"github.com/curusarn/resh/internal/record"
7+
"io"
8+
"io/ioutil"
9+
"log"
10+
"net/http"
11+
"time"
12+
)
413

514
func (sc SyncConnector) getLatestRecord(machineId *string) (map[string]string, error) {
615
return map[string]string{}, nil
716
}
817

918
func (sc SyncConnector) downloadRecords(lastRecords map[string]string) ([]record.V1, error) {
1019
var records []record.V1
20+
21+
client := http.Client{
22+
Timeout: 3 * time.Second,
23+
}
24+
25+
// TODO: create request based on the local last records
26+
responseBody := bytes.NewBuffer([]byte("{}"))
27+
28+
address := sc.getAddressWithPath(historyEndpoint)
29+
resp, err := client.Post(address, "application/json", responseBody)
30+
if err != nil {
31+
sc.sugar.Errorw("history request failed", "address", address, "err", err)
32+
return nil, err
33+
}
34+
35+
defer func(Body io.ReadCloser) {
36+
err := Body.Close()
37+
if err != nil {
38+
sc.sugar.Errorw("reader close failed", "err", err)
39+
}
40+
}(resp.Body)
41+
body, err := ioutil.ReadAll(resp.Body)
42+
if err != nil {
43+
log.Fatalln(err)
44+
}
45+
46+
err = json.Unmarshal(body, &records)
47+
if err != nil {
48+
sc.sugar.Errorw("Unmarshalling failed", "err", err)
49+
return nil, err
50+
}
51+
1152
return records, nil
1253
}

internal/syncconnector/syncconnector.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ package syncconnector
22

33
import (
44
"github.com/curusarn/resh/internal/histcli"
5-
"github.com/curusarn/resh/internal/record"
65
"github.com/curusarn/resh/internal/recordint"
76
"go.uber.org/zap"
87
"net/url"
8+
"path"
99
"time"
1010
)
1111

12+
const storeEndpoint = "/store"
13+
const historyEndpoint = "/history"
14+
const latestEndpoint = "/latest"
15+
1216
type SyncConnector struct {
1317
sugar *zap.SugaredLogger
1418

@@ -37,18 +41,29 @@ func New(sugar *zap.SugaredLogger, address string, authToken string, pullPeriodS
3741
// TODO: propagate signals
3842
go func(sc *SyncConnector) {
3943
for _ = range time.Tick(time.Second * time.Duration(pullPeriodSeconds)) {
40-
sc.sugar.Infow("checking remote")
44+
sc.sugar.Debug("checking remote")
45+
46+
recs, err := sc.downloadRecords(map[string]string{})
47+
if err != nil {
48+
continue
49+
}
50+
51+
sc.sugar.Debugf("Got %d records", len(recs))
4152

42-
// Add fake record (this will be produced by the sync connector)
43-
sc.history.AddRecord(&recordint.Indexed{
44-
Rec: record.V1{
45-
CmdLine: "__fake_test__",
46-
DeviceID: "__test__",
47-
},
48-
})
53+
for _, rec := range recs {
54+
sc.history.AddRecord(&recordint.Indexed{
55+
Rec: rec,
56+
})
57+
}
4958

5059
}
5160
}(sc)
5261

5362
return sc, nil
5463
}
64+
65+
func (sc SyncConnector) getAddressWithPath(endpoint string) string {
66+
address := *sc.address
67+
address.Path = path.Join(address.Path, endpoint)
68+
return address.String()
69+
}

0 commit comments

Comments
 (0)