Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3ed7d8cd9e | ||
|
|
492e0af993 | ||
|
|
10b95672e3 | ||
|
|
183f39bd2a | ||
|
|
24154df995 |
@@ -7,6 +7,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"local/pt-todo-server/pttodo"
|
"local/pt-todo-server/pttodo"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path"
|
"path"
|
||||||
@@ -28,16 +29,17 @@ func _main() error {
|
|||||||
}
|
}
|
||||||
filepath := flag.String("f", defaultFilepath, "($PTTODO_FILE) path to yaml file")
|
filepath := flag.String("f", defaultFilepath, "($PTTODO_FILE) path to yaml file")
|
||||||
e := flag.Bool("e", false, "edit file")
|
e := flag.Bool("e", false, "edit file")
|
||||||
|
dry := flag.Bool("dry", false, "dry run")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
if *e {
|
if *e {
|
||||||
if err := edit(*filepath); err != nil {
|
if err := edit(*dry, *filepath); err != nil {
|
||||||
return err
|
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
|
var tempFile string
|
||||||
cp := func() error {
|
cp := func() error {
|
||||||
f, err := ioutil.TempFile(os.TempDir(), path.Base(filepath))
|
f, err := ioutil.TempFile(os.TempDir(), path.Base(filepath))
|
||||||
@@ -90,15 +92,19 @@ func edit(filepath string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
verify := func() error {
|
verify := func() error {
|
||||||
return dump(io.Discard, tempFile)
|
return dump(true, io.Discard, tempFile)
|
||||||
}
|
}
|
||||||
save := func() error {
|
save := func() error {
|
||||||
|
if dry {
|
||||||
|
log.Printf("would've saved %s as %s", tempFile, filepath)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
return os.Rename(tempFile, filepath)
|
return os.Rename(tempFile, filepath)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, foo := range []func() error{cp, vi, verify, save} {
|
for _, foo := range []func() error{cp, vi, verify, save} {
|
||||||
if err := foo(); err != nil {
|
if err := foo(); err != nil {
|
||||||
if tempFile != "" {
|
if tempFile != "" && !dry {
|
||||||
os.Remove(tempFile)
|
os.Remove(tempFile)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
@@ -108,7 +114,7 @@ func edit(filepath string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func dump(writer io.Writer, filepath string) error {
|
func dump(dry bool, writer io.Writer, filepath string) error {
|
||||||
var reader io.Reader
|
var reader io.Reader
|
||||||
if filepath == "-" {
|
if filepath == "-" {
|
||||||
reader = os.Stdin
|
reader = os.Stdin
|
||||||
@@ -131,10 +137,25 @@ func dump(writer io.Writer, filepath string) error {
|
|||||||
}
|
}
|
||||||
root.MoveScheduledToTodo()
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
fmt.Fprintf(writer, "%s\n", b2)
|
fmt.Fprintf(writer, "%s\n", b2)
|
||||||
return nil
|
|
||||||
|
if dry {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return os.WriteFile(filepath, b2, os.ModePerm)
|
||||||
}
|
}
|
||||||
|
|||||||
20
cmd/pttodo-cli/install.sh
Normal file
20
cmd/pttodo-cli/install.sh
Normal 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"
|
||||||
@@ -7,8 +7,8 @@ import (
|
|||||||
|
|
||||||
type Todo struct {
|
type Todo struct {
|
||||||
Todo string
|
Todo string
|
||||||
|
TS TS
|
||||||
Details string `yaml:",omitempty"`
|
Details string `yaml:",omitempty"`
|
||||||
TS TS `yaml:",omitempty"`
|
|
||||||
Schedule Schedule `yaml:",omitempty"`
|
Schedule Schedule `yaml:",omitempty"`
|
||||||
Tags string `yaml:",omitempty"`
|
Tags string `yaml:",omitempty"`
|
||||||
Subtasks []Todo `yaml:",omitempty"`
|
Subtasks []Todo `yaml:",omitempty"`
|
||||||
|
|||||||
@@ -2,10 +2,40 @@ package pttodo
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"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) {
|
func TestJSONTS(t *testing.T) {
|
||||||
ts := TS(time.Now().Unix())
|
ts := TS(time.Now().Unix())
|
||||||
js, err := json.Marshal(ts)
|
js, err := json.Marshal(ts)
|
||||||
|
|||||||
Reference in New Issue
Block a user