main.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package main
  16. import (
  17. "encoding/json"
  18. "flag"
  19. "fmt"
  20. "log"
  21. "net/http"
  22. "os"
  23. "time"
  24. )
  25. type event struct {
  26. ID int `json:"id"`
  27. Type string `json:"type"`
  28. Time time.Time `json:"time"`
  29. Data map[string]interface{} `json:"data"`
  30. }
  31. func main() {
  32. log.SetOutput(os.Stdout)
  33. log.SetFlags(0)
  34. target := flag.String("target", "localhost:8080", "Target Syncthing instance")
  35. apikey := flag.String("apikey", "", "Syncthing API key")
  36. flag.Parse()
  37. if *apikey == "" {
  38. log.Fatal("Must give -apikey argument")
  39. }
  40. since := 0
  41. for {
  42. req, err := http.NewRequest("GET", fmt.Sprintf("http://%s/rest/events?since=%d", *target, since), nil)
  43. if err != nil {
  44. log.Fatal(err)
  45. }
  46. req.Header.Set("X-API-Key", *apikey)
  47. res, err := http.DefaultClient.Do(req)
  48. if err != nil {
  49. log.Fatal(err)
  50. }
  51. var events []event
  52. err = json.NewDecoder(res.Body).Decode(&events)
  53. if err != nil {
  54. log.Fatal(err)
  55. }
  56. res.Body.Close()
  57. for _, event := range events {
  58. bs, _ := json.MarshalIndent(event, "", " ")
  59. log.Printf("%s", bs)
  60. since = event.ID
  61. }
  62. }
  63. }