Skip to content

Commit

Permalink
fix note edit and view
Browse files Browse the repository at this point in the history
  • Loading branch information
tmacro committed May 17, 2024
1 parent 375fe47 commit 694f2fa
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 70 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ module github.com/tmacro/today

go 1.20

require github.com/spf13/pflag v1.0.5

require (
github.com/alecthomas/kong v0.7.1
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/sirupsen/logrus v1.9.2
gopkg.in/yaml.v3 v3.0.1
)
Expand Down
7 changes: 2 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
github.com/alecthomas/assert/v2 v2.1.0 h1:tbredtNcQnoSd3QBhQWI7QZ3XHOVkw1Moklp2ojoH/0=
github.com/alecthomas/assert/v2 v2.1.0/go.mod h1:b/+1DI2Q6NckYi+3mXyH3wFb8qG37K/DuK80n7WefXA=
github.com/alecthomas/kong v0.7.1 h1:azoTh0IOfwlAX3qN9sHWTxACE2oV8Bg2gAwBsMwDQY4=
github.com/alecthomas/kong v0.7.1/go.mod h1:n1iCIO2xS46oE8ZfYCNDqdR0b0wZNrXAIAqro/2132U=
github.com/alecthomas/repr v0.1.0 h1:ENn2e1+J3k09gyj2shc0dHr/yjaWSHRlrJ4DPMevDqE=
github.com/alecthomas/repr v0.1.0/go.mod h1:2kn6fqh/zIyPLmm3ugklbEi5hg5wS435eygvNfaDQL8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.2 h1:oxx1eChJGI6Uks2ZC4W1zpLlVgqB8ner4EuQwV4Ik1Y=
github.com/sirupsen/logrus v1.9.2/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down
24 changes: 8 additions & 16 deletions pkg/cli/note/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package note
import (
"fmt"
"os"
"path/filepath"
"time"

"github.com/tmacro/today/pkg/cli"
"github.com/tmacro/today/pkg/utils"
Expand All @@ -15,28 +13,22 @@ type CreateCommand struct {
}

func (c *CreateCommand) 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, err := utils.GetDatestamp(c.Date, ctx.Config.DateFormat)
if err != nil {
return err
}

date := t.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)
filename, err := utils.FormatDateFile(ctx.Config.Notes.Directory, date, ctx.Config.Notes.Extension)
if err != nil {
return err
}
_, err = os.Stat(resolved)

_, err = os.Stat(filename)
if err == nil {
ctx.Logger.Debug("File already exists.")
return nil
} else if !os.IsNotExist(err) {
return err
}

fileHeader := fmt.Sprintf("# %s\n\n", date)
Expand Down
7 changes: 1 addition & 6 deletions pkg/cli/note/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"html/template"
"os"
"os/exec"
"path/filepath"
"time"

Expand Down Expand Up @@ -72,9 +71,5 @@ func (c *EditCommand) Run(ctx *cli.Context) error {
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.Exec(cmd)
}
20 changes: 5 additions & 15 deletions pkg/cli/note/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package note

import (
"fmt"
"path/filepath"
"time"

"github.com/tmacro/today/pkg/cli"
"github.com/tmacro/today/pkg/utils"
Expand All @@ -14,24 +12,16 @@ type ShowCommand struct {
}

func (c *ShowCommand) 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, err := utils.GetDatestamp(c.Date, ctx.Config.DateFormat)
if err != nil {
return err
}

date := t.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)
filename, err := utils.FormatDateFile(ctx.Config.Notes.Directory, date, ctx.Config.Notes.Extension)
if err != nil {
return err
}

fmt.Println(resolved)
fmt.Println(filename)
return nil
}
30 changes: 6 additions & 24 deletions pkg/cli/note/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ package note

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

"github.com/tmacro/today/pkg/cli"
"github.com/tmacro/today/pkg/utils"
Expand All @@ -22,20 +17,12 @@ 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, err := utils.GetDatestamp(c.Date, ctx.Config.DateFormat)
if err != nil {
return err
}

date := t.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)
filename, err := utils.FormatDateFile(ctx.Config.Notes.Directory, date, ctx.Config.Notes.Extension)
if err != nil {
return err
}
Expand All @@ -46,7 +33,7 @@ func (c *ViewCommand) Run(ctx *cli.Context) error {
}

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

var buf bytes.Buffer
Expand All @@ -58,10 +45,5 @@ func (c *ViewCommand) Run(ctx *cli.Context) error {
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.Exec(cmd)
}
4 changes: 2 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ var DefaultTodayConfig = TodayConfig{
Notes: NotesConfig{
Extension: ".md",
Directory: "~/notes",
Editor: "nano -Y markdown -f /usr/share/nano/markdown.nanorc {{ .Filename }}",
Viewer: "nano -Y markdown -f /usr/share/nano/markdown.nanorc --view {{ .Filename }}",
Editor: "nano -Ymarkdown -f/usr/share/nano/markdown.nanorc {{ .Filename }}",
Viewer: "nano -Ymarkdown -f/usr/share/nano/markdown.nanorc --view {{ .Filename }}",
Search: "grep -i -n -H -T -r --color=auto \"{{ .Expression }}\" {{ .Directory }}",
},
Scratch: ScratchConfig{
Expand Down
16 changes: 16 additions & 0 deletions pkg/utils/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,19 @@ func ParseDate(date string) (time.Time, error) {

return time.Time{}, err
}

func GetDatestamp(date string, format string) (string, error) {
var t time.Time
if date == "" {
t = time.Now()
} else {
var err error
t, err = ParseDate(date)
if err != nil {
return "", err
}
}

date = t.Format(format)
return date, nil
}
38 changes: 38 additions & 0 deletions pkg/utils/exec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package utils

import (
"fmt"
"os"
"os/exec"
"syscall"

"github.com/google/shlex"
)

func Exec(cmd string) error {
tokens, err := shlex.Split(cmd)
if err != nil {
return err
}

if len(tokens) == 0 {
return fmt.Errorf("no command to execute")
}

binary, err := exec.LookPath(tokens[0])
if err != nil {
return err
}

args := tokens[1:]

fmt.Println(binary, args)

environ := os.Environ()
execErr := syscall.Exec(binary, args, environ)
if execErr != nil {
return execErr
}

return nil
}
8 changes: 8 additions & 0 deletions pkg/utils/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,11 @@ func getHomeDir(name string) (string, error) {

return dir, nil
}

func FormatDateDir(parent, date string) (string, error) {
return filepath.Join(parent, date), nil
}

func FormatDateFile(parent, date, extension string) (string, error) {
return filepath.Join(parent, date+extension), nil
}

0 comments on commit 694f2fa

Please sign in to comment.