Compare commits

...

8 Commits

Author SHA1 Message Date
Bel LaPointe 3ed7d8cd9e update install for gomo 2021-12-31 22:32:47 -05:00
Bel LaPointe 492e0af993 if optional positional arg is todo/scheduled/done, then resolve first level 2021-12-31 22:32:07 -05:00
Bel LaPointe 10b95672e3 up install script for location 2021-12-31 22:28:12 -05:00
Bel LaPointe 183f39bd2a set ts to currenttime if should display 2021-12-31 22:27:48 -05:00
Bel LaPointe 24154df995 cli has dry mode and install script 2021-12-31 22:27:33 -05:00
Bel LaPointe f7dac79233 pttodo-cli default file via env 2021-12-31 22:07:16 -05:00
Bel LaPointe 8c45d4a7df pttodo-cli works even if file does not initially exist 2021-12-31 22:05:47 -05:00
Bel LaPointe 1e3f24b4d5 rename gomod, root project 2021-12-31 21:58:02 -05:00
7 changed files with 96 additions and 19 deletions

View File

@ -7,6 +7,7 @@ import (
"io"
"io/ioutil"
"local/pt-todo-server/pttodo"
"log"
"os"
"os/exec"
"path"
@ -22,32 +23,39 @@ func main() {
}
func _main() error {
filepath := flag.String("f", "-", "path to yaml file")
defaultFilepath, ok := os.LookupEnv("PTTODO_FILE")
if !ok {
defaultFilepath = "-"
}
filepath := flag.String("f", defaultFilepath, "($PTTODO_FILE) path to yaml file")
e := flag.Bool("e", false, "edit file")
dry := flag.Bool("dry", false, "dry run")
flag.Parse()
if *e {
if err := edit(*filepath); err != nil {
if err := edit(*dry, *filepath); err != nil {
return err
}
}
return dump(os.Stdout, *filepath)
return dump(*dry, os.Stdout, *filepath)
}
func edit(filepath string) error {
func edit(dry bool, filepath string) error {
var tempFile string
cp := func() error {
f, err := ioutil.TempFile(os.TempDir(), path.Base(filepath))
if err != nil {
return err
}
g, err := os.Open(filepath)
if err != nil {
return err
if _, err := os.Stat(filepath); err == nil {
g, err := os.Open(filepath)
if err != nil {
return err
}
if _, err := io.Copy(f, g); err != nil {
return err
}
g.Close()
}
if _, err := io.Copy(f, g); err != nil {
return err
}
g.Close()
f.Close()
tempFile = f.Name()
return nil
@ -84,15 +92,19 @@ func edit(filepath string) error {
return nil
}
verify := func() error {
return dump(io.Discard, tempFile)
return dump(true, io.Discard, tempFile)
}
save := func() error {
if dry {
log.Printf("would've saved %s as %s", tempFile, filepath)
return nil
}
return os.Rename(tempFile, filepath)
}
for _, foo := range []func() error{cp, vi, verify, save} {
if err := foo(); err != nil {
if tempFile != "" {
if tempFile != "" && !dry {
os.Remove(tempFile)
}
return err
@ -102,7 +114,7 @@ func edit(filepath string) error {
return nil
}
func dump(writer io.Writer, filepath string) error {
func dump(dry bool, writer io.Writer, filepath string) error {
var reader io.Reader
if filepath == "-" {
reader = os.Stdin
@ -125,10 +137,25 @@ func dump(writer io.Writer, filepath string) error {
}
root.MoveScheduledToTodo()
b2, err := yaml.Marshal(root)
var v interface{} = root
switch flag.Arg(0) {
case "":
case "todo":
v = root.Todo
case "scheduled":
v = root.Scheduled
case "done":
v = root.Done
}
b2, err := yaml.Marshal(v)
if err != nil {
return err
}
fmt.Fprintf(writer, "%s\n", b2)
return nil
if dry {
return nil
}
return os.WriteFile(filepath, b2, os.ModePerm)
}

View File

@ -1,4 +1,4 @@
module pttodo
module pttodo-cli
go 1.17

20
cmd/pttodo-cli/install.sh Normal file
View File

@ -0,0 +1,20 @@
#! /bin/bash
cd "$(dirname "$BASH_SOURCE")"
binary_name="$(head -n 1 go.mod | awk '{print $NF}' | sed 's/.*\///')"
git_commit="$((
git rev-list -1 HEAD
if git diff | grep . > /dev/null; then
echo "-dirty"
fi
) 2> /dev/null | tr -d '\n')"
GOFLAGS="" \
GO111MODULE="" \
CGO_ENABLED=0 \
go build \
-o $GOPATH/bin/$binary_name \
-a \
-installsuffix cgo \
-ldflags "-s -w -X main.GitCommit=$git_commit"

2
go.mod
View File

@ -1,4 +1,4 @@
module local/pt-todo-server
module pttodo
go 1.17

View File

@ -7,8 +7,8 @@ import (
type Todo struct {
Todo string
TS TS
Details string `yaml:",omitempty"`
TS TS `yaml:",omitempty"`
Schedule Schedule `yaml:",omitempty"`
Tags string `yaml:",omitempty"`
Subtasks []Todo `yaml:",omitempty"`

View File

@ -2,10 +2,40 @@ package pttodo
import (
"encoding/json"
"strings"
"testing"
"time"
yaml "gopkg.in/yaml.v2"
)
func TestTSMarshalYaml(t *testing.T) {
t.Run("nonzero", func(t *testing.T) {
var ts TS
if b, err := yaml.Marshal(TS(5)); err != nil {
t.Fatal(err)
} else if s := string(b); strings.TrimSpace(s) != `5` {
t.Fatal(s)
} else if err := yaml.Unmarshal(b, &ts); err != nil {
t.Fatal(err)
} else if ts != 5 {
t.Fatal(ts)
}
})
t.Run("zero", func(t *testing.T) {
var ts TS
if b, err := yaml.Marshal(TS(0)); err != nil {
t.Fatal(err)
} else if s := string(b); strings.TrimSpace(s) == `0` {
t.Fatal(s)
} else if err := yaml.Unmarshal(b, &ts); err != nil {
t.Fatal(err)
} else if ts == 0 {
t.Fatal(ts)
}
})
}
func TestJSONTS(t *testing.T) {
ts := TS(time.Now().Unix())
js, err := json.Marshal(ts)