Compare commits
15 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
a54ccae4c2 | |
|
|
a27d5c38d7 | |
|
|
cb4886992a | |
|
|
15c5f03ccf | |
|
|
b82f11c248 | |
|
|
3c9b34202b | |
|
|
967a02c90a | |
|
|
3ed7d8cd9e | |
|
|
492e0af993 | |
|
|
10b95672e3 | |
|
|
183f39bd2a | |
|
|
24154df995 | |
|
|
f7dac79233 | |
|
|
8c45d4a7df | |
|
|
1e3f24b4d5 |
|
|
@ -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"
|
||||||
|
|
@ -22,44 +23,59 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func _main() error {
|
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")
|
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))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
g, err := os.Open(filepath)
|
if _, err := os.Stat(filepath); err == nil {
|
||||||
if err != nil {
|
g, err := os.Open(filepath)
|
||||||
return err
|
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()
|
f.Close()
|
||||||
tempFile = f.Name()
|
tempFile = f.Name() + ".yaml"
|
||||||
return nil
|
return os.Rename(f.Name(), tempFile)
|
||||||
}
|
}
|
||||||
vi := func() error {
|
vi := func() error {
|
||||||
vibin, err := exec.LookPath("vi")
|
bin := "vim"
|
||||||
|
if editor := os.Getenv("EDITOR"); editor != "" {
|
||||||
|
bin = editor
|
||||||
|
}
|
||||||
|
editorbin, err := exec.LookPath(bin)
|
||||||
|
if err != nil {
|
||||||
|
editorbin, err = exec.LookPath("vi")
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
args := []string{editorbin, tempFile}
|
||||||
cpid, err := syscall.ForkExec(
|
cpid, err := syscall.ForkExec(
|
||||||
vibin,
|
editorbin,
|
||||||
[]string{vibin, tempFile},
|
args,
|
||||||
&syscall.ProcAttr{
|
&syscall.ProcAttr{
|
||||||
Dir: "",
|
Dir: "",
|
||||||
Env: os.Environ(),
|
Env: os.Environ(),
|
||||||
|
|
@ -84,25 +100,32 @@ func edit(filepath string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
verify := func() error {
|
verify := func() error {
|
||||||
return dump(io.Discard, tempFile)
|
if err := dump(true, io.Discard, tempFile); err != nil {
|
||||||
|
return fmt.Errorf("failed to verify %s: %v", tempFile, err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
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 != "" {
|
|
||||||
os.Remove(tempFile)
|
|
||||||
}
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if !dry {
|
||||||
|
os.Remove(tempFile)
|
||||||
|
}
|
||||||
|
|
||||||
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
|
||||||
|
|
@ -125,10 +148,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)
|
||||||
}
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
module pttodo
|
module pttodo-cli
|
||||||
|
|
||||||
go 1.17
|
go 1.17
|
||||||
|
|
||||||
|
|
@ -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"`
|
||||||
|
|
|
||||||
23
pttodo/ts.go
23
pttodo/ts.go
|
|
@ -2,7 +2,10 @@ package pttodo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
yaml "gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TS int64
|
type TS int64
|
||||||
|
|
@ -23,9 +26,25 @@ func (ts TS) MarshalYAML() (interface{}, error) {
|
||||||
if ts == 0 {
|
if ts == 0 {
|
||||||
ts = TS(time.Now().Unix())
|
ts = TS(time.Now().Unix())
|
||||||
}
|
}
|
||||||
return int64(ts), nil
|
t := time.Unix(int64(ts), 0)
|
||||||
|
return t.Format(time.UnixDate), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ts *TS) UnmarshalJSON(b []byte) error {
|
func (ts *TS) UnmarshalJSON(b []byte) error {
|
||||||
return json.Unmarshal(b, (*int64)(ts))
|
return yaml.Unmarshal(b, ts)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ts *TS) UnmarshalYAML(unmarshaller func(interface{}) error) error {
|
||||||
|
var n int64
|
||||||
|
if err := unmarshaller(&n); err == nil {
|
||||||
|
*ts = TS(n)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var s string
|
||||||
|
if err := unmarshaller(&s); err == nil {
|
||||||
|
t, err := time.Parse(time.UnixDate, s)
|
||||||
|
*ts = TS(t.Unix())
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return errors.New("illegal TS")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,12 +2,41 @@ package pttodo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"strings"
|
||||||
"testing"
|
"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.HasSuffix(strings.TrimSpace(s), ` 1969`) {
|
||||||
|
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(1234567890)
|
||||||
js, err := json.Marshal(ts)
|
js, err := json.Marshal(ts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
|
@ -18,7 +47,7 @@ func TestJSONTS(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if ts != ts2 {
|
if ts != ts2 {
|
||||||
t.Fatal(ts2)
|
t.Fatalf("want: %v, got: %v", ts, ts2)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := json.Unmarshal([]byte(`123`), &ts2); err != nil {
|
if err := json.Unmarshal([]byte(`123`), &ts2); err != nil {
|
||||||
|
|
|
||||||
31
todo.yaml
31
todo.yaml
|
|
@ -1,10 +1,12 @@
|
||||||
todo:
|
todo:
|
||||||
- todo: when to run scheduled modifier? like, syncthing could have conflicts if I modify only file on remote
|
- todo: when to run scheduled modifier? like, syncthing could have conflicts if I
|
||||||
|
modify only file on remote
|
||||||
|
ts: Fri Dec 31 22:33:12 EST 2021
|
||||||
details: |
|
details: |
|
||||||
- if it's a web ui hook or somethin, then it'd only have file conflict if I modify without waiting
|
- if it's a web ui hook or somethin, then it'd only have file conflict if I modify without waiting
|
||||||
- but thats a step back from current todo solution
|
- but thats a step back from current todo solution
|
||||||
|
|
||||||
- todo: ez edit on many platforms, even offline and mobile
|
- todo: ez edit on many platforms, even offline and mobile
|
||||||
|
ts: Fri Dec 31 22:33:12 EST 2021
|
||||||
details: |
|
details: |
|
||||||
mobile view + complete method
|
mobile view + complete method
|
||||||
collab editing of file prob resolves mobile and other stuff...
|
collab editing of file prob resolves mobile and other stuff...
|
||||||
|
|
@ -12,22 +14,35 @@ todo:
|
||||||
web server access to ops
|
web server access to ops
|
||||||
is a web ui for mobile best solution?
|
is a web ui for mobile best solution?
|
||||||
let git be smart-ish and keep latest? would provide versioning OOTB without touching raw
|
let git be smart-ish and keep latest? would provide versioning OOTB without touching raw
|
||||||
|
scheduled: []
|
||||||
done:
|
done:
|
||||||
- todo: crontab -e style editing to ensure good syntax
|
- ts to human readable
|
||||||
|
- here is my really here is my really here is my really here is my really here is
|
||||||
|
my really here is my really here is my really here is my really here is my really
|
||||||
|
here is my really here is my really here is my really here is my really here is
|
||||||
|
my really here is my really long string
|
||||||
|
- vim doesnt source vimrc, so stuff like tab width and tab characters, also syntax
|
||||||
|
highlight :(
|
||||||
|
- crontab -e style editing to ensure good syntax
|
||||||
- todo: YAML based todo
|
- todo: YAML based todo
|
||||||
details:
|
ts: Fri Dec 31 22:33:12 EST 2021
|
||||||
because goddamnit a year of this shit
|
details: because goddamnit a year of this shit isn't significant on disk or in RAM
|
||||||
isn't significant on disk or in RAM for vim
|
for vim
|
||||||
- todo: yaml based todo for plaintext
|
- todo: yaml based todo for plaintext
|
||||||
|
ts: Fri Dec 31 22:33:12 EST 2021
|
||||||
details: a year isnt even a mb
|
details: a year isnt even a mb
|
||||||
- ez edit, start on many platforms
|
- ez edit, start on many platforms
|
||||||
- defer
|
- defer
|
||||||
- schedule
|
- schedule
|
||||||
- looping
|
- looping
|
||||||
- details
|
- details
|
||||||
- todo: let UI be UI for whatever platform
|
- let UI be UI for whatever platform
|
||||||
- tags
|
- tags
|
||||||
- todo: sub tasks
|
- todo: sub tasks
|
||||||
|
ts: Fri Dec 31 22:33:12 EST 2021
|
||||||
subtasks:
|
subtasks:
|
||||||
- a
|
- a
|
||||||
|
- todo: crap losing on a bad edit hurts
|
||||||
|
ts: Fri Dec 31 22:37:58 EST 2021
|
||||||
|
details: |
|
||||||
|
?
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue