From a471e978989fecb533bcf8666772a8a425215346 Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Fri, 25 Jul 2014 12:53:27 +0100 Subject: Added function to output poll info/votes --- blavote.go | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) (limited to 'blavote.go') diff --git a/blavote.go b/blavote.go index fb4444b..03d2f28 100644 --- a/blavote.go +++ b/blavote.go @@ -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 +} -- cgit v1.2.3