diff --git a/cmd/copy.go b/cmd/copy.go index 12bcef1..a7c202d 100644 --- a/cmd/copy.go +++ b/cmd/copy.go @@ -2,7 +2,6 @@ package cmd import ( "fmt" - "log" "os/exec" "runtime" @@ -14,28 +13,19 @@ var copyCmd = &cobra.Command{ Use: "copy", Short: "Copy the specified log to the clipboard", Long: "Copy the specified log to the clipboard", - Run: func(cmd *cobra.Command, args []string) { - projectPath, err := daylog.EnsureProjectPath(config.Project) - if err != nil { - log.Fatal(err) - } - - dl, err := daylog.New(args, projectPath) - if err != nil { - log.Fatal(err) - } - + Run: runCommand(func(cmd *cobra.Command, dl *daylog.DayLog) error { logContents, err := dl.Show("text") if err != nil { - log.Fatal(err) + return err } if err := copy([]byte(logContents)); err != nil { - log.Fatal(err) + return err } fmt.Println("Copied to clipboard.") - }, + return nil + }), } func init() { diff --git a/cmd/list.go b/cmd/list.go index 75d4c18..60ced9f 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -2,50 +2,30 @@ package cmd import ( "fmt" - "log" "github.com/notnmeyer/daylog-cli/internal/daylog" "github.com/notnmeyer/daylog-cli/internal/file" "github.com/spf13/cobra" ) -// listCmd represents the list command var listCmd = &cobra.Command{ Use: "list", Short: "list all log files", Long: "list all log files relative to the project directory", - Run: func(cmd *cobra.Command, args []string) { - projectPath, err := daylog.EnsureProjectPath(config.Project) - if err != nil { - log.Fatal(err) - } - - dl, err := daylog.New(args, projectPath) - if err != nil { - log.Fatal(err) - } - + Run: runCommand(func(cmd *cobra.Command, dl *daylog.DayLog) error { logs, err := file.LogProvider{}.GetLogs(dl.ProjectPath) if err != nil { - log.Fatal(err) + return err } for _, log := range logs { fmt.Println(log) } - }, + + return nil + }), } func init() { rootCmd.AddCommand(listCmd) - - // Here you will define your flags and configuration settings. - - // Cobra supports Persistent Flags which will work for this command - // and all subcommands, e.g.: - // listCmd.PersistentFlags().String("foo", "", "A help for foo") - - // Cobra supports local flags which will only run when this command - // is called directly, e.g.: - // listCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } diff --git a/cmd/root.go b/cmd/root.go index 9fa45cb..c5b34f7 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -27,42 +27,22 @@ var rootCmd = &cobra.Command{ Short: "A tool for keeping track of what you did today", Long: "DayLog: Fighter of the Night Log! A tool for keeping track of what you did today, yesterday, and tomorrow", - Run: func(cmd *cobra.Command, args []string) { - projectPath, err := daylog.EnsureProjectPath(config.Project) - if err != nil { - log.Fatal(err) - } - - dl, err := daylog.New(args, projectPath) - if err != nil { - log.Fatal(err) - } - - if err := applyPrevFlag(cmd, dl); err != nil { - log.Fatal(err) - } - + Run: runCommand(func(cmd *cobra.Command, dl *daylog.DayLog) error { piped, err := stdinIsPiped() if err != nil { - log.Fatal(err) + return err } if piped { content, err := io.ReadAll(os.Stdin) if err != nil { - log.Fatal(err) - } - formatted := formatStdinContent(string(content)) - if err := dl.Append(formatted); err != nil { - log.Fatal(err) + return err } - return + return dl.Append(formatStdinContent(string(content))) } - if err := dl.Edit(); err != nil { - log.Fatal(err) - } - }, + return dl.Edit() + }), } // entrypoint @@ -86,15 +66,33 @@ func init() { rootCmd.PersistentFlags().Bool("prev", false, "Operate on the most recent log that isn't today's") } -func applyPrevFlag(cmd *cobra.Command, dl *daylog.DayLog) error { - showPrevious, err := cmd.PersistentFlags().GetBool("prev") - if err != nil { - return err - } - if showPrevious { - return dl.UsePrevious(time.Now()) +func runCommand(fn func(cmd *cobra.Command, dl *daylog.DayLog) error) func(cmd *cobra.Command, args []string) { + return func(cmd *cobra.Command, args []string) { + projectPath, err := daylog.EnsureProjectPath(config.Project) + if err != nil { + log.Fatal(err) + } + + dl, err := daylog.New(args, projectPath) + if err != nil { + log.Fatal(err) + } + + showPrevious, err := cmd.Root().PersistentFlags().GetBool("prev") + if err != nil { + log.Fatal(err) + } + + if showPrevious { + if err := dl.UsePrevious(time.Now()); err != nil { + log.Fatal(err) + } + } + + if err := fn(cmd, dl); err != nil { + log.Fatal(err) + } } - return nil } func formatStdinContent(content string) string { diff --git a/cmd/show.go b/cmd/show.go index 54a86f3..94cb9f7 100644 --- a/cmd/show.go +++ b/cmd/show.go @@ -2,55 +2,29 @@ package cmd import ( "fmt" - "log" - "time" "github.com/notnmeyer/daylog-cli/internal/daylog" "github.com/spf13/cobra" ) -type ShowConfig struct { - Output string -} - var showCmd = &cobra.Command{ Use: "show", Short: "Display today's log", Long: "Display today's log", - Run: func(cmd *cobra.Command, args []string) { - projectPath, err := daylog.EnsureProjectPath(config.Project) - if err != nil { - log.Fatal(err) - } - - dl, err := daylog.New(args, projectPath) - if err != nil { - log.Fatal(err) - } - + Run: runCommand(func(cmd *cobra.Command, dl *daylog.DayLog) error { format, err := cmd.PersistentFlags().GetString("output") if err != nil { - log.Fatal(err) - } - - showPrevious, err := cmd.Root().PersistentFlags().GetBool("prev") - if err != nil { - log.Fatal(err) - } - - if showPrevious { - if err := dl.UsePrevious(time.Now()); err != nil { - log.Fatal(err) - } + return err } logContents, err := dl.Show(format) if err != nil { - log.Fatal(err) + return err } fmt.Println(string(logContents)) - }, + return nil + }), } func init() {