Author: Pedro Lucas Porcellis <pedrolucasporcellis@gmail.com>
Introduce commands instead of wrongly use POSIX args
main.go | 124 +++++++++++++++++++++++++++++-----------------------------
diff --git a/main.go b/main.go index 740476a5a3ebc4f97bc6da07ba82f66523526fbc..c338bfd7b3ddd067094f334cc46d507b39a9dab1 100644 --- a/main.go +++ b/main.go @@ -1,112 +1,112 @@ package main import ( + "fmt" "git.sr.ht/~porcellis/t/commands" "git.sr.ht/~porcellis/t/config" "git.sr.ht/~porcellis/t/models" - "git.sr.ht/~sircmpwn/getopt" "log" "os" - "strconv" ) -var Version = "0.0.1" +var Version = "0.1.0" func usage() { - log.Fatal("Usage: t -[l,c,e]") + log.Fatal("Usage: t [list, create, edit, version]") } func main() { var ( - c *config.TConfig + configuration *config.TConfig ) - c, err := config.Initialize() + configuration, err := config.Initialize() if err != nil { panic(err) } - opts, _, err := getopt.Getopts(os.Args, "vle:c:d:") - - if err != nil { + if len(os.Args) < 2 { usage() return } - for _, opt := range opts { - switch opt.Option { - case 'e': - println("Editing") + switch os.Args[1] { + case "create", "c": + var note models.Note + if len(os.Args) == 2 { + note, err = commands.Create(*configuration) + } else { + note, err = commands.CreateWithName(*configuration, os.Args[2]) + } - var note models.Note - notes, _ := commands.BuildList(*c) + if err != nil { + panic("We we're not able to create your note") + } - if opt.Value == "" { - note = notes[0] - } else { - index, err := strconv.Atoi(opt.Value) + err = commands.Write(note) - if err == nil { - note = notes[index] - } - } + if err != nil { + panic("We could not invoke your $EDITOR") + } - err = commands.Write(note) + // We should call commit + err = commands.Commit(*configuration, note) - err = commands.Commit(*c, note) + if err != nil { + panic("We could not commit your note") + } - if err != nil { - panic("Could not commit your edited note") - } + err = commands.Sync(*configuration) - err = commands.Sync(*c) + if err != nil { + panic("We could not sync your notes") + } - if err != nil { - panic("Could not sync your note") - } + fmt.Println(fmt.Sprintf("Note created %s", note.Name)) - println("Finished editing ", note.Title()) - case 'l': - commands.List(*c) - case 'c': - println(opt.Value) + case "list", "l": + commands.List(*configuration) + case "edit", "e": + var note models.Note + notes, _ := commands.BuildList(*configuration) - var note models.Note - if opt.Value != "" { - note, err = commands.CreateWithName(*c, opt.Value) - } else { - note, err = commands.Create(*c) - } + if len(os.Args) == 2 { + note = notes[0] + } else { + index, err := strconv.Atoi(os.Args[2]) - if err != nil { - panic("We we're not able to create your note") + if err == nil { + note = notes[index] } + } - err = commands.Write(note) + err = commands.Write(note) - if err != nil { - panic("We could not invoke your $EDITOR") - } + err = commands.Commit(*configuration, note) - // We should call commit - err = commands.Commit(*c, note) + if err != nil { + panic("Could not commit your edited note") + } - if err != nil { - panic("We could not commit your note") - } + err = commands.Sync(*configuration) + + if err != nil { + panic("Could not sync your note") + } + + println("Finished editing ", note.Title()) - err = commands.Sync(*c) + case "version", "v": + println("t ", Version) - if err != nil { - panic("We could not sync your notes") - } + case "help", "h": + usage() - println("Note created", note.Name) - case 'v': - println("t", Version) - } + default: + usage() + os.Exit(1) } }