syncthing/cmd/stevents/main.go

62 lines
1.3 KiB
Go
Raw Normal View History

// Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
// All rights reserved. Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
2014-07-13 19:07:24 +00:00
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
2014-07-13 19:39:35 +00:00
"os"
2014-07-13 19:07:24 +00:00
"time"
)
type event struct {
2014-07-13 19:39:35 +00:00
ID int `json:"id"`
Type string `json:"type"`
Time time.Time `json:"time"`
Data map[string]interface{} `json:"data"`
2014-07-13 19:07:24 +00:00
}
func main() {
2014-07-13 19:39:35 +00:00
log.SetOutput(os.Stdout)
log.SetFlags(0)
2014-07-13 19:07:24 +00:00
target := flag.String("target", "localhost:8080", "Target Syncthing instance")
apikey := flag.String("apikey", "", "Syncthing API key")
flag.Parse()
if *apikey == "" {
log.Fatal("Must give -apikey argument")
}
since := 0
for {
req, err := http.NewRequest("GET", fmt.Sprintf("http://%s/rest/events?since=%d", *target, since), nil)
if err != nil {
log.Fatal(err)
}
req.Header.Set("X-API-Key", *apikey)
res, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
var events []event
err = json.NewDecoder(res.Body).Decode(&events)
if err != nil {
log.Fatal(err)
}
2014-07-13 19:39:35 +00:00
res.Body.Close()
2014-07-13 19:07:24 +00:00
for _, event := range events {
2014-07-13 19:39:35 +00:00
bs, _ := json.MarshalIndent(event, "", " ")
log.Printf("%s", bs)
2014-07-13 19:07:24 +00:00
since = event.ID
}
}
}