drop some args
This commit is contained in:
@@ -31,12 +31,10 @@ const (
|
|||||||
|
|
||||||
type config struct {
|
type config struct {
|
||||||
targets []string
|
targets []string
|
||||||
mergeme string
|
|
||||||
root string
|
root string
|
||||||
tags []string
|
tags []string
|
||||||
search string
|
search string
|
||||||
edit bool
|
edit bool
|
||||||
dry bool
|
|
||||||
add string
|
add string
|
||||||
addSchedule string
|
addSchedule string
|
||||||
addTags string
|
addTags string
|
||||||
@@ -50,27 +48,22 @@ func main() {
|
|||||||
|
|
||||||
func _main() error {
|
func _main() error {
|
||||||
config := getConfig()
|
config := getConfig()
|
||||||
if config.mergeme != "" {
|
|
||||||
if err := merge(config.dry, config.targets[0], config.mergeme); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if config.add != "" {
|
if config.add != "" {
|
||||||
v := pttodo.Todo{
|
v := pttodo.Todo{
|
||||||
Todo: config.add,
|
Todo: config.add,
|
||||||
Schedule: pttodo.Schedule(config.addSchedule),
|
Schedule: pttodo.Schedule(config.addSchedule),
|
||||||
Tags: config.addTags,
|
Tags: config.addTags,
|
||||||
}
|
}
|
||||||
if err := add(config.dry, config.targets, v); err != nil {
|
if err := add(config.targets, v); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if config.edit {
|
if config.edit {
|
||||||
if err := edit(config.dry, config.targets); err != nil {
|
if err := edit(config.targets); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return dump(config.dry, os.Stdout, config.targets, config.tags, config.search, config.root)
|
return dump(os.Stdout, config.targets, config.tags, config.search, config.root)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getConfig() config {
|
func getConfig() config {
|
||||||
@@ -82,13 +75,11 @@ func getConfig() config {
|
|||||||
var config config
|
var config config
|
||||||
var target string
|
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(&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})+")")
|
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")
|
||||||
flag.StringVar(&config.search, "search", "", "fts case insensitive")
|
flag.StringVar(&config.search, "search", "", "fts case insensitive")
|
||||||
flag.BoolVar(&config.edit, "e", false, "edit file")
|
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.add, "add", "", "todo to add")
|
||||||
flag.StringVar(&config.addSchedule, "add-schedule", "", "todo to add schedule")
|
flag.StringVar(&config.addSchedule, "add-schedule", "", "todo to add schedule")
|
||||||
flag.StringVar(&config.addTags, "add-tags", "", "todo to add csv tags")
|
flag.StringVar(&config.addTags, "add-tags", "", "todo to add csv tags")
|
||||||
@@ -122,13 +113,13 @@ func verifyRoot(root pttodo.Root) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func verifyFile(path string) error {
|
func verifyFile(path string) error {
|
||||||
if err := dump(true, io.Discard, []string{path}, nil, "", DUMP_ALL); err != nil {
|
if err := dump(io.Discard, []string{path}, nil, "", DUMP_ALL); err != nil {
|
||||||
return fmt.Errorf("failed verifying file %s: %w", path, err)
|
return fmt.Errorf("failed verifying file %s: %w", path, err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func add(dry bool, filepaths []string, todo pttodo.Todo) error {
|
func add(filepaths []string, todo pttodo.Todo) error {
|
||||||
target := filepaths[0]
|
target := filepaths[0]
|
||||||
var original pttodo.Root
|
var original pttodo.Root
|
||||||
|
|
||||||
@@ -146,9 +137,6 @@ func add(dry bool, filepaths []string, todo pttodo.Todo) error {
|
|||||||
c, err := yaml.Marshal(original)
|
c, err := yaml.Marshal(original)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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 {
|
} else if err := ioutil.WriteFile(target, c, os.ModePerm); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -156,7 +144,7 @@ func add(dry bool, filepaths []string, todo pttodo.Todo) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func edit(dry bool, filepaths []string) error {
|
func edit(filepaths []string) error {
|
||||||
tempDir, err := ioutil.TempDir(os.TempDir(), "edit-pttodo-*")
|
tempDir, err := ioutil.TempDir(os.TempDir(), "edit-pttodo-*")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -208,9 +196,6 @@ func edit(dry bool, filepaths []string) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if dry {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
for _, target := range filepaths {
|
for _, target := range filepaths {
|
||||||
r, err := filePathReader(path.Join(tempDir, path.Base(target)))
|
r, err := filePathReader(path.Join(tempDir, path.Base(target)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -255,7 +240,7 @@ func edit(dry bool, filepaths []string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func _edit(dry bool, filepaths []string) error {
|
func _edit(filepaths []string) error {
|
||||||
tempDir, err := ioutil.TempDir(os.TempDir(), "edit-pttodo")
|
tempDir, err := ioutil.TempDir(os.TempDir(), "edit-pttodo")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -330,10 +315,6 @@ func _edit(dry bool, filepaths []string) error {
|
|||||||
//log.Printf("no changes to %s", filepath)
|
//log.Printf("no changes to %s", filepath)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if dry {
|
|
||||||
log.Printf("would've saved %s as %s", tempFile, filepath)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return os.Rename(tempFile, filepath)
|
return os.Rename(tempFile, filepath)
|
||||||
}
|
}
|
||||||
save := func() error {
|
save := func() error {
|
||||||
@@ -350,9 +331,7 @@ func _edit(dry bool, filepaths []string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !dry {
|
os.RemoveAll(tempDir)
|
||||||
os.RemoveAll(tempDir)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -399,7 +378,7 @@ func vimd(d string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func merge(dry bool, filepath string, mergeTargetFilePath string) error {
|
func merge(filepath string, mergeTargetFilePath string) error {
|
||||||
baseReader, err := filePathReader(filepath)
|
baseReader, err := filePathReader(filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -435,10 +414,6 @@ func merge(dry bool, filepath string, mergeTargetFilePath string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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)
|
return os.Rename(tmppath, filepath)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -458,7 +433,7 @@ func marshalRootToTempFile(root pttodo.Root) (string, error) {
|
|||||||
return filepath, err
|
return filepath, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func dump(dry bool, 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 _, filepath := range filepaths {
|
for _, filepath := range filepaths {
|
||||||
@@ -482,19 +457,17 @@ func dump(dry bool, writer io.Writer, filepaths []string, tags []string, search,
|
|||||||
|
|
||||||
root2.MoveScheduledToTodo()
|
root2.MoveScheduledToTodo()
|
||||||
|
|
||||||
if !dry {
|
if !root2.Equals(root2post) {
|
||||||
if !root2.Equals(root2post) {
|
log.Printf("refreshing %s", filepath)
|
||||||
log.Printf("refreshing %s", filepath)
|
b3, err := yaml.Marshal(root2)
|
||||||
b3, err := yaml.Marshal(root2)
|
if err != nil {
|
||||||
if err != nil {
|
return err
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := os.WriteFile(filepath, b3, os.ModePerm); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
//log.Printf("not refreshing %s", filepath)
|
|
||||||
}
|
}
|
||||||
|
if err := os.WriteFile(filepath, b3, os.ModePerm); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
//log.Printf("not refreshing %s", filepath)
|
||||||
}
|
}
|
||||||
|
|
||||||
root.MergeIn(root2)
|
root.MergeIn(root2)
|
||||||
|
|||||||
Reference in New Issue
Block a user