diff options
author | Joe Robinson <joe@mumsnet.com> | 2014-07-25 12:53:27 +0100 |
---|---|---|
committer | Joe Robinson <joe@mumsnet.com> | 2014-07-25 12:53:27 +0100 |
commit | a471e978989fecb533bcf8666772a8a425215346 (patch) | |
tree | 33b95702e73a377f65fb74bb68459ac0cf48d33e /blavote.go | |
parent | b91bba1ebb7e62ed0c06c02580b18a10fc7326db (diff) |
Added function to output poll info/votes
Diffstat (limited to 'blavote.go')
-rw-r--r-- | blavote.go | 34 |
1 files changed, 29 insertions, 5 deletions
@@ -14,7 +14,7 @@ var version string func main() { - version = "0.5" + version = "0.6" //Command line arguments var opts struct { @@ -23,6 +23,7 @@ func main() { New string `short:"n" long:"new" description:"Title for a new poll"` Username string `short:"u" long:"username" description:"Username of user adding poll"` Remove int `short:"r" long:"remove" description:"ID of a poll to delete"` + Info int `short:"i" long:"info" description:"Get info and vote stats for a given poll ID"` Args struct { Rest []string @@ -84,15 +85,21 @@ func main() { } else { fmt.Println(err) } - + } else if (opts.Info > 0) { + err = pollInfo(db, opts.Info) } else { - err = vote(db, opts.Username, opts.Args.Rest) + if (len(opts.Args.Rest) > 1) { + err = vote(db, opts.Username, opts.Args.Rest) + + if (err != nil) { + fmt.Println(err) + } + } else { - if (err != nil) { - fmt.Println(err) } } + } func connectDb(name string) (*sqlite3.Conn, error) { @@ -177,3 +184,20 @@ func vote(db *sqlite3.Conn, nick string, options []string) error { return errors.New("You have already voted in this poll") } } + +func pollInfo(db *sqlite3.Conn, id int) error { + + poll , err := getPollFromId(db, id) + fmt.Print(poll.title + " - ") + options, err := getOptionsForPoll(db, poll) + + for _, option := range options { + option, err = getVotesForOption(db, option) + fmt.Print(option.text + " (") + fmt.Print(option.numVotes) + fmt.Print(") ") + } + fmt.Println() + + return err +} |