package main // import "blavote" import "fmt" import "github.com/mxk/go-sqlite/sqlite3" import "github.com/jessevdk/go-flags" import "os" import "io/ioutil" import "strings" import "strconv" var version string func main() { version = "0.3" //Command line arguments var opts struct { Version bool `short:"v" long:"version" description:"Show program version"` Add string `short:"a" long:"add" description:"Title for a new poll"` 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"` Args struct { Rest []string } `positional-args:"yes"` } var args []string //If there are no command line arguments, read them from stdin if (len(os.Args) > 1) { flags.Parse(&opts) } else { bytes, err := ioutil.ReadAll(os.Stdin) if (err == nil) { args = strings.Split(strings.Split(string(bytes),"\n")[0], " ") flags.ParseArgs(&opts, args) } else { fmt.Println(err) //No args given, deal with it } } db, err := connectDb("blavote.db") if (err != nil) { fmt.Print("Could not connect to vote database: ") fmt.Println(err) return } if (opts.Version) { fmt.Println("v" + version) } else if (opts.Add != "") { fmt.Println(opts.Args.Rest) pollId, err := addPoll(db, opts.Add, opts.Args.Rest, opts.Username) if (err == nil) { fmt.Print("Poll added with ID ") fmt.Println(pollId) } else { fmt.Println(err) } } else if (opts.New != "") { pollId, err := addPoll(db, opts.New, opts.Args.Rest, opts.Username) if (err == nil) { fmt.Print("Poll added with ID ") fmt.Println(pollId) } else { fmt.Println(err) } } else if (opts.Remove > 0) { deletePoll(db, opts.Remove, opts.Username) if (err == nil) { fmt.Print("Poll removed with ID ") fmt.Println(opts.Remove) } else { fmt.Println(err) } } else { fmt.Println(opts.Args.Rest) vote(db, opts.Username, opts.Args.Rest) } } func connectDb(name string) (*sqlite3.Conn, error) { db, err := sqlite3.Open(name) if (err != nil) { return nil, err } //Check the version in the DB, if it doesn't exist we'll create it //Can use this later to update the DB if needed sql := "select * from info where key = 'version'" _, err = db.Query(sql) if (err != nil) { fmt.Println("info table does not exist, creating database") initTables(db) return nil, err } return db, err } func initTables(db *sqlite3.Conn) { db.Exec("create table info(id int, key text, value text)") db.Exec("insert into info (key, value) values('version', '0.01')") db.Exec("create table users(id integer primary key autoincrement, name text, admin boolean)") db.Exec("create table polls(id integer primary key autoincrement, title text, user_id int)") db.Exec("create table options(id integer primary key autoincrement, text text, poll_id int)") db.Exec("create table votes(id integer primary key autoincrement, user_id int, poll_id int, option_id int)") } func vote(db *sqlite3.Conn, nick string, options []string) { if pollId, err := strconv.Atoi(options[0]); err == nil { option := getOptionFromText(db, options[1]) user := getUserForName(db, nick) args := sqlite3.NamedArgs{"$a": user.id, "$b": pollId, "$c": option.id} sql := "INSERT INTO votes (user_id, poll_id, option_id) VALUES ($a, $b, $c)" err := db.Exec(sql, args) if (err != nil ) { fmt.Print("Failed to vote: ") fmt.Print(err) } else { fmt.Println("Vote added") } } }