-
Notifications
You must be signed in to change notification settings - Fork 0
/
vote.go
52 lines (43 loc) · 1.3 KB
/
vote.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package cmd
import (
"bytes"
"fmt"
"net/http"
"time"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var baseUrl string
var ballot string
var electionId string
var voteCmd = &cobra.Command{
Use: "vote",
Short: "Cast a ballot for an on-going election",
Run: func(cmd *cobra.Command, args []string) {
requestUrl := fmt.Sprintf("http://%s:%s/%s", baseUrl, viper.GetString("port"), electionId)
data := []byte(ballot)
req, err := http.NewRequest("POST", requestUrl, bytes.NewBuffer(data))
if err != nil {
log.WithFields(log.Fields{"err": err}).Error("Bad request")
log.WithFields(log.Fields{
"ballot": ballot,
"url": requestUrl,
"electionId": electionId,
}).Fatal("Bad request")
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{Timeout: time.Second * 10}
resp, err := client.Do(req)
if err != nil {
log.WithFields(log.Fields{"err": err}).Fatal("Bad request")
}
defer resp.Body.Close()
},
}
func init() {
rootCmd.AddCommand(voteCmd)
voteCmd.Flags().StringVar(&ballot, "ballot", "", "A json string representing a ballot")
voteCmd.Flags().StringVar(&baseUrl, "url", "", "The url of the election server")
voteCmd.Flags().StringVar(&electionId, "electionId", "", "The id of the election")
}