Compare commits
4 Commits
23ef7f13ee
...
92e8e14c08
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
92e8e14c08 | ||
|
|
84aa7cb319 | ||
|
|
d36f9e74b3 | ||
|
|
8dacd8da5d |
17
cmd/add.go
17
cmd/add.go
@@ -18,23 +18,18 @@ func add(config *config) error {
|
|||||||
Schedule: pttodo.Schedule(config.addSchedule),
|
Schedule: pttodo.Schedule(config.addSchedule),
|
||||||
Tags: config.addTags,
|
Tags: config.addTags,
|
||||||
}
|
}
|
||||||
newTarget, err := _add(config.targets, v)
|
return _add(config.Targets()[0], v)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
config.targets = append(config.targets, newTarget)
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func _add(filepaths []string, todo pttodo.Todo) (string, error) {
|
func _add(filepath string, todo pttodo.Todo) error {
|
||||||
target := filepaths[0] + ".todo." + uuid.New().String()
|
target := filepath + ".todo." + uuid.New().String()
|
||||||
|
|
||||||
c, err := yaml.Marshal([]pttodo.Todo{todo})
|
c, err := yaml.Marshal([]pttodo.Todo{todo})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return err
|
||||||
} else if err := ioutil.WriteFile(target, c, os.ModePerm); err != nil {
|
} else if err := ioutil.WriteFile(target, c, os.ModePerm); err != nil {
|
||||||
return "", err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return target, nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,11 +4,14 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
|
"path/filepath"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type config struct {
|
type config struct {
|
||||||
targets []string
|
target string
|
||||||
root string
|
root string
|
||||||
tags []string
|
tags []string
|
||||||
search string
|
search string
|
||||||
@@ -25,8 +28,7 @@ func getConfig() config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var config config
|
var config config
|
||||||
var target string
|
flag.StringVar(&config.target, "f", defaultFilepath, "($PTTODO_FILE) path to yaml file or dir (starting with root then alphabetical for dir)")
|
||||||
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})+")")
|
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
|
var tagss string
|
||||||
flag.StringVar(&tagss, "tags", "", "csv of all tags to find, -x to invert")
|
flag.StringVar(&tagss, "tags", "", "csv of all tags to find, -x to invert")
|
||||||
@@ -38,10 +40,53 @@ func getConfig() config {
|
|||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
config.tags = strings.Split(tagss, ",")
|
config.tags = strings.Split(tagss, ",")
|
||||||
config.targets = []string{target}
|
|
||||||
if stat, err := os.Stat(target); err == nil && stat.IsDir() {
|
config.Targets()
|
||||||
config.targets, _ = listDir(target)
|
|
||||||
}
|
|
||||||
|
|
||||||
return config
|
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
|
||||||
|
}
|
||||||
|
result = append(result, results...)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(result) == 0 {
|
||||||
|
if isDir {
|
||||||
|
return []string{path.Join(config.target, "root.yaml")}, nil
|
||||||
|
}
|
||||||
|
return []string{config.target}, nil
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|||||||
87
cmd/config_test.go
Normal file
87
cmd/config_test.go
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
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, "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])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
16
cmd/dump.go
16
cmd/dump.go
@@ -6,6 +6,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"gogs.inhome.blapointe.com/bel/pttodo/pttodo"
|
"gogs.inhome.blapointe.com/bel/pttodo/pttodo"
|
||||||
@@ -13,12 +14,25 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func dump(config config) error {
|
func dump(config config) error {
|
||||||
return _dump(os.Stdout, config.targets, config.tags, config.search, config.root)
|
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 {
|
func _dump(writer io.Writer, filepaths []string, tags []string, search, rootDisplay string) error {
|
||||||
var root pttodo.Root
|
var root pttodo.Root
|
||||||
|
|
||||||
|
for _, p := range filepaths {
|
||||||
|
results, err := filepath.Glob(p + ".*")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, result := range results {
|
||||||
|
if result == p {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
filepaths = append(filepaths, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for _, filepath := range filepaths {
|
for _, filepath := range filepaths {
|
||||||
reader, err := filePathReader(filepath)
|
reader, err := filePathReader(filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -15,11 +15,11 @@ import (
|
|||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func edit(config config) error {
|
func edit(config *config) error {
|
||||||
if !config.edit {
|
if !config.edit {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return _edit(config.targets)
|
return _edit(config.Targets())
|
||||||
}
|
}
|
||||||
|
|
||||||
func _edit(filepaths []string) error {
|
func _edit(filepaths []string) error {
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ func _main() error {
|
|||||||
if err := add(&config); err != nil {
|
if err := add(&config); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := edit(config); err != nil {
|
if err := edit(&config); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return dump(config)
|
return dump(config)
|
||||||
|
|||||||
Reference in New Issue
Block a user