Compare commits

...

15 Commits

Author SHA1 Message Date
Bel LaPointe a54ccae4c2 update todo 2021-12-31 23:00:16 -05:00
Bel LaPointe a27d5c38d7 times are now unix dates over ints 2021-12-31 22:59:13 -05:00
Bel LaPointe cb4886992a remove unused stub 2021-12-31 22:49:51 -05:00
Bel LaPointe 15c5f03ccf fix syntax highlight by using tempfile.yaml over tempfile 2021-12-31 22:46:49 -05:00
Bel LaPointe b82f11c248 dont try to find vimrc 2021-12-31 22:45:29 -05:00
Bel LaPointe 3c9b34202b use $EDITOR, default to vim, use $HOME/.vimrc if exists 2021-12-31 22:44:33 -05:00
Bel LaPointe 967a02c90a updated todo because i oofed 2021-12-31 22:37:47 -05:00
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
10 changed files with 160 additions and 39 deletions

View File

@ -7,6 +7,7 @@ import (
"io"
"io/ioutil"
"local/pt-todo-server/pttodo"
"log"
"os"
"os/exec"
"path"
@ -22,44 +23,59 @@ 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
tempFile = f.Name() + ".yaml"
return os.Rename(f.Name(), tempFile)
}
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 {
return err
}
args := []string{editorbin, tempFile}
cpid, err := syscall.ForkExec(
vibin,
[]string{vibin, tempFile},
editorbin,
args,
&syscall.ProcAttr{
Dir: "",
Env: os.Environ(),
@ -84,25 +100,32 @@ func edit(filepath string) error {
return nil
}
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 {
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 != "" {
os.Remove(tempFile)
}
return err
}
}
if !dry {
os.Remove(tempFile)
}
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 +148,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,7 +2,10 @@ package pttodo
import (
"encoding/json"
"errors"
"time"
yaml "gopkg.in/yaml.v2"
)
type TS int64
@ -23,9 +26,25 @@ func (ts TS) MarshalYAML() (interface{}, error) {
if ts == 0 {
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 {
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")
}

View File

@ -2,12 +2,41 @@ 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.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) {
ts := TS(time.Now().Unix())
ts := TS(1234567890)
js, err := json.Marshal(ts)
if err != nil {
t.Fatal(err)
@ -18,7 +47,7 @@ func TestJSONTS(t *testing.T) {
t.Fatal(err)
}
if ts != ts2 {
t.Fatal(ts2)
t.Fatalf("want: %v, got: %v", ts, ts2)
}
if err := json.Unmarshal([]byte(`123`), &ts2); err != nil {

View File

View File

@ -1,10 +1,12 @@
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: |
- 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
- todo: ez edit on many platforms, even offline and mobile
ts: Fri Dec 31 22:33:12 EST 2021
details: |
mobile view + complete method
collab editing of file prob resolves mobile and other stuff...
@ -12,22 +14,35 @@ todo:
web server access to ops
is a web ui for mobile best solution?
let git be smart-ish and keep latest? would provide versioning OOTB without touching raw
scheduled: []
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
details:
because goddamnit a year of this shit
isn't significant on disk or in RAM for vim
ts: Fri Dec 31 22:33:12 EST 2021
details: because goddamnit a year of this shit isn't significant on disk or in RAM
for vim
- todo: yaml based todo for plaintext
ts: Fri Dec 31 22:33:12 EST 2021
details: a year isnt even a mb
- ez edit, start on many platforms
- defer
- schedule
- looping
- details
- todo: let UI be UI for whatever platform
- let UI be UI for whatever platform
- tags
- todo: sub tasks
ts: Fri Dec 31 22:33:12 EST 2021
subtasks:
- a
- todo: crap losing on a bad edit hurts
ts: Fri Dec 31 22:37:58 EST 2021
details: |
?