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" var version string func main() { version = "0.02" //Command line arguments var opts struct { Version bool `short:"v" long:"version" description:"Show program version"` Add bool `short:"a" long:"add" description:"Add a new poll"` New bool `short:"n" long:"new" description:"Add a new poll"` Args struct { Title string 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 || opts.New) { addPoll(db, opts.Args.Title, opts.Args.Rest) } createUser(db, "l_bratch", true) getUserForId(db, 1) } 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, text 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 addPoll(db *sqlite3.Conn, title string, options []string) { }