main.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. package main
  7. import (
  8. "encoding/json"
  9. "flag"
  10. "fmt"
  11. "log"
  12. "net/http"
  13. "os"
  14. "time"
  15. )
  16. type event struct {
  17. ID int `json:"id"`
  18. Type string `json:"type"`
  19. Time time.Time `json:"time"`
  20. Data map[string]interface{} `json:"data"`
  21. }
  22. func main() {
  23. log.SetOutput(os.Stdout)
  24. log.SetFlags(0)
  25. target := flag.String("target", "localhost:8384", "Target Syncthing instance")
  26. apikey := flag.String("apikey", "", "Syncthing API key")
  27. flag.Parse()
  28. if *apikey == "" {
  29. log.Fatal("Must give -apikey argument")
  30. }
  31. since := 0
  32. for {
  33. req, err := http.NewRequest("GET", fmt.Sprintf("http://%s/rest/events?since=%d", *target, since), nil)
  34. if err != nil {
  35. log.Fatal(err)
  36. }
  37. req.Header.Set("X-API-Key", *apikey)
  38. res, err := http.DefaultClient.Do(req)
  39. if err != nil {
  40. log.Fatal(err)
  41. }
  42. var events []event
  43. err = json.NewDecoder(res.Body).Decode(&events)
  44. if err != nil {
  45. log.Fatal(err)
  46. }
  47. res.Body.Close()
  48. for _, event := range events {
  49. bs, _ := json.MarshalIndent(event, "", " ")
  50. log.Printf("%s", bs)
  51. since = event.ID
  52. }
  53. }
  54. }