Compare commits
9 Commits
7618d5665b
...
v0.2.1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e913c9cd3c | ||
|
|
02e4c95d31 | ||
|
|
bbdd38fba7 | ||
|
|
4bdee5d9a0 | ||
|
|
7c84d7650c | ||
|
|
29646808f9 | ||
|
|
8276308d52 | ||
|
|
8c050d1d89 | ||
|
|
e8a83176aa |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -3,4 +3,3 @@ todo-server-yaml
|
|||||||
cmd/cmd
|
cmd/cmd
|
||||||
cmd/cli
|
cmd/cli
|
||||||
cmd/pttodo/pttodo
|
cmd/pttodo/pttodo
|
||||||
cmd/pttodo-cli/pttodo-cli
|
|
||||||
|
|||||||
@@ -1,46 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"gogs.inhome.blapointe.com/bel/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, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
func _add(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 err := ioutil.WriteFile(target, c, os.ModePerm); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
529
cmd/pttodo-cli/cli.go
Normal file
529
cmd/pttodo-cli/cli.go
Normal file
@@ -0,0 +1,529 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
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.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.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 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 b, err := ioutil.ReadFile(target); err != nil && !os.IsNotExist(err) {
|
||||||
|
return err
|
||||||
|
} else if err := yaml.Unmarshal(b, &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 b, err := ioutil.ReadFile(path.Join(tempDir, path.Base(target))); err != nil {
|
||||||
|
return err
|
||||||
|
} else if err := yaml.Unmarshal(b, &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 {
|
||||||
|
b, err := ioutil.ReadFile(path.Join(tempDir, path.Base(target)))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
var newTodos []pttodo.Todo
|
||||||
|
if err := yaml.Unmarshal(b, &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])
|
||||||
|
} else {
|
||||||
|
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 := ioutil.ReadFile(tempFile); err != nil {
|
||||||
|
return err
|
||||||
|
} else if err := yaml.Unmarshal(a, &rootTemp); err != nil {
|
||||||
|
return err
|
||||||
|
} else if b, err := ioutil.ReadFile(filepath); err != nil {
|
||||||
|
return err
|
||||||
|
} else if err := yaml.Unmarshal(b, &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) {
|
||||||
|
var reader io.Reader
|
||||||
|
if path == "-" {
|
||||||
|
reader = os.Stdin
|
||||||
|
} else {
|
||||||
|
b, err := ioutil.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
reader = bytes.NewReader(b)
|
||||||
|
}
|
||||||
|
return reader, 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
|
||||||
|
}
|
||||||
@@ -1,106 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"gogs.inhome.blapointe.com/bel/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 {
|
|
||||||
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 !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
|
|
||||||
}
|
|
||||||
@@ -1,161 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
"os/exec"
|
|
||||||
"path"
|
|
||||||
"syscall"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
"gogs.inhome.blapointe.com/bel/pttodo/pttodo"
|
|
||||||
"gopkg.in/yaml.v2"
|
|
||||||
)
|
|
||||||
|
|
||||||
func edit(config config) error {
|
|
||||||
if !config.edit {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return _edit(config.targets)
|
|
||||||
}
|
|
||||||
|
|
||||||
func _edit(filepaths []string) error {
|
|
||||||
tempDir, err := ioutil.TempDir(os.TempDir(), "edit-pttodo-*")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
originals := map[string]pttodo.Root{}
|
|
||||||
lastModified := map[string]time.Time{}
|
|
||||||
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 info, _ := os.Stat(target); info != nil {
|
|
||||||
lastModified[target] = info.ModTime()
|
|
||||||
} else {
|
|
||||||
lastModified[target] = time.Time{}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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
|
|
||||||
}
|
|
||||||
outputPath := target
|
|
||||||
if info, _ := os.Stat(target); info != nil && info.ModTime() != lastModified[target] {
|
|
||||||
outputPath = fmt.Sprintf("%s.%s.%s", outputPath, uuid.New().String(), path.Ext(outputPath))
|
|
||||||
}
|
|
||||||
if err := ioutil.WriteFile(outputPath, c, os.ModePerm); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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
|
|
||||||
}
|
|
||||||
@@ -3,13 +3,10 @@ module pttodo-cli
|
|||||||
go 1.17
|
go 1.17
|
||||||
|
|
||||||
require (
|
require (
|
||||||
gogs.inhome.blapointe.com/bel/pttodo v0.4.0
|
|
||||||
gopkg.in/yaml.v2 v2.4.0
|
gopkg.in/yaml.v2 v2.4.0
|
||||||
|
local/pt-todo-server v0.0.0-00010101000000-000000000000
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require github.com/robfig/cron/v3 v3.0.1 // indirect
|
||||||
github.com/google/uuid v1.3.0 // indirect
|
|
||||||
github.com/robfig/cron/v3 v3.0.1 // indirect
|
|
||||||
)
|
|
||||||
|
|
||||||
replace gogs.inhome.blapointe.com/bel/pttodo => ../..
|
replace local/pt-todo-server => ../../
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
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 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
|
||||||
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
|
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=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
|
|||||||
@@ -1,198 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"path"
|
|
||||||
"sort"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"gogs.inhome.blapointe.com/bel/pttodo/pttodo"
|
|
||||||
|
|
||||||
"gopkg.in/yaml.v2"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
DUMP_ALL = "all"
|
|
||||||
DUMP_TODO = "todo"
|
|
||||||
DUMP_SCHEDULED = "scheduled"
|
|
||||||
DUMP_DONE = "done"
|
|
||||||
)
|
|
||||||
|
|
||||||
type config struct {
|
|
||||||
targets []string
|
|
||||||
root string
|
|
||||||
tags []string
|
|
||||||
search string
|
|
||||||
edit bool
|
|
||||||
add string
|
|
||||||
addSchedule string
|
|
||||||
addTags string
|
|
||||||
}
|
|
||||||
|
|
||||||
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 getConfig() config {
|
|
||||||
defaultFilepath := os.Getenv("PTTODO_FILE")
|
|
||||||
if defaultFilepath == "" {
|
|
||||||
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.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 = []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(io.Discard, []string{path}, nil, "", DUMP_ALL); err != nil {
|
|
||||||
return fmt.Errorf("failed verifying file %s: %w", path, err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
|
|
||||||
if err := verifyRoot(base); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
49
cmd/pttodo-cli/testdata/test.yaml
vendored
49
cmd/pttodo-cli/testdata/test.yaml
vendored
@@ -1,10 +1,45 @@
|
|||||||
todo:
|
todo:
|
||||||
- hi20890
|
- todo: b
|
||||||
- hi
|
ts: Thu Mar 24 09:03:24 MDT 2022
|
||||||
- b
|
scheduled:
|
||||||
|
- todo: due
|
||||||
|
schedule: "2022-05-01"
|
||||||
|
ts: Thu Mar 24 09:16:21 MDT 2022
|
||||||
- todo: loop
|
- todo: loop
|
||||||
schedule: 10s
|
schedule: 10s
|
||||||
- todo: past
|
ts: Thu Mar 24 09:17:11 MDT 2022
|
||||||
schedule: "2000-01-02"
|
done:
|
||||||
scheduled: []
|
- todo: abc
|
||||||
done: []
|
schedule: "2022-05-01"
|
||||||
|
ts: Wed Mar 23 08:03:45 MDT 2022
|
||||||
|
- todo: def
|
||||||
|
schedule: "2022-05-01"
|
||||||
|
ts: Wed Mar 23 08:04:05 MDT 2022
|
||||||
|
- todo: other
|
||||||
|
ts: Wed Mar 23 09:44:57 MDT 2022
|
||||||
|
- todo: b
|
||||||
|
ts: Wed Mar 23 09:45:13 MDT 2022
|
||||||
|
- todo: a
|
||||||
|
ts: Wed Mar 23 09:45:15 MDT 2022
|
||||||
|
- todo: a
|
||||||
|
ts: Wed Mar 23 09:45:24 MDT 2022
|
||||||
|
- todo: a
|
||||||
|
ts: Wed Mar 23 09:45:31 MDT 2022
|
||||||
|
- todo: looper
|
||||||
|
schedule: 15s
|
||||||
|
ts: Thu Mar 24 09:01:41 MDT 2022
|
||||||
|
- todo: looper
|
||||||
|
schedule: 15s
|
||||||
|
ts: Thu Mar 24 09:04:51 MDT 2022
|
||||||
|
- todo: loop
|
||||||
|
schedule: 10s
|
||||||
|
ts: Thu Mar 24 09:06:59 MDT 2022
|
||||||
|
- todo: due
|
||||||
|
schedule: "2022-02-01"
|
||||||
|
ts: Thu Mar 24 09:15:42 MDT 2022
|
||||||
|
- todo: due
|
||||||
|
schedule: "2022-02-01"
|
||||||
|
ts: Thu Mar 24 09:16:53 MDT 2022
|
||||||
|
- todo: due
|
||||||
|
schedule: "2022-03-01"
|
||||||
|
ts: Thu Mar 24 09:17:07 MDT 2022
|
||||||
|
|||||||
10
go.mod
10
go.mod
@@ -1,4 +1,4 @@
|
|||||||
module gogs.inhome.blapointe.com/bel/pttodo
|
module pttodo
|
||||||
|
|
||||||
go 1.17
|
go 1.17
|
||||||
|
|
||||||
@@ -6,3 +6,11 @@ require (
|
|||||||
github.com/robfig/cron/v3 v3.0.1
|
github.com/robfig/cron/v3 v3.0.1
|
||||||
gopkg.in/yaml.v2 v2.4.0
|
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 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
|
||||||
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
|
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 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||||
|
|||||||
@@ -22,19 +22,18 @@ func (root Root) Equals(other Root) bool {
|
|||||||
|
|
||||||
func (root *Root) AutoMove() {
|
func (root *Root) AutoMove() {
|
||||||
root.MoveScheduledToTodo()
|
root.MoveScheduledToTodo()
|
||||||
root.MoveTodoToScheduled()
|
//root.MoveTodoToScheduled()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (root *Root) MoveTodoToScheduled() {
|
func (root *Root) MoveTodoToScheduled() {
|
||||||
for i := len(root.Todo) - 1; i >= 0; i-- {
|
for i := len(root.Todo) - 1; i >= 0; i-- {
|
||||||
if !root.Todo[i].Schedule.isFixedFuture() {
|
if root.Todo[i].Schedule != "" && !root.Todo[i].Triggered() {
|
||||||
continue
|
root.Scheduled = append(root.Scheduled, root.Todo[i])
|
||||||
|
for j := i; j < len(root.Todo)-1; j++ {
|
||||||
|
root.Todo[j] = root.Todo[j+1]
|
||||||
|
}
|
||||||
|
root.Todo = root.Todo[:len(root.Todo)-1]
|
||||||
}
|
}
|
||||||
root.Scheduled = append(root.Scheduled, root.Todo[i])
|
|
||||||
for j := i; j < len(root.Todo)-1; j++ {
|
|
||||||
root.Todo[j] = root.Todo[j+1]
|
|
||||||
}
|
|
||||||
root.Todo = root.Todo[:len(root.Todo)-1]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,42 +25,6 @@ func (schedule Schedule) Next(t time.Time) (time.Time, error) {
|
|||||||
return scheduler.next(t)
|
return scheduler.next(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (schedule Schedule) isFixedFuture() bool {
|
|
||||||
if !schedule.isFixed() {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return schedule.isFuture()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (schedule Schedule) isFixed() bool {
|
|
||||||
if schedule.empty() {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
scheduler := schedulerFactory(string(schedule))
|
|
||||||
switch scheduler.(type) {
|
|
||||||
case scheduleEZDate, scheduleDue:
|
|
||||||
default:
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (schedule Schedule) isFuture() bool {
|
|
||||||
if schedule.empty() {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
scheduler := schedulerFactory(string(schedule))
|
|
||||||
t, err := scheduler.next(time.Now())
|
|
||||||
if err != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return time.Now().Before(t)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (schedule Schedule) empty() bool {
|
|
||||||
return string(schedule) == ""
|
|
||||||
}
|
|
||||||
|
|
||||||
type scheduler interface {
|
type scheduler interface {
|
||||||
next(time.Time) (time.Time, error)
|
next(time.Time) (time.Time, error)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -130,60 +130,3 @@ func TestSchedulerFactory(t *testing.T) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestScheduleIsFixedFuture(t *testing.T) {
|
|
||||||
cases := map[string]struct {
|
|
||||||
input string
|
|
||||||
want bool
|
|
||||||
}{
|
|
||||||
"empty": {
|
|
||||||
input: "",
|
|
||||||
want: false,
|
|
||||||
},
|
|
||||||
"cron": {
|
|
||||||
input: "* * * * *",
|
|
||||||
want: false,
|
|
||||||
},
|
|
||||||
"duration": {
|
|
||||||
input: "1m",
|
|
||||||
want: false,
|
|
||||||
},
|
|
||||||
"due past": {
|
|
||||||
input: "123",
|
|
||||||
want: false,
|
|
||||||
},
|
|
||||||
"due future": {
|
|
||||||
input: "9648154541",
|
|
||||||
want: true,
|
|
||||||
},
|
|
||||||
"ez date short past": {
|
|
||||||
input: "2000-01-02",
|
|
||||||
want: false,
|
|
||||||
},
|
|
||||||
"ez date short future": {
|
|
||||||
input: "2100-01-02",
|
|
||||||
want: true,
|
|
||||||
},
|
|
||||||
"ez date long past": {
|
|
||||||
input: "2000-01-02T01",
|
|
||||||
want: false,
|
|
||||||
},
|
|
||||||
"ez date long future": {
|
|
||||||
input: "2100-01-02T02",
|
|
||||||
want: true,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for name, d := range cases {
|
|
||||||
c := d
|
|
||||||
t.Run(name, func(t *testing.T) {
|
|
||||||
schedule := Schedule(c.input)
|
|
||||||
got := schedule.isFixedFuture()
|
|
||||||
if got != c.want {
|
|
||||||
gotFuture := schedule.isFuture()
|
|
||||||
gotFixed := schedule.isFixed()
|
|
||||||
t.Errorf("want %v, got %v = %v && %v", c.want, got, gotFuture, gotFixed)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user