Author: Pedro Lucas Porcellis <pedrolucasporcellis@gmail.com>
Add a usage and version to the main program
commands/commit.go | 39 ++++++++++++++++++++++++++ commands/sync.go | 16 ++++++++++ main.go | 70 ++++++++++++++++++++++++++++++++++++++---------
diff --git a/commands/commit.go b/commands/commit.go new file mode 100644 index 0000000000000000000000000000000000000000..419a22c4d73634b9787ddf96edd69f3c9d59846d --- /dev/null +++ b/commands/commit.go @@ -0,0 +1,39 @@ +package commands + +import ( + "fmt" + "git.sr.ht/~porcellis/t/config" + "git.sr.ht/~porcellis/t/models" + "os" + "os/exec" +) + +func AddToStagedList(basePath string, notePath string) error { + cmd := exec.Command("git", "-C", basePath, "add", notePath) + cmd.Stdin = nil + cmd.Stdout = nil + cmd.Stderr = os.Stderr + + return cmd.Run() +} + +func Commit(config config.TConfig, note models.Note) error { + var ( + err error + ) + + err = AddToStagedList(config.BasePath, note.Path) + + if err != nil { + return err + } + + commitMessage := fmt.Sprintf("Modified '%s'", note.Title()) + + cmd := exec.Command("git", "-C", config.BasePath, "commit", "-am", commitMessage) + cmd.Stdin = nil + cmd.Stdout = nil + cmd.Stderr = os.Stderr + + return cmd.Run() +} diff --git a/commands/sync.go b/commands/sync.go new file mode 100644 index 0000000000000000000000000000000000000000..b95b946bc5773b59db7ca6f55a0edc2692b2c796 --- /dev/null +++ b/commands/sync.go @@ -0,0 +1,16 @@ +package commands + +import ( + "git.sr.ht/~porcellis/t/config" + "os" + "os/exec" +) + +func Sync(config config.TConfig) error { + cmd := exec.Command("git", "-C", config.BasePath, "push", config.GitRemote, "-u", "--quiet") + cmd.Stdin = nil + cmd.Stdout = os.NewFile(0, os.DevNull) + cmd.Stderr = os.Stderr + + return cmd.Run() +} diff --git a/main.go b/main.go index 981efc8df19c3931594e4fbcf1a59e6dd6c89122..740476a5a3ebc4f97bc6da07ba82f66523526fbc 100644 --- a/main.go +++ b/main.go @@ -5,8 +5,17 @@ "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" + +func usage() { + log.Fatal("Usage: t -[l,c,e]") +} func main() { var ( @@ -19,16 +28,47 @@ if err != nil { panic(err) } - opts, optind, err := getopt.Getopts(os.Args, "le:c:d:") + opts, _, err := getopt.Getopts(os.Args, "vle:c:d:") if err != nil { - panic(err) + usage() + + return } for _, opt := range opts { switch opt.Option { case 'e': - println("Edit specified") + println("Editing") + + var note models.Note + notes, _ := commands.BuildList(*c) + + if opt.Value == "" { + note = notes[0] + } else { + index, err := strconv.Atoi(opt.Value) + + if err == nil { + note = notes[index] + } + } + + err = commands.Write(note) + + err = commands.Commit(*c, note) + + if err != nil { + panic("Could not commit your edited note") + } + + err = commands.Sync(*c) + + if err != nil { + panic("Could not sync your note") + } + + println("Finished editing ", note.Title()) case 'l': commands.List(*c) case 'c': @@ -52,19 +92,21 @@ panic("We could not invoke your $EDITOR") } // We should call commit - // commands.Commit() + err = commands.Commit(*c, note) + + if err != nil { + panic("We could not commit your note") + } + + err = commands.Sync(*c) + + if err != nil { + panic("We could not sync your notes") + } - // commands.Sync() println("Note created", note.Name) - case 'd': - println("Delete specified") + case 'v': + println("t", Version) } } - - for _, arg := range os.Args[optind:] { - if string(arg) == "l" { - commands.List(*c) - } - } - }