Author: Pedro Lucas Porcellis <pedrolucasporcellis@gmail.com>
Tidy up and add a nice POSIX compliant options parsing
commands/create.go | 17 ++++++++++++++++ commands/list.go | 12 ++++------ config/config.go | 5 ---- main.go | 51 +++++++++++++++++++++++++++++++++++++++++++++--
diff --git a/commands/create.go b/commands/create.go index 1da0dedf1cdceb1219eefc6a125303eac9bbe4a8..6917dc19d9c4981e3e22e34f439f0608a3680af8 100644 --- a/commands/create.go +++ b/commands/create.go @@ -9,6 +9,22 @@ "path" "time" ) +func CreateWithName(config config.TConfig, title string) (models.Note, error) { + var ( + err error + note models.Note + ) + + file, err := os.Create(path.Join(config.BasePath, fmt.Sprintf("%s.md", title))) + + stat, _ := file.Stat() + + note = models.Note{Name: stat.Name(), Path: path.Join(config.BasePath, stat.Name()), ModTime: stat.ModTime()} + + return note, err + +} + func Create(config config.TConfig) (models.Note, error) { var ( err error @@ -17,6 +33,7 @@ ) currentTime := time.Now() noteName := currentTime.Format("2006-01-02T15:04:05Z07:00") + file, err := os.Create(path.Join(config.BasePath, fmt.Sprintf("%s.md", noteName))) stat, _ := file.Stat() diff --git a/commands/list.go b/commands/list.go index e7349b08354999abd7a8c22b109f21920620e2eb..a754974119be83f79806b642ddba7e99b6b36206 100644 --- a/commands/list.go +++ b/commands/list.go @@ -23,9 +23,7 @@ fmt.Println("\n \t Your notes") for index, note := range notes { - if index > 0 { - fmt.Printf("|#%-6d|\t%6s\t|%6s|\n", index, note.Title(), note.UpdatedAt()) - } + fmt.Printf("|#%-6d|\t%6s\t|%6s|\n", index, note.Title(), note.UpdatedAt()) } return err @@ -36,8 +34,6 @@ var ( err error notes []models.Note ) - - fmt.Println("Listing some stuff") files, err := ioutil.ReadDir(config.BasePath) @@ -46,9 +42,11 @@ return files[index].ModTime().After(files[aux].ModTime()) }) for _, entry := range files { - note := models.Note{Name: entry.Name(), Path: path.Join(config.BasePath, entry.Name()), ModTime: entry.ModTime()} + if !entry.IsDir() { + note := models.Note{Name: entry.Name(), Path: path.Join(config.BasePath, entry.Name()), ModTime: entry.ModTime()} - notes = append(notes, note) + notes = append(notes, note) + } } return notes, err diff --git a/config/config.go b/config/config.go index 6064399a4f14604076ae44acfa9e73860157945b..d9157a9a2676b2e19419ccdb05963afe68fa6e5b 100644 --- a/config/config.go +++ b/config/config.go @@ -1,7 +1,6 @@ package config import ( - "fmt" "os/user" "path" ) @@ -24,11 +23,7 @@ } base := path.Join(currentUser.HomeDir, "notes") - fmt.Println(base) - config := &TConfig{BasePath: base, GitRemote: "origin"} - - fmt.Println(config) return config, err } diff --git a/main.go b/main.go index d4794144cd9c3ad952e0e4c9ad3fe9a4602d7423..b0e03f5189689b2abb4b6767896cbed8388a8336 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,11 @@ 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" + "os" ) func main() { @@ -17,9 +19,52 @@ if err != nil { panic(err) } - err = commands.List(*c) + opts, optind, err := getopt.Getopts(os.Args, "le:c:d:") if err != nil { - fmt.Println(err) + panic(err) + } + + for _, opt := range opts { + switch opt.Option { + case 'e': + println("Edit specified") + case 'l': + commands.List(*c) + case 'c': + println("On Create") + println(opt.Value) + + var note models.Note + if opt.Value != "" { + note, err = commands.CreateWithName(*c, opt.Value) + } else { + note, err = commands.Create(*c) + } + + if err != nil { + panic("We we're not able to create your note") + } + + err = commands.Write(note) + + if err != nil { + panic("We could not invoke your $EDITOR") + } + + // We should call sync + // commands.Sync() + + println("Note created", note.Name) + case 'd': + println("Delete specified") + } } + + for _, arg := range os.Args[optind:] { + if string(arg) == "l" { + commands.List(*c) + } + } + }