From d36f9e74b3231e599c6f97ac10e4c5008e27684d Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Mon, 6 Nov 2023 12:25:52 -0700 Subject: [PATCH] bake listing files not cached in config --- cmd/add.go | 17 ++++++----------- cmd/config.go | 41 ++++++++++++++++++++++++++++++++++------- cmd/dump.go | 16 +++++++++++++++- cmd/edit.go | 2 +- 4 files changed, 56 insertions(+), 20 deletions(-) diff --git a/cmd/add.go b/cmd/add.go index 04745b1..5ac2bdd 100644 --- a/cmd/add.go +++ b/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 } diff --git a/cmd/config.go b/cmd/config.go index b9d629a..9976dd2 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -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 +} diff --git a/cmd/dump.go b/cmd/dump.go index f1e4f76..8c91eb2 100644 --- a/cmd/dump.go +++ b/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 { diff --git a/cmd/edit.go b/cmd/edit.go index 0e75d26..368484b 100644 --- a/cmd/edit.go +++ b/cmd/edit.go @@ -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 {