main.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 https://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. types := flag.String("types", "", "Filter for specific event types (comma-separated)")
  27. apikey := flag.String("apikey", "", "Syncthing API key")
  28. flag.Parse()
  29. if *apikey == "" {
  30. log.Fatal("Must give -apikey argument")
  31. }
  32. var eventsArg string
  33. if len(*types) > 0 {
  34. eventsArg = "&events=" + *types
  35. }
  36. since := 0
  37. for {
  38. req, err := http.NewRequest("GET", fmt.Sprintf("http://%s/rest/events?since=%d%s", *target, since, eventsArg), nil)
  39. if err != nil {
  40. log.Fatal(err)
  41. }
  42. req.Header.Set("X-API-Key", *apikey)
  43. res, err := http.DefaultClient.Do(req)
  44. if err != nil {
  45. log.Fatal(err)
  46. }
  47. var events []event
  48. err = json.NewDecoder(res.Body).Decode(&events)
  49. if err != nil {
  50. log.Fatal(err)
  51. }
  52. res.Body.Close()
  53. for _, event := range events {
  54. bs, _ := json.MarshalIndent(event, "", " ")
  55. log.Printf("%s", bs)
  56. since = event.ID
  57. }
  58. }
  59. }