main.go 1.28 KB
Newer Older
1 2 3 4
// Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
// All rights reserved. Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.

Jakob Borg's avatar
Jakob Borg committed
5 6 7 8 9 10 11 12
package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"log"
	"net/http"
Jakob Borg's avatar
Jakob Borg committed
13
	"os"
Jakob Borg's avatar
Jakob Borg committed
14 15 16 17
	"time"
)

type event struct {
Jakob Borg's avatar
Jakob Borg committed
18 19 20 21
	ID   int                    `json:"id"`
	Type string                 `json:"type"`
	Time time.Time              `json:"time"`
	Data map[string]interface{} `json:"data"`
Jakob Borg's avatar
Jakob Borg committed
22 23 24
}

func main() {
Jakob Borg's avatar
Jakob Borg committed
25 26 27
	log.SetOutput(os.Stdout)
	log.SetFlags(0)

28 29
	target := flag.String("target", "localhost:8080", "Target Pulse instance")
	apikey := flag.String("apikey", "", "Pulse API key")
Jakob Borg's avatar
Jakob Borg committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
	flag.Parse()

	if *apikey == "" {
		log.Fatal("Must give -apikey argument")
	}

	since := 0
	for {
		req, err := http.NewRequest("GET", fmt.Sprintf("http://%s/rest/events?since=%d", *target, since), nil)
		if err != nil {
			log.Fatal(err)
		}
		req.Header.Set("X-API-Key", *apikey)
		res, err := http.DefaultClient.Do(req)
		if err != nil {
			log.Fatal(err)
		}

		var events []event
		err = json.NewDecoder(res.Body).Decode(&events)
		if err != nil {
			log.Fatal(err)
		}
Jakob Borg's avatar
Jakob Borg committed
53
		res.Body.Close()
Jakob Borg's avatar
Jakob Borg committed
54 55

		for _, event := range events {
Jakob Borg's avatar
Jakob Borg committed
56 57
			bs, _ := json.MarshalIndent(event, "", "    ")
			log.Printf("%s", bs)
Jakob Borg's avatar
Jakob Borg committed
58 59 60 61
			since = event.ID
		}
	}
}