super basic TODO cli program
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
2.2 KiB

package main
import (
"log"
"os"
"github.com/urfave/cli/v2"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func main() {
var (
force bool
)
dbFlags := []cli.Flag{
&cli.BoolFlag{
Name: "force",
Aliases: []string{"f"},
Value: false,
Usage: "Overwrite the db if one already exists in the path",
Destination: &force,
},
}
app := &cli.App{
Name: "godo",
Description: "a simple Go cli todo program",
Version: "1.0",
Authors: []*cli.Author{
&cli.Author{
Name: "grumbulon",
Email: "grumbulon@dismail.de",
},
},
Commands: []*cli.Command{
{
Name: "db",
Aliases: []string{"d"},
Usage: "run database related tasks",
Subcommands: []*cli.Command{
{
Name: "init",
Aliases: []string{"i"},
Flags: dbFlags,
Action: func(c *cli.Context) error {
dbResult := InitDb(force)
if !dbResult {
log.Fatalln("Ran into issues creating the database")
}
return nil
},
},
{
Name: "migrate",
Aliases: []string{"m"},
Usage: "When upgrading versions the database may change if rows or tables are added or removed. This will run the db migration for you",
Action: func(c *cli.Context) error {
db, err := gorm.Open(sqlite.Open(path), &gorm.Config{})
if err != nil {
panic("failed to connect to the database")
}
err = db.AutoMigrate(&Task{})
if err != nil {
return err
}
log.Println("Migration successful")
return nil
},
},
},
},
{
Name: "add",
Usage: "add a task",
Action: func(c *cli.Context) error {
if c.Args().First() == "" {
log.Fatalln("You must provide a task to be added")
}
AddTask(c.Args().Slice())
return nil
},
},
{
Name: "ls",
Usage: "list existing tasks",
Action: func(c *cli.Context) error {
LsTask()
return nil
},
},
{
Name: "rm",
Usage: "remove an existing task or two",
Action: func(c *cli.Context) error {
RmTask(c.Args().Slice())
return nil
},
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatalln(err)
}
}