Skip to content

Commit

Permalink
cleanup dup code
Browse files Browse the repository at this point in the history
  • Loading branch information
tmacro committed May 18, 2024
1 parent b015c82 commit c32d6ba
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 131 deletions.
50 changes: 15 additions & 35 deletions pkg/cli/dir/find.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package dir

import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"text/template"
"time"

"github.com/tmacro/today/pkg/cli"
"github.com/tmacro/today/pkg/utils"
)

type FindCommand struct {
Expression string `arg:"" name:"expression" help:"Regex expression to search for."`
Date string `arg:"" optional:"" default:"all" name:"date" help:"Date to show."`
Date string `arg:"" optional:"" name:"date" help:"Limit search to this date"`
}

type FindTemplate struct {
Expand All @@ -23,32 +20,28 @@ type FindTemplate struct {
}

func (c *FindCommand) Run(ctx *cli.Context) error {

var dir string
if c.Date != "all" {
var t time.Time
if c.Date == "" {
t = time.Now()
} else {
var err error
t, err = utils.ParseDate(c.Date)
if err != nil {
return err
}

if c.Date == "" {
dir = ctx.Config.Scratch.Directory
} else {
date, err := utils.GetDatestamp(c.Date, ctx.Config.DateFormat)
if err != nil {
return err
}
date := t.Format(ctx.Config.DateFormat)

dir = filepath.Join(ctx.Config.Scratch.Directory, date)
} else {
dir = ctx.Config.Scratch.Directory
}

resolved, err := utils.ResolvePath(dir)
if err != nil {
return err
}

tmpl, err := template.New("find").Parse(ctx.Config.Scratch.Find)
if err != nil {
_, err = os.Stat(resolved)
if os.IsNotExist(err) {
return fmt.Errorf("specified path does not exist: %s", resolved)
} else if err != nil {
return err
}

Expand All @@ -57,18 +50,5 @@ func (c *FindCommand) Run(ctx *cli.Context) error {
Expression: c.Expression,
}

var buf bytes.Buffer
err = tmpl.Execute(&buf, env)
if err != nil {
return err
}

cmd := buf.String()
ctx.Logger.Debug(cmd)

proc := exec.Command("sh", "-c", cmd)
proc.Stdout = os.Stdout
proc.Stderr = os.Stderr

return proc.Run()
return utils.RunTemplate(ctx.Config.Scratch.Find, env)
}
47 changes: 10 additions & 37 deletions pkg/cli/dir/search.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
package dir

import (
"bytes"
"os"
"os/exec"
"path/filepath"
"text/template"
"time"

"github.com/tmacro/today/pkg/cli"
"github.com/tmacro/today/pkg/utils"
)

type SearchCommand struct {
Expression string `arg:"" name:"expression" help:"Regex expression to search for."`
Date string `arg:"" optional:"" name:"date" help:"Date to show."`
Date string `arg:"" optional:"" name:"date" help:"Limit search to this date"`
}

type SearchTemplate struct {
Expand All @@ -23,50 +18,28 @@ type SearchTemplate struct {
}

func (c *SearchCommand) Run(ctx *cli.Context) error {

var dir string
if c.Date != "all" {
var t time.Time
if c.Date == "" {
t = time.Now()
} else {
var err error
t, err = utils.ParseDate(c.Date)
if err != nil {
return err
}

if c.Date == "" {
dir = ctx.Config.Scratch.Directory
} else {
date, err := utils.GetDatestamp(c.Date, ctx.Config.DateFormat)
if err != nil {
return err
}
date := t.Format(ctx.Config.DateFormat)

dir = filepath.Join(ctx.Config.Scratch.Directory, date)
} else {
dir = ctx.Config.Scratch.Directory
}

resolved, err := utils.ResolvePath(dir)
if err != nil {
return err
}

tmpl, err := template.New("search").Parse(ctx.Config.Scratch.Search)
if err != nil {
return err
}

env := SearchTemplate{
Directory: resolved,
Expression: c.Expression,
}

var buf bytes.Buffer
err = tmpl.Execute(&buf, env)
if err != nil {
return err
}

cmd := buf.String()
ctx.Logger.Debug(cmd)
proc := exec.Command("sh", "-c", cmd)
proc.Stdout = os.Stdout
proc.Stderr = os.Stderr
return proc.Run()
return utils.RunTemplate(ctx.Config.Scratch.Search, env)
}
37 changes: 3 additions & 34 deletions pkg/cli/dir/view.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package dir

import (
"bytes"
"os"
"os/exec"
"path/filepath"
"text/template"
"time"

"github.com/tmacro/today/pkg/cli"
"github.com/tmacro/today/pkg/utils"
)

type ViewCommand struct {
Date string `arg:"" optional:"" name:"date" help:"Date to show."`
Date time.Time `arg:"" optional:"" default:"" type:"reldate" name:"date" help:"Date to view."`
}

type ViewTemplate struct {
Expand All @@ -22,44 +18,17 @@ type ViewTemplate struct {
}

func (c *ViewCommand) Run(ctx *cli.Context) error {
var t time.Time
if c.Date == "" {
t = time.Now()
} else {
var err error
t, err = utils.ParseDate(c.Date)
if err != nil {
return err
}
}

date := t.Format(ctx.Config.DateFormat)
date := c.Date.Format(ctx.Config.DateFormat)
dir := filepath.Join(ctx.Config.Scratch.Directory, date)
resolved, err := utils.ResolvePath(dir)
if err != nil {
return err
}

tmpl, err := template.New("view").Parse(ctx.Config.Scratch.Viewer)
if err != nil {
return err
}

env := ViewTemplate{
Directory: resolved,
Date: date,
}

var buf bytes.Buffer
err = tmpl.Execute(&buf, env)
if err != nil {
return err
}

cmd := buf.String()
ctx.Logger.Debug(cmd)
proc := exec.Command("sh", "-c", cmd)
proc.Stdout = os.Stdout
proc.Stderr = os.Stderr
return proc.Run()
return utils.RunTemplate(ctx.Config.Scratch.Viewer, env)
}
25 changes: 21 additions & 4 deletions pkg/cli/note/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package note
import (
"fmt"
"os"
"path/filepath"

"github.com/tmacro/today/pkg/cli"
"github.com/tmacro/today/pkg/utils"
Expand All @@ -18,16 +19,32 @@ func (c *CreateCommand) Run(ctx *cli.Context) error {
return err
}

filename := utils.FormatDateFile(ctx.Config.Notes.Directory, date, ctx.Config.Notes.Extension)
filename := filepath.Join(ctx.Config.Notes.Directory, date, ctx.Config.Notes.Extension)
resolved, err := utils.ResolvePath(filename)
if err != nil {
return err
}

_, err = os.Stat(filename)
_, err = os.Stat(resolved)
if err == nil {
ctx.Logger.Debug("File already exists.")
return nil
} else if !os.IsNotExist(err) {
} else if os.IsNotExist(err) {
// Check to see if the notes directory exists
resolved, err := utils.ResolvePath(ctx.Config.Notes.Directory)
if err != nil {
return err
}
_, err = os.Stat(resolved)
if os.IsNotExist(err) {
return fmt.Errorf("notes directory does not exist: %s", resolved)
} else if err != nil {
return err
}
} else {
return err
}

fileHeader := fmt.Sprintf("# %s\n\n", date)
return os.WriteFile(filename, []byte(fileHeader), 0640)
return os.WriteFile(resolved, []byte(fileHeader), 0640)
}
21 changes: 5 additions & 16 deletions pkg/cli/note/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,15 @@ import (
)

type EditCommand struct {
Date string `arg:"" optional:"" name:"date" help:"Date to show."`
Date time.Time `arg:"" optional:"" default:"" type:"reldate" name:"date" help:"Date to edit."`
}

type EditTemplate struct {
Filename string
}

func (c *EditCommand) Run(ctx *cli.Context) error {
var t time.Time
if c.Date == "" {
t = time.Now()
} else {
var err error
t, err = utils.ParseDate(c.Date)
if err != nil {
return err
}
}

date := t.Format(ctx.Config.DateFormat)
date := c.Date.Format(ctx.Config.DateFormat)
filename := filepath.Join(ctx.Config.Notes.Directory, fmt.Sprintf("%s%s", date, ctx.Config.Notes.Extension))

resolved, err := utils.ResolvePath(filename)
Expand All @@ -41,12 +30,12 @@ func (c *EditCommand) Run(ctx *cli.Context) error {
}

_, err = os.Stat(resolved)
if err != nil && err != os.ErrNotExist {
if err != nil && !os.IsNotExist(err) {
return err
}

if err == os.ErrNotExist {
createCmd := CreateCommand{Date: c.Date}
if os.IsNotExist(err) {
createCmd := CreateCommand{Date: c.Date.Format(ctx.Config.DateFormat)}
err = createCmd.Run(ctx)
if err != nil {
return err
Expand Down
14 changes: 9 additions & 5 deletions pkg/cli/note/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,39 @@ package note

import (
"bytes"
"fmt"
"html/template"
"path/filepath"
"time"

"github.com/tmacro/today/pkg/cli"
"github.com/tmacro/today/pkg/utils"
)

type ViewCommand struct {
Date string `arg:"" optional:"" name:"date" help:"Date to show."`
Date time.Time `arg:"" optional:"" default:"" type:"reldate" name:"date" help:"Date to edit."`
}

type ViewTemplate struct {
Filename string
}

func (c *ViewCommand) Run(ctx *cli.Context) error {
date, err := utils.GetDatestamp(c.Date, ctx.Config.DateFormat)
date := c.Date.Format(ctx.Config.DateFormat)
filename := filepath.Join(ctx.Config.Notes.Directory, fmt.Sprintf("%s%s", date, ctx.Config.Notes.Extension))

resolved, err := utils.ResolvePath(filename)
if err != nil {
return err
}

filename := utils.FormatDateFile(ctx.Config.Notes.Directory, date, ctx.Config.Notes.Extension)

tmpl, err := template.New("view").Parse(ctx.Config.Notes.Viewer)
if err != nil {
return err
}

env := ViewTemplate{
Filename: filename,
Filename: resolved,
}

var buf bytes.Buffer
Expand Down
Loading

0 comments on commit c32d6ba

Please sign in to comment.