2014-11-16 20:13:20 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 19:43:32 +00:00
|
|
|
//
|
2015-03-07 20:36:35 +00:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-07-31 10:30:19 +00:00
|
|
|
|
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)
|
|
|
|
|
2015-03-26 20:36:06 +00:00
|
|
|
target := flag.String("target", "localhost:8384", "Target Syncthing instance")
|
2020-12-22 21:10:26 +00:00
|
|
|
types := flag.String("types", "", "Filter for specific event types (comma-separated)")
|
2014-07-13 19:07:24 +00:00
|
|
|
apikey := flag.String("apikey", "", "Syncthing API key")
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if *apikey == "" {
|
|
|
|
log.Fatal("Must give -apikey argument")
|
|
|
|
}
|
2020-12-22 21:10:26 +00:00
|
|
|
var eventsArg string
|
|
|
|
if len(*types) > 0 {
|
|
|
|
eventsArg = "&events=" + *types
|
|
|
|
}
|
2014-07-13 19:07:24 +00:00
|
|
|
|
|
|
|
since := 0
|
|
|
|
for {
|
2020-12-22 21:10:26 +00:00
|
|
|
req, err := http.NewRequest("GET", fmt.Sprintf("http://%s/rest/events?since=%d%s", *target, since, eventsArg), nil)
|
2014-07-13 19:07:24 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|