summaryrefslogtreecommitdiffstats
path: root/lib/syncthing/st.go
diff options
context:
space:
mode:
authorSebastien Douheret <sebastien.douheret@iot.bzh>2017-05-11 19:42:00 +0200
committerSebastien Douheret <sebastien.douheret@iot.bzh>2017-05-11 19:42:22 +0200
commitec7051e1da665206f594c7616ad381bfeaea333a (patch)
treeecc01ee358794c9d8c5fbb87d2f5b6ce3f60f431 /lib/syncthing/st.go
parentca3e1762832b27dc25cf90125b376c56e24e2db2 (diff)
Initial main commit.
Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
Diffstat (limited to 'lib/syncthing/st.go')
-rw-r--r--lib/syncthing/st.go76
1 files changed, 76 insertions, 0 deletions
diff --git a/lib/syncthing/st.go b/lib/syncthing/st.go
new file mode 100644
index 0000000..7d07b70
--- /dev/null
+++ b/lib/syncthing/st.go
@@ -0,0 +1,76 @@
+package st
+
+import (
+ "encoding/json"
+
+ "strings"
+
+ "fmt"
+
+ "github.com/Sirupsen/logrus"
+ "github.com/iotbzh/xds-server/lib/common"
+ "github.com/syncthing/syncthing/lib/config"
+)
+
+// SyncThing .
+type SyncThing struct {
+ BaseURL string
+ client *common.HTTPClient
+ log *logrus.Logger
+}
+
+// NewSyncThing creates a new instance of Syncthing
+func NewSyncThing(url string, apikey string, log *logrus.Logger) *SyncThing {
+ cl, err := common.HTTPNewClient(url,
+ common.HTTPClientConfig{
+ URLPrefix: "/rest",
+ HeaderClientKeyName: "X-Syncthing-ID",
+ })
+ if err != nil {
+ msg := ": " + err.Error()
+ if strings.Contains(err.Error(), "connection refused") {
+ msg = fmt.Sprintf("(url: %s)", url)
+ }
+ log.Debugf("ERROR: cannot connect to Syncthing %s", msg)
+ return nil
+ }
+
+ s := SyncThing{
+ BaseURL: url,
+ client: cl,
+ log: log,
+ }
+
+ return &s
+}
+
+// IDGet returns the Syncthing ID of Syncthing instance running locally
+func (s *SyncThing) IDGet() (string, error) {
+ var data []byte
+ if err := s.client.HTTPGet("system/status", &data); err != nil {
+ return "", err
+ }
+ status := make(map[string]interface{})
+ json.Unmarshal(data, &status)
+ return status["myID"].(string), nil
+}
+
+// ConfigGet returns the current Syncthing configuration
+func (s *SyncThing) ConfigGet() (config.Configuration, error) {
+ var data []byte
+ config := config.Configuration{}
+ if err := s.client.HTTPGet("system/config", &data); err != nil {
+ return config, err
+ }
+ err := json.Unmarshal(data, &config)
+ return config, err
+}
+
+// ConfigSet set Syncthing configuration
+func (s *SyncThing) ConfigSet(cfg config.Configuration) error {
+ body, err := json.Marshal(cfg)
+ if err != nil {
+ return err
+ }
+ return s.client.HTTPPost("system/config", string(body))
+}