Compare commits
64 Commits
ad9c32c987
...
v0.4.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d9cb376c11 | ||
|
|
47f46a8d78 | ||
|
|
f90124ca27 | ||
|
|
976f5bf073 | ||
|
|
ac3dc241f1 | ||
|
|
e913c9cd3c | ||
|
|
02e4c95d31 | ||
|
|
bbdd38fba7 | ||
|
|
4bdee5d9a0 | ||
|
|
7c84d7650c | ||
|
|
29646808f9 | ||
|
|
8276308d52 | ||
|
|
8c050d1d89 | ||
|
|
e8a83176aa | ||
|
|
7d98e6f0fc | ||
|
|
82a615919c | ||
|
|
50913b8913 | ||
|
|
42d532ee61 | ||
|
|
24316fe690 | ||
|
|
8058304219 | ||
|
|
7f24b3f337 | ||
|
|
fc12e0550d | ||
|
|
05f5244cd1 | ||
|
|
c559c8eba6 | ||
|
|
dd7ac8d786 | ||
|
|
ccdd2615c4 | ||
|
|
0241f2d76c | ||
|
|
fa812a16ee | ||
|
|
cf3b233a11 | ||
|
|
64868bdc0b | ||
|
|
1b1724bea7 | ||
|
|
f4968d1d1b | ||
|
|
6203ced79b | ||
|
|
4d171d10d0 | ||
|
|
8ad2ca9345 | ||
|
|
bc0bb05fb6 | ||
|
|
22dc04341c | ||
|
|
bf10c00708 | ||
|
|
cd5ce0f0df | ||
|
|
eb41f065a5 | ||
|
|
96bfb96ee3 | ||
|
|
770f2719d2 | ||
|
|
3554bf8ba4 | ||
|
|
b4aa4ad310 | ||
|
|
0dddf26265 | ||
|
|
5afdeed3b5 | ||
|
|
af0f094a65 | ||
|
|
031db8788b | ||
|
|
b8efdbfa52 | ||
|
|
a54ccae4c2 | ||
|
|
a27d5c38d7 | ||
|
|
cb4886992a | ||
|
|
15c5f03ccf | ||
|
|
b82f11c248 | ||
|
|
3c9b34202b | ||
|
|
967a02c90a | ||
|
|
3ed7d8cd9e | ||
|
|
492e0af993 | ||
|
|
10b95672e3 | ||
|
|
183f39bd2a | ||
|
|
24154df995 | ||
|
|
f7dac79233 | ||
|
|
8c45d4a7df | ||
|
|
1e3f24b4d5 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -4,5 +4,3 @@ cmd/cmd
|
||||
cmd/cli
|
||||
cmd/pttodo/pttodo
|
||||
cmd/pttodo-cli/pttodo-cli
|
||||
cmd/pttodo-cli
|
||||
pttodoer/target
|
||||
|
||||
36
cmd/add.go
36
cmd/add.go
@@ -1,36 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gitea.inhome.blapointe.com/gogs/pttodo/pttodo"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
func add(config *config) error {
|
||||
if config.add == "" {
|
||||
return nil
|
||||
}
|
||||
v := pttodo.Todo{
|
||||
Todo: config.add,
|
||||
Schedule: pttodo.Schedule(config.addSchedule),
|
||||
Tags: config.addTags,
|
||||
}
|
||||
return _add(config.Targets()[0], v)
|
||||
}
|
||||
|
||||
func _add(filepath string, todo pttodo.Todo) error {
|
||||
target := fmt.Sprintf("%s.todo.%s", filepath, uuid.New().String())
|
||||
|
||||
c, err := yaml.Marshal([]pttodo.Todo{todo})
|
||||
if err != nil {
|
||||
return err
|
||||
} else if err := ioutil.WriteFile(target, c, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,98 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type config struct {
|
||||
target string
|
||||
root string
|
||||
tags []string
|
||||
search string
|
||||
edit bool
|
||||
add string
|
||||
addSchedule string
|
||||
addTags string
|
||||
}
|
||||
|
||||
func getConfig() config {
|
||||
defaultFilepath := os.Getenv("PTTODO_FILE")
|
||||
if defaultFilepath == "" {
|
||||
defaultFilepath = "./todo.yaml"
|
||||
}
|
||||
|
||||
var config config
|
||||
flag.StringVar(&config.target, "f", defaultFilepath, "($PTTODO_FILE) path to yaml file or dir (starting with root then alphabetical for dir)")
|
||||
flag.StringVar(&config.root, "root", DUMP_TODO, "path to pretty print ("+fmt.Sprint([]string{DUMP_ALL, DUMP_TODO, DUMP_SCHEDULED, DUMP_DONE})+")")
|
||||
var tagss string
|
||||
flag.StringVar(&tagss, "tags", "", "csv of all tags to find, -x to invert")
|
||||
flag.StringVar(&config.search, "search", "", "fts case insensitive")
|
||||
flag.BoolVar(&config.edit, "e", false, "edit file")
|
||||
flag.StringVar(&config.add, "add", "", "todo to add")
|
||||
flag.StringVar(&config.addSchedule, "add-schedule", "", "todo to add schedule")
|
||||
flag.StringVar(&config.addTags, "add-tags", "", "todo to add csv tags")
|
||||
flag.Parse()
|
||||
|
||||
config.tags = strings.Split(tagss, ",")
|
||||
|
||||
config.Targets()
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
func (config config) Targets() []string {
|
||||
result, err := config.targets()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
sort.Strings(result)
|
||||
for i := range result {
|
||||
if path.Base(result[i]) == "root.yaml" {
|
||||
newresult := append([]string{result[i]}, result[:i]...)
|
||||
newresult = append(newresult, result[i+1:]...)
|
||||
result = newresult
|
||||
break
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (config config) targets() ([]string, error) {
|
||||
patterns := []string{config.target, fmt.Sprintf("%s.*", config.target)}
|
||||
isDir := false
|
||||
if stat, err := os.Stat(config.target); err == nil {
|
||||
isDir = stat.IsDir()
|
||||
}
|
||||
if isDir {
|
||||
patterns = []string{fmt.Sprintf("%s/*", config.target)}
|
||||
}
|
||||
|
||||
result := make([]string, 0, 1)
|
||||
for _, pattern := range patterns {
|
||||
results, err := filepath.Glob(pattern)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for i := range results {
|
||||
if stat, err := os.Stat(results[i]); err != nil {
|
||||
return nil, err
|
||||
} else if !stat.IsDir() {
|
||||
result = append(result, results[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(result) == 0 {
|
||||
if isDir {
|
||||
return []string{path.Join(config.target, "root.yaml")}, nil
|
||||
}
|
||||
return []string{config.target}, nil
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
@@ -1,87 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestConfigTargets(t *testing.T) {
|
||||
touch := func(t *testing.T, p string) {
|
||||
if err := os.WriteFile(p, nil, os.ModePerm); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
cases := map[string]struct {
|
||||
setup func(*testing.T, string)
|
||||
given string
|
||||
want []string
|
||||
}{
|
||||
"does not exist": {
|
||||
given: "x",
|
||||
want: []string{"x"},
|
||||
},
|
||||
"one file": {
|
||||
setup: func(t *testing.T, d string) {
|
||||
touch(t, path.Join(d, "x"))
|
||||
},
|
||||
given: "x",
|
||||
want: []string{"x"},
|
||||
},
|
||||
"two files": {
|
||||
setup: func(t *testing.T, d string) {
|
||||
touch(t, path.Join(d, "a"))
|
||||
touch(t, path.Join(d, "root.yaml"))
|
||||
touch(t, path.Join(d, "z"))
|
||||
},
|
||||
given: "",
|
||||
want: []string{"root.yaml", "a", "z"},
|
||||
},
|
||||
"empty dir": {
|
||||
setup: func(t *testing.T, d string) {
|
||||
os.Mkdir(path.Join(d, "x"), os.ModePerm)
|
||||
},
|
||||
given: "x",
|
||||
want: []string{"root.yaml"},
|
||||
},
|
||||
"dir": {
|
||||
setup: func(t *testing.T, d string) {
|
||||
os.Mkdir(path.Join(d, "x"), os.ModePerm)
|
||||
touch(t, path.Join(d, "x", "a"))
|
||||
touch(t, path.Join(d, "x", "a.todo.xyz"))
|
||||
touch(t, path.Join(d, "x", "b"))
|
||||
},
|
||||
given: "x",
|
||||
want: []string{"a", "a.todo.xyz", "b"},
|
||||
},
|
||||
}
|
||||
|
||||
for name, d := range cases {
|
||||
c := d
|
||||
t.Run(name, func(t *testing.T) {
|
||||
d := t.TempDir()
|
||||
if c.setup != nil {
|
||||
c.setup(t, d)
|
||||
}
|
||||
config := config{target: path.Join(d, c.given)}
|
||||
got := config.Targets()
|
||||
for i := range got {
|
||||
got[i] = path.Base(got[i])
|
||||
}
|
||||
for i := range c.want {
|
||||
c.want[i] = path.Base(c.want[i])
|
||||
}
|
||||
|
||||
t.Logf("want\n\t%+v, got \n\t%+v", c.want, got)
|
||||
if len(got) != len(c.want) {
|
||||
t.Error(c.want, got)
|
||||
}
|
||||
for i := range got {
|
||||
if got[i] != c.want[i] {
|
||||
t.Errorf("[%d] wanted %s, got %s", i, c.want[i], got[i])
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
44
cmd/dump.go
44
cmd/dump.go
@@ -1,44 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"gitea.inhome.blapointe.com/gogs/pttodo/pttodo"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
func dump(config config) error {
|
||||
return _dump(os.Stdout, config.Targets(), config.tags, config.search, config.root)
|
||||
}
|
||||
|
||||
func _dump(writer io.Writer, filepaths []string, tags []string, search, rootDisplay string) error {
|
||||
root, err := pttodo.NewRootFromFiles(filepaths...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, x := range []*[]pttodo.Todo{
|
||||
&root.Todo,
|
||||
&root.Scheduled,
|
||||
&root.Done,
|
||||
} {
|
||||
y := pttodo.Todos(*x)
|
||||
y = y.LikeTags(tags)
|
||||
y = y.LikeSearch(search)
|
||||
*x = y
|
||||
}
|
||||
|
||||
var v interface{} = root
|
||||
switch rootDisplay {
|
||||
case DUMP_TODO:
|
||||
v = root.Todo
|
||||
case DUMP_SCHEDULED:
|
||||
v = root.Scheduled
|
||||
case DUMP_DONE:
|
||||
v = root.Done
|
||||
default:
|
||||
}
|
||||
|
||||
return yaml.NewEncoder(writer).Encode(v)
|
||||
}
|
||||
262
cmd/edit.go
262
cmd/edit.go
@@ -1,262 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"sort"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"gitea.inhome.blapointe.com/gogs/pttodo/pttodo"
|
||||
"github.com/google/uuid"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
func edit(config *config) error {
|
||||
if !config.edit {
|
||||
return nil
|
||||
}
|
||||
return _edit(config.Targets())
|
||||
}
|
||||
|
||||
func _edit(filepaths []string) error {
|
||||
editableDir, err := inEditableDirAsTodos(filepaths)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := modifyEditableDir(editableDir, func() error {
|
||||
files, err := listDir(editableDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, p := range files {
|
||||
if _, err := pttodo.NewTodosFromFile(p); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
editedFiles, err := listDir(editableDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
edits := map[string]string{}
|
||||
for _, editedFile := range editedFiles {
|
||||
edits[path.Base(editedFile)] = editedFile
|
||||
|
||||
edited, err := pttodo.NewTodosFromFile(editedFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
original, err := pttodo.NewRootFromFile(func() string {
|
||||
for _, f := range filepaths {
|
||||
if path.Base(f) == path.Base(editedFile) {
|
||||
return f
|
||||
}
|
||||
}
|
||||
return "/dev/null"
|
||||
}())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
editedIDs := map[string]int{}
|
||||
for i := range edited {
|
||||
editedIDs[edited[i].ID()] = 1
|
||||
}
|
||||
for i := range original.Todo {
|
||||
if _, ok := editedIDs[original.Todo[i].ID()]; ok {
|
||||
continue
|
||||
}
|
||||
original.Todo[i].TS = pttodo.TS(time.Now().Unix())
|
||||
if string(original.Todo[i].Schedule) != "" && !original.Todo[i].Triggered() {
|
||||
original.Scheduled = append(original.Scheduled, original.Todo[i])
|
||||
}
|
||||
original.Done = append(original.Done, original.Todo[i])
|
||||
}
|
||||
original.Todo = edited
|
||||
original.AutoMove()
|
||||
|
||||
if err := func() error {
|
||||
f, err := os.Create(editedFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
return yaml.NewEncoder(f).Encode(original)
|
||||
}(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
dir := ""
|
||||
for _, f := range filepaths {
|
||||
if edited, ok := edits[path.Base(f)]; ok {
|
||||
if err := os.Rename(edited, f); err != nil {
|
||||
return err
|
||||
}
|
||||
delete(edits, path.Base(f))
|
||||
} else if err := os.Remove(f); err != nil {
|
||||
return err
|
||||
}
|
||||
dir = path.Dir(f)
|
||||
}
|
||||
|
||||
for base, editedFile := range edits {
|
||||
f := path.Join(dir, base)
|
||||
if _, err := os.Stat(f); err == nil {
|
||||
f = fmt.Sprintf("%s.todo.%s", f, uuid.New().String())
|
||||
}
|
||||
if err := os.Rename(editedFile, f); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func inEditableDirAsTodos(filepaths []string) (string, error) {
|
||||
tempDir, err := ioutil.TempDir(os.TempDir(), "edit-pttodo-*")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return tempDir, copyTodoToDir(tempDir, filepaths)
|
||||
}
|
||||
|
||||
func copyTodoToDir(d string, filepaths []string) error {
|
||||
inboxes := map[string][]string{}
|
||||
for _, target := range filepaths {
|
||||
p := path.Join(d, path.Base(target))
|
||||
if strings.Contains(path.Base(target), ".todo.") {
|
||||
p := path.Join(d, strings.Split(path.Base(p), ".todo")[0])
|
||||
inboxes[p] = append(inboxes[p], target)
|
||||
continue
|
||||
}
|
||||
if root, err := pttodo.NewRootFromFile(target); err != nil {
|
||||
return err
|
||||
} else if b, err := yaml.Marshal(root.Todo); err != nil {
|
||||
return err
|
||||
} else if err := os.WriteFile(p, b, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for p, inboxes := range inboxes {
|
||||
inboxRoot := pttodo.Root{}
|
||||
for _, inbox := range inboxes {
|
||||
subInboxRoot, err := pttodo.NewRootFromFile(inbox)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
inboxRoot.MergeIn(pttodo.Root{Todo: subInboxRoot.Todo})
|
||||
inboxRoot.MergeIn(pttodo.Root{Todo: subInboxRoot.Scheduled})
|
||||
}
|
||||
|
||||
root, err := pttodo.NewRootFromFile(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
root.MergeIn(pttodo.Root{Todo: inboxRoot.Todo})
|
||||
root.MergeIn(pttodo.Root{Todo: inboxRoot.Scheduled})
|
||||
|
||||
if b, err := yaml.Marshal(root.Todo); err != nil {
|
||||
return err
|
||||
} else if err := os.WriteFile(p, b, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func modifyEditableDir(d string, check func() error) error {
|
||||
for {
|
||||
if err := vimd(d); err != nil {
|
||||
return err
|
||||
}
|
||||
err := check()
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
log.Printf("%s, press <Enter> to resume editing", err)
|
||||
b := make([]byte, 1)
|
||||
if _, err := os.Stdin.Read(b); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func vimd(d string) error {
|
||||
bin := "vim"
|
||||
editorbin, err := exec.LookPath(bin)
|
||||
if err != nil {
|
||||
editorbin, err = exec.LookPath("vi")
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
args := []string{editorbin, "-p"}
|
||||
files, err := listDir(d)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
args = append(args, files...)
|
||||
cpid, err := syscall.ForkExec(
|
||||
editorbin,
|
||||
args,
|
||||
&syscall.ProcAttr{
|
||||
Dir: d,
|
||||
Env: os.Environ(),
|
||||
Files: []uintptr{os.Stdin.Fd(), os.Stdout.Fd(), os.Stderr.Fd()},
|
||||
Sys: nil,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
proc, err := os.FindProcess(cpid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
state, err := proc.Wait()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if exitCode := state.ExitCode(); exitCode != 0 {
|
||||
return fmt.Errorf("bad exit code on vim: %d, state: %+v", exitCode, state)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func listDir(dname string) ([]string, error) {
|
||||
entries, err := os.ReadDir(dname)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
paths := make([]string, 0, len(entries))
|
||||
for i := range entries {
|
||||
if entries[i].IsDir() {
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(path.Base(entries[i].Name()), ".") {
|
||||
continue
|
||||
}
|
||||
paths = append(paths, path.Join(dname, entries[i].Name()))
|
||||
}
|
||||
sort.Slice(paths, func(i, j int) bool {
|
||||
if path.Base(paths[i]) == "root.yaml" {
|
||||
return true
|
||||
}
|
||||
return paths[i] < paths[j]
|
||||
})
|
||||
return paths, nil
|
||||
}
|
||||
13
cmd/go.mod
13
cmd/go.mod
@@ -1,13 +0,0 @@
|
||||
module pttodo-cli
|
||||
|
||||
go 1.17
|
||||
|
||||
require (
|
||||
gitea.inhome.blapointe.com/gogs/pttodo v0.0.0-20231109151914-5d74b458d542
|
||||
github.com/google/uuid v1.3.0
|
||||
gopkg.in/yaml.v2 v2.4.0
|
||||
)
|
||||
|
||||
require github.com/robfig/cron/v3 v3.0.1 // indirect
|
||||
|
||||
replace gogs.inhome.blapointe.com/gogs/pttodo => ./..
|
||||
102
cmd/main.go
102
cmd/main.go
@@ -1,102 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
"gitea.inhome.blapointe.com/gogs/pttodo/pttodo"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
const (
|
||||
DUMP_ALL = "all"
|
||||
DUMP_TODO = "todo"
|
||||
DUMP_SCHEDULED = "scheduled"
|
||||
DUMP_DONE = "done"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := _main(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func _main() error {
|
||||
config := getConfig()
|
||||
if err := add(&config); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := edit(&config); err != nil {
|
||||
return err
|
||||
}
|
||||
return dump(config)
|
||||
}
|
||||
|
||||
func merge(filepath string, mergeTargetFilePath string) error {
|
||||
baseReader, err := filePathReader(filepath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
baseB, err := ioutil.ReadAll(baseReader)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
mergingReader, err := filePathReader(mergeTargetFilePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
mergingB, err := ioutil.ReadAll(mergingReader)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var base, merging pttodo.Root
|
||||
if err := yaml.Unmarshal(baseB, &base); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := yaml.Unmarshal(mergingB, &merging); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
base.MergeIn(merging)
|
||||
|
||||
tmppath, err := marshalRootToTempFile(base)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Rename(tmppath, filepath)
|
||||
}
|
||||
|
||||
func marshalRootToTempFile(root pttodo.Root) (string, error) {
|
||||
f, err := ioutil.TempFile(os.TempDir(), "tmp")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
f.Close()
|
||||
os.Remove(f.Name())
|
||||
b, err := yaml.Marshal(root)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
filepath := f.Name() + ".yaml"
|
||||
err = ioutil.WriteFile(filepath, b, os.ModePerm)
|
||||
return filepath, err
|
||||
}
|
||||
|
||||
func filePathReader(path string) (io.Reader, error) {
|
||||
if path == "-" {
|
||||
return os.Stdin, nil
|
||||
}
|
||||
b, err := ioutil.ReadFile(path)
|
||||
if os.IsNotExist(err) {
|
||||
return bytes.NewReader([]byte("{}")), nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return bytes.NewReader(b), nil
|
||||
}
|
||||
568
cmd/pttodo-cli/cli.go
Normal file
568
cmd/pttodo-cli/cli.go
Normal file
@@ -0,0 +1,568 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"local/pt-todo-server/pttodo"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"sort"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
const (
|
||||
DUMP_ALL = "all"
|
||||
DUMP_TODO = "todo"
|
||||
DUMP_SCHEDULED = "scheduled"
|
||||
DUMP_DONE = "done"
|
||||
)
|
||||
|
||||
type config struct {
|
||||
targets []string
|
||||
mergeme string
|
||||
root string
|
||||
tags []string
|
||||
search string
|
||||
edit bool
|
||||
dry bool
|
||||
add string
|
||||
addSchedule string
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := _main(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func _main() error {
|
||||
config := getConfig()
|
||||
if config.mergeme != "" {
|
||||
if err := merge(config.dry, config.targets[0], config.mergeme); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if config.add != "" {
|
||||
if err := add(config.dry, config.targets, pttodo.Todo{
|
||||
Todo: config.add,
|
||||
Schedule: pttodo.Schedule(config.addSchedule),
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if config.edit {
|
||||
if err := edit(config.dry, config.targets); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return dump(config.dry, os.Stdout, config.targets, config.tags, config.search, config.root)
|
||||
}
|
||||
|
||||
func getConfig() config {
|
||||
defaultFilepath, ok := os.LookupEnv("PTTODO_FILE")
|
||||
if !ok {
|
||||
defaultFilepath = "./todo.yaml"
|
||||
}
|
||||
|
||||
var config config
|
||||
var target string
|
||||
flag.StringVar(&target, "f", defaultFilepath, "($PTTODO_FILE) path to yaml file or dir (starting with root then alphabetical for dir)")
|
||||
flag.StringVar(&config.mergeme, "g", "", "path to yaml file to merge into -f (modifies f)")
|
||||
flag.StringVar(&config.root, "root", DUMP_TODO, "path to pretty print ("+fmt.Sprint([]string{DUMP_ALL, DUMP_TODO, DUMP_SCHEDULED, DUMP_DONE})+")")
|
||||
var tagss string
|
||||
flag.StringVar(&tagss, "tags", "", "csv of all tags to find, -tag to invert")
|
||||
flag.StringVar(&config.search, "search", "", "fts case insensitive")
|
||||
flag.BoolVar(&config.edit, "e", false, "edit file")
|
||||
flag.BoolVar(&config.dry, "dry", false, "dry run")
|
||||
flag.StringVar(&config.add, "add", "", "todo to add")
|
||||
flag.StringVar(&config.addSchedule, "add-schedule", "", "todo to add schedule")
|
||||
flag.Parse()
|
||||
|
||||
config.tags = strings.Split(tagss, ",")
|
||||
config.targets = []string{target}
|
||||
if stat, err := os.Stat(target); err == nil && stat.IsDir() {
|
||||
config.targets, _ = listDir(target)
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
func verifyRoot(root pttodo.Root) error {
|
||||
f, err := ioutil.TempFile(os.TempDir(), "tmp")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
f.Close()
|
||||
tempFile := f.Name()
|
||||
b, err := yaml.Marshal(root)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ioutil.WriteFile(tempFile, b, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
defer os.Remove(tempFile)
|
||||
return verifyFile(tempFile)
|
||||
}
|
||||
|
||||
func verifyFile(path string) error {
|
||||
if err := dump(true, io.Discard, []string{path}, nil, "", DUMP_ALL); err != nil {
|
||||
return fmt.Errorf("failed verifying file %s: %w", path, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func add(dry bool, filepaths []string, todo pttodo.Todo) error {
|
||||
target := filepaths[0]
|
||||
var original pttodo.Root
|
||||
|
||||
r, err := filePathReader(target)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := yaml.NewDecoder(r).Decode(&original); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
original.Todo = append([]pttodo.Todo{todo}, original.Todo...)
|
||||
original.AutoMove()
|
||||
|
||||
c, err := yaml.Marshal(original)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if dry {
|
||||
log.Printf("wouldve written...\n%s", c)
|
||||
return nil
|
||||
} else if err := ioutil.WriteFile(target, c, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func edit(dry bool, filepaths []string) error {
|
||||
tempDir, err := ioutil.TempDir(os.TempDir(), "edit-pttodo-*")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
originals := map[string]pttodo.Root{}
|
||||
for _, target := range filepaths {
|
||||
var original pttodo.Root
|
||||
if r, err := filePathReader(target); err != nil {
|
||||
return err
|
||||
} else if err := yaml.NewDecoder(r).Decode(&original); err != nil {
|
||||
return err
|
||||
} else if c, err := yaml.Marshal(original.Todo); err != nil {
|
||||
return err
|
||||
} else if err := ioutil.WriteFile(path.Join(tempDir, path.Base(target)), c, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
originals[target] = original
|
||||
}
|
||||
if err := vimd(tempDir); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, target := range filepaths {
|
||||
for {
|
||||
err := func() error {
|
||||
var todos []pttodo.Todo
|
||||
if r, err := filePathReader(path.Join(tempDir, path.Base(target))); err != nil {
|
||||
return err
|
||||
} else if err := yaml.NewDecoder(r).Decode(&todos); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}()
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
log.Printf("%s, press <Enter> to resume editing", err)
|
||||
b := make([]byte, 1)
|
||||
if _, err := os.Stdin.Read(b); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := vimd(tempDir); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
if dry {
|
||||
return nil
|
||||
}
|
||||
for _, target := range filepaths {
|
||||
r, err := filePathReader(path.Join(tempDir, path.Base(target)))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var newTodos []pttodo.Todo
|
||||
if err := yaml.NewDecoder(r).Decode(&newTodos); err != nil {
|
||||
return err
|
||||
}
|
||||
original := originals[target]
|
||||
|
||||
newTodoIDs := map[string]struct{}{}
|
||||
for i := range newTodos {
|
||||
newTodoIDs[newTodos[i].ID()] = struct{}{}
|
||||
}
|
||||
|
||||
for i := range original.Todo {
|
||||
if _, ok := newTodoIDs[original.Todo[i].ID()]; !ok {
|
||||
original.Todo[i].TS = pttodo.TS(time.Now().Unix())
|
||||
if string(original.Todo[i].Schedule) != "" && !original.Todo[i].Triggered() {
|
||||
original.Scheduled = append(original.Scheduled, original.Todo[i])
|
||||
}
|
||||
original.Done = append(original.Done, original.Todo[i])
|
||||
}
|
||||
}
|
||||
original.Todo = newTodos
|
||||
|
||||
original.AutoMove()
|
||||
|
||||
c, err := yaml.Marshal(original)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ioutil.WriteFile(target, c, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func _edit(dry bool, filepaths []string) error {
|
||||
tempDir, err := ioutil.TempDir(os.TempDir(), "edit-pttodo")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cpOne := func(filepath string) error {
|
||||
f, err := os.Create(path.Join(tempDir, path.Base(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()
|
||||
}
|
||||
f.Close()
|
||||
return nil
|
||||
}
|
||||
cp := func() error {
|
||||
for _, filepath := range filepaths {
|
||||
if err := cpOne(filepath); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
vi := func() error {
|
||||
return vimd(tempDir)
|
||||
}
|
||||
verifyOne := func(tempFile string) error {
|
||||
for {
|
||||
err := verifyFile(tempFile)
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
log.Printf("%v, press <Enter> to resume editing", err)
|
||||
b := make([]byte, 1)
|
||||
if _, err := os.Stdin.Read(b); err != nil {
|
||||
break
|
||||
}
|
||||
if err := vi(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return verifyFile(tempFile)
|
||||
}
|
||||
verify := func() error {
|
||||
for _, filepath := range filepaths {
|
||||
tempFile := path.Join(tempDir, path.Base(filepath))
|
||||
if err := verifyOne(tempFile); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
saveOne := func(filepath string) error {
|
||||
tempFile := path.Join(tempDir, path.Base(filepath))
|
||||
var rootTemp, rootOld pttodo.Root
|
||||
if a, err := filePathReader(tempFile); err != nil {
|
||||
return err
|
||||
} else if err := yaml.NewDecoder(a).Decode(&rootTemp); err != nil {
|
||||
return err
|
||||
} else if b, err := filePathReader(filepath); err != nil {
|
||||
return err
|
||||
} else if err := yaml.NewDecoder(b).Decode(&rootOld); err != nil {
|
||||
return err
|
||||
} else if rootTemp.Equals(rootOld) {
|
||||
//log.Printf("no changes to %s", filepath)
|
||||
return nil
|
||||
}
|
||||
if dry {
|
||||
log.Printf("would've saved %s as %s", tempFile, filepath)
|
||||
return nil
|
||||
}
|
||||
return os.Rename(tempFile, filepath)
|
||||
}
|
||||
save := func() error {
|
||||
for _, filepath := range filepaths {
|
||||
if err := saveOne(filepath); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, foo := range []func() error{cp, vi, verify, save} {
|
||||
if err := foo(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if !dry {
|
||||
os.RemoveAll(tempDir)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func vimd(d string) error {
|
||||
bin := "vim"
|
||||
editorbin, err := exec.LookPath(bin)
|
||||
if err != nil {
|
||||
editorbin, err = exec.LookPath("vi")
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
args := []string{editorbin, "-p"}
|
||||
files, err := listDir(d)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
args = append(args, files...)
|
||||
cpid, err := syscall.ForkExec(
|
||||
editorbin,
|
||||
args,
|
||||
&syscall.ProcAttr{
|
||||
Dir: d,
|
||||
Env: os.Environ(),
|
||||
Files: []uintptr{os.Stdin.Fd(), os.Stdout.Fd(), os.Stderr.Fd()},
|
||||
Sys: nil,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
proc, err := os.FindProcess(cpid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
state, err := proc.Wait()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if exitCode := state.ExitCode(); exitCode != 0 {
|
||||
return fmt.Errorf("bad exit code on vim: %d, state: %+v", exitCode, state)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func merge(dry bool, filepath string, mergeTargetFilePath string) error {
|
||||
baseReader, err := filePathReader(filepath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
baseB, err := ioutil.ReadAll(baseReader)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
mergingReader, err := filePathReader(mergeTargetFilePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
mergingB, err := ioutil.ReadAll(mergingReader)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var base, merging pttodo.Root
|
||||
if err := yaml.Unmarshal(baseB, &base); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := yaml.Unmarshal(mergingB, &merging); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
base.MergeIn(merging)
|
||||
|
||||
if err := verifyRoot(base); err != nil {
|
||||
return err
|
||||
}
|
||||
tmppath, err := marshalRootToTempFile(base)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if dry {
|
||||
log.Printf("would've moved %s to %s when adding %s", tmppath, filepath, mergeTargetFilePath)
|
||||
return nil
|
||||
}
|
||||
return os.Rename(tmppath, filepath)
|
||||
}
|
||||
|
||||
func marshalRootToTempFile(root pttodo.Root) (string, error) {
|
||||
f, err := ioutil.TempFile(os.TempDir(), "tmp")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
f.Close()
|
||||
os.Remove(f.Name())
|
||||
b, err := yaml.Marshal(root)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
filepath := f.Name() + ".yaml"
|
||||
err = ioutil.WriteFile(filepath, b, os.ModePerm)
|
||||
return filepath, err
|
||||
}
|
||||
|
||||
func dump(dry bool, writer io.Writer, filepaths []string, tags []string, search, rootDisplay string) error {
|
||||
var root pttodo.Root
|
||||
|
||||
for _, filepath := range filepaths {
|
||||
reader, err := filePathReader(filepath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
b, err := ioutil.ReadAll(reader)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var root2, root2post pttodo.Root
|
||||
if err := yaml.Unmarshal(b, &root2); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := yaml.Unmarshal(b, &root2post); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
root2.MoveScheduledToTodo()
|
||||
|
||||
if !dry {
|
||||
if !root2.Equals(root2post) {
|
||||
log.Printf("refreshing %s", filepath)
|
||||
b3, err := yaml.Marshal(root2)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.WriteFile(filepath, b3, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
//log.Printf("not refreshing %s", filepath)
|
||||
}
|
||||
}
|
||||
|
||||
root.MergeIn(root2)
|
||||
}
|
||||
|
||||
root.MoveScheduledToTodo()
|
||||
|
||||
var v interface{} = root
|
||||
switch rootDisplay {
|
||||
case DUMP_ALL:
|
||||
case DUMP_TODO:
|
||||
v = root.Todo
|
||||
case DUMP_SCHEDULED:
|
||||
v = root.Scheduled
|
||||
case DUMP_DONE:
|
||||
v = root.Done
|
||||
}
|
||||
if todos, ok := v.([]pttodo.Todo); ok {
|
||||
if len(tags) > 0 {
|
||||
result := make([]pttodo.Todo, 0, len(todos))
|
||||
for _, todo := range todos {
|
||||
skip := false
|
||||
for _, tag := range tags {
|
||||
positiveTag := strings.TrimLeft(tag, "-")
|
||||
hasTag := strings.Contains(todo.Tags, positiveTag)
|
||||
wantToHaveTag := !strings.HasPrefix(tag, "-")
|
||||
skip = skip || !(hasTag == wantToHaveTag)
|
||||
}
|
||||
if !skip {
|
||||
result = append(result, todo)
|
||||
}
|
||||
}
|
||||
todos = result
|
||||
}
|
||||
if len(search) > 0 {
|
||||
result := make([]pttodo.Todo, 0, len(todos))
|
||||
for _, todo := range todos {
|
||||
if strings.Contains(strings.ToLower(fmt.Sprint(todo)), strings.ToLower(search)) {
|
||||
result = append(result, todo)
|
||||
}
|
||||
}
|
||||
todos = result
|
||||
}
|
||||
v = todos
|
||||
}
|
||||
|
||||
b2, err := yaml.Marshal(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(writer, "%s\n", b2)
|
||||
return nil
|
||||
}
|
||||
|
||||
func filePathReader(path string) (io.Reader, error) {
|
||||
if path == "-" {
|
||||
return os.Stdin, nil
|
||||
}
|
||||
b, err := ioutil.ReadFile(path)
|
||||
if os.IsNotExist(err) {
|
||||
return bytes.NewReader([]byte("{}")), nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return bytes.NewReader(b), nil
|
||||
}
|
||||
|
||||
func listDir(dname string) ([]string, error) {
|
||||
entries, err := os.ReadDir(dname)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
paths := make([]string, 0, len(entries))
|
||||
for i := range entries {
|
||||
if entries[i].IsDir() {
|
||||
continue
|
||||
}
|
||||
paths = append(paths, path.Join(dname, entries[i].Name()))
|
||||
}
|
||||
sort.Slice(paths, func(i, j int) bool {
|
||||
if path.Base(paths[i]) == "root.yaml" {
|
||||
return true
|
||||
}
|
||||
return paths[i] < paths[j]
|
||||
})
|
||||
return paths, nil
|
||||
}
|
||||
12
cmd/pttodo-cli/go.mod
Normal file
12
cmd/pttodo-cli/go.mod
Normal file
@@ -0,0 +1,12 @@
|
||||
module pttodo-cli
|
||||
|
||||
go 1.17
|
||||
|
||||
require (
|
||||
gopkg.in/yaml.v2 v2.4.0
|
||||
local/pt-todo-server v0.0.0-00010101000000-000000000000
|
||||
)
|
||||
|
||||
require github.com/robfig/cron/v3 v3.0.1 // indirect
|
||||
|
||||
replace local/pt-todo-server => ../../
|
||||
@@ -1,7 +1,3 @@
|
||||
gitea.inhome.blapointe.com/gogs/pttodo v0.0.0-20231109151914-5d74b458d542 h1:eOWrA2hEQoyu413vbdXbEXHLXEX2TVBXjWawlWndFhg=
|
||||
gitea.inhome.blapointe.com/gogs/pttodo v0.0.0-20231109151914-5d74b458d542/go.mod h1:9CFZf/SSod0Z/8WvbmOI4gEzy6xGpBQAq8coVNncNvk=
|
||||
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
||||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
|
||||
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
0
cmd/testdata/1.yaml → cmd/pttodo-cli/testdata/1.yaml
vendored
Executable file → Normal file
0
cmd/testdata/1.yaml → cmd/pttodo-cli/testdata/1.yaml
vendored
Executable file → Normal file
0
cmd/testdata/2.yaml → cmd/pttodo-cli/testdata/2.yaml
vendored
Executable file → Normal file
0
cmd/testdata/2.yaml → cmd/pttodo-cli/testdata/2.yaml
vendored
Executable file → Normal file
0
cmd/testdata/3.yaml → cmd/pttodo-cli/testdata/3.yaml
vendored
Executable file → Normal file
0
cmd/testdata/3.yaml → cmd/pttodo-cli/testdata/3.yaml
vendored
Executable file → Normal file
4
cmd/pttodo-cli/testdata/root.yaml
vendored
Normal file
4
cmd/pttodo-cli/testdata/root.yaml
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
todo:
|
||||
- root
|
||||
scheduled: []
|
||||
done: []
|
||||
17
cmd/testdata/root.yaml
vendored
17
cmd/testdata/root.yaml
vendored
@@ -1,17 +0,0 @@
|
||||
todo:
|
||||
- root
|
||||
- stub
|
||||
scheduled:
|
||||
- todo: abc
|
||||
schedule: "2024-01-01"
|
||||
ts: Thu Nov 9 07:49:59 MST 2023
|
||||
- todo: "1"
|
||||
schedule: "2024-01-02"
|
||||
ts: Thu Nov 9 07:51:13 MST 2023
|
||||
- todo: "1"
|
||||
schedule: "2024-01-02"
|
||||
ts: Thu Nov 9 07:51:28 MST 2023
|
||||
- todo: "1"
|
||||
schedule: "2024-01-02"
|
||||
ts: Thu Nov 9 07:51:36 MST 2023
|
||||
done: []
|
||||
21
cmd/xfer/all_from_todo_server_to_pttodo.sh
Normal file
21
cmd/xfer/all_from_todo_server_to_pttodo.sh
Normal file
@@ -0,0 +1,21 @@
|
||||
#! /bin/bash
|
||||
|
||||
main() {
|
||||
cd "$(dirname "$BASH_SOURCE")"
|
||||
source ./from_todo_server_to_pttodo.sh
|
||||
type from_todo_server_to_pttodo_main
|
||||
list_lists | jq .list[] | jq -c . | while read -r line; do
|
||||
local name="$(echo "$line" | jq -r .name | sed 's/^Today$/todo/g' | tr '[:upper:]' '[:lower:]' | sed 's/ /-/g')"
|
||||
local id="$(echo "$line" | jq -r .id)"
|
||||
echo $name, $id
|
||||
TODO_SERVER_LIST="$id" from_todo_server_to_pttodo_main > ./$name.yaml
|
||||
done
|
||||
}
|
||||
|
||||
list_lists() {
|
||||
todo_server_curl "$TODO_SERVER_URL/ajax.php?loadLists=&rnd=0.9900282499544026"
|
||||
}
|
||||
|
||||
if [ "$0" == "$BASH_SOURCE" ]; then
|
||||
main "$@"
|
||||
fi
|
||||
55
cmd/xfer/from_todo_server_to_pttodo.sh
Normal file
55
cmd/xfer/from_todo_server_to_pttodo.sh
Normal file
@@ -0,0 +1,55 @@
|
||||
#! /bin/bash
|
||||
|
||||
export TODO_SERVER_URL="${TODO_SERVER_URL:-"https://todo-server.remote.blapointe.com"}"
|
||||
export TODO_SERVER_HEADERS="${TODO_SERVER_HEADERS:-"Cookie: BOAuthZ=$TODO_SERVER_BOAUTHZ"}"
|
||||
export TODO_SERVER_LIST="${TODO_SERVER_LIST:-"2548023766"}"
|
||||
|
||||
main() {
|
||||
from_todo_server_to_pttodo_main "$@"
|
||||
}
|
||||
|
||||
from_todo_server_to_pttodo_main() {
|
||||
set -e
|
||||
set -o pipefail
|
||||
local tasks_in_todo="$(fetch_tasks_in_list | format_tasks_in_list)"
|
||||
local schedule_tasks_in_flight="$(COMPL=1 LOOPING=1 fetch_tasks_in_list | format_tasks_in_list)"
|
||||
echo "{\"todo\": $tasks_in_todo, \"scheduled\": $schedule_tasks_in_flight, \"done\": []}" | yq -P eval -
|
||||
}
|
||||
|
||||
format_tasks_in_list() {
|
||||
jq -c .list[] | while read -r line; do
|
||||
echo "$line" \
|
||||
| jq '{
|
||||
todo: .title,
|
||||
details:.note,
|
||||
ts: (if .compl == 1 then (.dateCompleted | strptime("%d %b %Y %I:%M %p") | mktime) else .dateEditedInt end),
|
||||
subtasks: [],
|
||||
tags: .tags,
|
||||
schedule: (if (.cron != "") then (.cron) else (.loop) end)
|
||||
}' \
|
||||
| jq -c .
|
||||
done | jq -sc | yq -P eval - | grep -v -E ' (""|\[]|0s)$' | yq -j eval - | jq -c .
|
||||
}
|
||||
|
||||
fetch_tasks_in_list() {
|
||||
todo_server_curl "$TODO_SERVER_URL/ajax.php?loadTasks=&list=$TODO_SERVER_LIST&compl=${COMPL:-0}&looping=${LOOPING:-0}"
|
||||
}
|
||||
|
||||
todo_server_curl() {
|
||||
local csv_headers="$TODO_SERVER_HEADERS"
|
||||
local headers=()
|
||||
while [ "$csv_headers" != "" ]; do
|
||||
header="${csv_headers%%,*}"
|
||||
headers+=("-H" "${header%%:*}: ${header#*:}")
|
||||
if echo "$csv_headers" | grep -q ,; then
|
||||
csv_headers="${csv_headers#*,}"
|
||||
else
|
||||
csv_headers=""
|
||||
fi
|
||||
done
|
||||
curl -sS "${headers[@]}" "$@"
|
||||
}
|
||||
|
||||
if [ "$0" == "$BASH_SOURCE" ]; then
|
||||
main "$@"
|
||||
fi
|
||||
10
go.mod
10
go.mod
@@ -1,4 +1,4 @@
|
||||
module gitea.inhome.blapointe.com/gogs/pttodo
|
||||
module pttodo
|
||||
|
||||
go 1.17
|
||||
|
||||
@@ -6,3 +6,11 @@ require (
|
||||
github.com/robfig/cron/v3 v3.0.1
|
||||
gopkg.in/yaml.v2 v2.4.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/bytbox/go-pop3 v0.0.0-20120201222208-3046caf0763e // indirect
|
||||
github.com/emersion/go-imap v1.2.0 // indirect
|
||||
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21 // indirect
|
||||
golang.org/x/text v0.3.7 // indirect
|
||||
)
|
||||
|
||||
|
||||
12
go.sum
12
go.sum
@@ -1,5 +1,17 @@
|
||||
github.com/bytbox/go-pop3 v0.0.0-20120201222208-3046caf0763e h1:mQTN05gz0rDZSABqKMzAPMb5ATWcvvdMljRzEh0LjBo=
|
||||
github.com/bytbox/go-pop3 v0.0.0-20120201222208-3046caf0763e/go.mod h1:alXX+s7a4cKaIprgjeEboqi4Tm7XR/HXEwUTxUV/ywU=
|
||||
github.com/emersion/go-imap v1.2.0 h1:lyUQ3+EVM21/qbWE/4Ya5UG9r5+usDxlg4yfp3TgHFA=
|
||||
github.com/emersion/go-imap v1.2.0/go.mod h1:Qlx1FSx2FTxjnjWpIlVNEuX+ylerZQNFE5NsmKFSejY=
|
||||
github.com/emersion/go-message v0.15.0/go.mod h1:wQUEfE+38+7EW8p8aZ96ptg6bAb1iwdgej19uXASlE4=
|
||||
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21 h1:OJyUGMJTzHTd1XQp98QTaHernxMYzRaOasRir9hUlFQ=
|
||||
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
|
||||
github.com/emersion/go-textwrapper v0.0.0-20200911093747-65d896831594/go.mod h1:aqO8z8wPrjkscevZJFVE1wXJrLpC5LtJG7fqLOsPb2U=
|
||||
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
|
||||
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||
|
||||
@@ -1,58 +1,11 @@
|
||||
package pttodo
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
|
||||
yaml "gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
type Root struct {
|
||||
Todo []Todo
|
||||
Scheduled []Todo
|
||||
Done []Todo
|
||||
}
|
||||
|
||||
func NewRootFromFiles(p ...string) (Root, error) {
|
||||
var result Root
|
||||
for _, p := range p {
|
||||
subroot, err := NewRootFromFile(p)
|
||||
if err != nil {
|
||||
return Root{}, err
|
||||
}
|
||||
result.MergeIn(subroot)
|
||||
}
|
||||
result.AutoMove()
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func NewRootFromFile(p string) (Root, error) {
|
||||
if b, err := os.ReadFile(p); err == nil && len(bytes.TrimSpace(b)) == 0 {
|
||||
return Root{}, nil
|
||||
}
|
||||
|
||||
f, err := os.Open(p)
|
||||
if os.IsNotExist(err) {
|
||||
return Root{}, nil
|
||||
}
|
||||
if err != nil {
|
||||
return Root{}, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
var result Root
|
||||
if err := yaml.NewDecoder(f).Decode(&result); err != nil {
|
||||
todos, err2 := NewTodosFromFile(p)
|
||||
if err2 != nil {
|
||||
return Root{}, err
|
||||
}
|
||||
result.Todo = todos
|
||||
}
|
||||
|
||||
result.AutoMove()
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (root Root) Equals(other Root) bool {
|
||||
for i, slice := range [][2][]Todo{
|
||||
[2][]Todo{root.Todo, other.Todo},
|
||||
|
||||
@@ -4,8 +4,6 @@ import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -182,78 +180,3 @@ done:
|
||||
t.Fatalf("want\n\t%q, got\n\t%q", want, string(got))
|
||||
}
|
||||
}
|
||||
|
||||
func TestRootFromFile(t *testing.T) {
|
||||
cases := map[string]struct {
|
||||
given string
|
||||
want Root
|
||||
}{
|
||||
"empty": {},
|
||||
"happy": {
|
||||
given: `{"todo":["a", "b"],"scheduled":["c"], "done":[{"todo": "d"}]}`,
|
||||
want: Root{
|
||||
Todo: []Todo{
|
||||
Todo{Todo: "a"},
|
||||
Todo{Todo: "b"},
|
||||
},
|
||||
Scheduled: []Todo{
|
||||
Todo{Todo: "c"},
|
||||
},
|
||||
Done: []Todo{
|
||||
Todo{Todo: "d"},
|
||||
},
|
||||
},
|
||||
},
|
||||
"todos": {
|
||||
given: `["a", {"todo": "b"}]`,
|
||||
want: Root{
|
||||
Todo: []Todo{
|
||||
{Todo: "a"},
|
||||
{Todo: "b"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for name, d := range cases {
|
||||
c := d
|
||||
t.Run(name, func(t *testing.T) {
|
||||
d := t.TempDir()
|
||||
p := path.Join(d, "input.yaml")
|
||||
if err := os.WriteFile(p, []byte(c.given), os.ModePerm); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := NewRootFromFile(p)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if fmt.Sprintf("%+v", got) != fmt.Sprintf("%+v", c.want) {
|
||||
t.Errorf("want\n\t%+v, got\n\t%+v", c.want, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRootFromFiles(t *testing.T) {
|
||||
d := t.TempDir()
|
||||
ps := []string{
|
||||
path.Join(d, "a"),
|
||||
path.Join(d, "b"),
|
||||
}
|
||||
os.WriteFile(ps[0], []byte(`["a"]`), os.ModePerm)
|
||||
os.WriteFile(ps[1], []byte(`["b"]`), os.ModePerm)
|
||||
|
||||
got, err := NewRootFromFiles(ps...)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := Root{
|
||||
Todo: []Todo{
|
||||
{Todo: "a"},
|
||||
{Todo: "b"},
|
||||
},
|
||||
}
|
||||
if fmt.Sprintf("%+v", got) != fmt.Sprintf("%+v", want) {
|
||||
t.Errorf("want\n\t%+v, got \n\t%+v", want, got)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,10 +4,7 @@ import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"hash/crc32"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
yaml "gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
type Todo struct {
|
||||
@@ -20,24 +17,6 @@ type Todo struct {
|
||||
writeTS bool
|
||||
}
|
||||
|
||||
func NewTodosFromFile(p string) ([]Todo, error) {
|
||||
f, err := os.Open(p)
|
||||
if os.IsNotExist(err) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
var result []Todo
|
||||
if err := yaml.NewDecoder(f).Decode(&result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (todo Todo) ID() string {
|
||||
hash := crc32.NewIEEE()
|
||||
fmt.Fprintf(hash, "%d:%s", 0, todo.Todo)
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
package pttodo
|
||||
|
||||
import "strings"
|
||||
|
||||
type Todos []Todo
|
||||
|
||||
func (todos Todos) LikeSearch(search string) Todos {
|
||||
return todos.Like(func(todo Todo) bool {
|
||||
return strings.Contains(
|
||||
strings.ToLower(todo.Todo),
|
||||
strings.ToLower(search),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
func (todos Todos) LikeTags(tags []string) Todos {
|
||||
return todos.Like(func(todo Todo) bool {
|
||||
matches := true
|
||||
for _, tag := range tags {
|
||||
str := strings.TrimLeft(tag, "-")
|
||||
want := !strings.HasPrefix(tag, "-")
|
||||
matches = matches && strings.Contains(todo.Tags, str) == want
|
||||
}
|
||||
return matches
|
||||
})
|
||||
}
|
||||
|
||||
func (todos Todos) Like(like func(Todo) bool) Todos {
|
||||
result := make(Todos, 0)
|
||||
for i := range todos {
|
||||
if like(todos[i]) {
|
||||
result = append(result, todos[i])
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package pttodo
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestTodosLikeTags(t *testing.T) {
|
||||
todos := Todos{
|
||||
{Todo: "a", Tags: "x"},
|
||||
{Todo: "b", Tags: "x,y"},
|
||||
}
|
||||
|
||||
result := todos.LikeTags([]string{"x", "-y"})
|
||||
if len(result) != 1 {
|
||||
t.Error(result)
|
||||
} else if result[0].Todo != "a" {
|
||||
t.Error(result[0].Todo)
|
||||
}
|
||||
}
|
||||
7
pttodoer/Cargo.lock
generated
7
pttodoer/Cargo.lock
generated
@@ -1,7 +0,0 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "pttodoer"
|
||||
version = "0.1.0"
|
||||
@@ -1,8 +0,0 @@
|
||||
[package]
|
||||
name = "pttodoer"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
@@ -1,3 +0,0 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
4
pttodoer/src/testdata/.mvp.yaml.done
vendored
4
pttodoer/src/testdata/.mvp.yaml.done
vendored
@@ -1,4 +0,0 @@
|
||||
- todo: a b and c
|
||||
schedule: * * * * *
|
||||
ts: 123
|
||||
- d e f
|
||||
@@ -1 +0,0 @@
|
||||
plaintext
|
||||
@@ -1,4 +0,0 @@
|
||||
todo: todo here
|
||||
schedule: * * * * *
|
||||
details: |
|
||||
hello world
|
||||
2
pttodoer/src/testdata/mvp.yaml
vendored
2
pttodoer/src/testdata/mvp.yaml
vendored
@@ -1,2 +0,0 @@
|
||||
- x
|
||||
- y and z
|
||||
Reference in New Issue
Block a user