main.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  2. // All rights reserved. Use of this source code is governed by an MIT-style
  3. // license that can be found in the LICENSE file.
  4. package main
  5. import (
  6. "encoding/json"
  7. "flag"
  8. "fmt"
  9. "log"
  10. "net/http"
  11. "os"
  12. "time"
  13. )
  14. type event struct {
  15. ID int `json:"id"`
  16. Type string `json:"type"`
  17. Time time.Time `json:"time"`
  18. Data map[string]interface{} `json:"data"`
  19. }
  20. func main() {
  21. log.SetOutput(os.Stdout)
  22. log.SetFlags(0)
  23. target := flag.String("target", "localhost:8080", "Target Syncthing instance")
  24. apikey := flag.String("apikey", "", "Syncthing API key")
  25. flag.Parse()
  26. if *apikey == "" {
  27. log.Fatal("Must give -apikey argument")
  28. }
  29. since := 0
  30. for {
  31. req, err := http.NewRequest("GET", fmt.Sprintf("http://%s/rest/events?since=%d", *target, since), nil)
  32. if err != nil {
  33. log.Fatal(err)
  34. }
  35. req.Header.Set("X-API-Key", *apikey)
  36. res, err := http.DefaultClient.Do(req)
  37. if err != nil {
  38. log.Fatal(err)
  39. }
  40. var events []event
  41. err = json.NewDecoder(res.Body).Decode(&events)
  42. if err != nil {
  43. log.Fatal(err)
  44. }
  45. res.Body.Close()
  46. for _, event := range events {
  47. bs, _ := json.MarshalIndent(event, "", " ")
  48. log.Printf("%s", bs)
  49. since = event.ID
  50. }
  51. }
  52. }