bake listing files not cached in config
parent
8dacd8da5d
commit
d36f9e74b3
17
cmd/add.go
17
cmd/add.go
|
|
@ -18,23 +18,18 @@ func add(config *config) error {
|
|||
Schedule: pttodo.Schedule(config.addSchedule),
|
||||
Tags: config.addTags,
|
||||
}
|
||||
newTarget, err := _add(config.targets, v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
config.targets = append(config.targets, newTarget)
|
||||
return nil
|
||||
return _add(config.Targets()[0], v)
|
||||
}
|
||||
|
||||
func _add(filepaths []string, todo pttodo.Todo) (string, error) {
|
||||
target := filepaths[0] + ".todo." + uuid.New().String()
|
||||
func _add(filepath string, todo pttodo.Todo) error {
|
||||
target := filepath + ".todo." + uuid.New().String()
|
||||
|
||||
c, err := yaml.Marshal([]pttodo.Todo{todo})
|
||||
if err != nil {
|
||||
return "", err
|
||||
return err
|
||||
} else if err := ioutil.WriteFile(target, c, os.ModePerm); err != nil {
|
||||
return "", err
|
||||
return err
|
||||
}
|
||||
|
||||
return target, nil
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,11 +4,12 @@ import (
|
|||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type config struct {
|
||||
targets []string
|
||||
target string
|
||||
root string
|
||||
tags []string
|
||||
search string
|
||||
|
|
@ -25,8 +26,7 @@ func getConfig() config {
|
|||
}
|
||||
|
||||
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.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")
|
||||
|
|
@ -38,10 +38,37 @@ func getConfig() config {
|
|||
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)
|
||||
}
|
||||
|
||||
config.Targets()
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
func (config config) Targets() []string {
|
||||
result, err := config.targets()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (config config) targets() ([]string, error) {
|
||||
patterns := []string{config.target, fmt.Sprintf("%s.*", config.target)}
|
||||
if stat, err := os.Stat(config.target); err == nil && stat.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 {
|
||||
return []string{config.target}, nil
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
|
|
|||
16
cmd/dump.go
16
cmd/dump.go
|
|
@ -6,6 +6,7 @@ import (
|
|||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"gogs.inhome.blapointe.com/bel/pttodo/pttodo"
|
||||
|
|
@ -13,12 +14,25 @@ import (
|
|||
)
|
||||
|
||||
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 {
|
||||
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 {
|
||||
reader, err := filePathReader(filepath)
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ func edit(config *config) error {
|
|||
if !config.edit {
|
||||
return nil
|
||||
}
|
||||
return _edit(config.targets)
|
||||
return _edit(config.Targets())
|
||||
}
|
||||
|
||||
func _edit(filepaths []string) error {
|
||||
|
|
|
|||
Loading…
Reference in New Issue