main.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. _ "github.com/syncthing/syncthing/lib/automaxprocs"
  16. )
  17. type event struct {
  18. ID int `json:"id"`
  19. Type string `json:"type"`
  20. Time time.Time `json:"time"`
  21. Data map[string]interface{} `json:"data"`
  22. }
  23. func main() {
  24. log.SetOutput(os.Stdout)
  25. log.SetFlags(0)
  26. target := flag.String("target", "localhost:8384", "Target Syncthing instance")
  27. types := flag.String("types", "", "Filter for specific event types (comma-separated)")
  28. apikey := flag.String("apikey", "", "Syncthing API key")
  29. flag.Parse()
  30. if *apikey == "" {
  31. log.Fatal("Must give -apikey argument")
  32. }
  33. var eventsArg string
  34. if len(*types) > 0 {
  35. eventsArg = "&events=" + *types
  36. }
  37. since := 0
  38. for {
  39. req, err := http.NewRequest("GET", fmt.Sprintf("http://%s/rest/events?since=%d%s", *target, since, eventsArg), nil)
  40. if err != nil {
  41. log.Fatal(err)
  42. }
  43. req.Header.Set("X-API-Key", *apikey)
  44. res, err := http.DefaultClient.Do(req)
  45. if err != nil {
  46. log.Fatal(err)
  47. }
  48. var events []event
  49. err = json.NewDecoder(res.Body).Decode(&events)
  50. if err != nil {
  51. log.Fatal(err)
  52. }
  53. res.Body.Close()
  54. for _, event := range events {
  55. bs, _ := json.MarshalIndent(event, "", " ")
  56. log.Printf("%s", bs)
  57. since = event.ID
  58. }
  59. }
  60. }