Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 5 additions & 15 deletions cmd/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"fmt"
"log"
"os/exec"
"runtime"

Expand All @@ -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() {
Expand Down
30 changes: 5 additions & 25 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
66 changes: 32 additions & 34 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
36 changes: 5 additions & 31 deletions cmd/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Loading