BREAKING CHANGE: The Task struct's Metadata field now maps to slices of strings (map[string][]string) instead of single strings. This allows tasks to have multiple values for the same metadata key. All code interacting with Metadata must be updated to handle slices. Version bumped to v3.0.0.
80 lines
1.9 KiB
Go
80 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
|
|
"git.donadeo.net/pdonadeo/todotxt2remind/v2/internal/parser"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var (
|
|
inputFile string
|
|
outputFile string
|
|
debug bool
|
|
version = "v3.0.0"
|
|
)
|
|
|
|
func main() {
|
|
rootCmd := &cobra.Command{
|
|
Use: "todotxt2remind",
|
|
Short: "Convert a todo.txt file to Remind format",
|
|
Long: "Reads tasks from todo.txt format and outputs Remind-compatible entries.",
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
var in io.Reader = os.Stdin
|
|
if inputFile != "" {
|
|
f, err := os.Open(inputFile)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "error opening input file: %v\n", err)
|
|
os.Exit(2)
|
|
}
|
|
defer func() { _ = f.Close() }()
|
|
in = f
|
|
}
|
|
|
|
tasks, errors := parser.ParseReader(in)
|
|
if len(errors) > 0 {
|
|
fmt.Fprintf(os.Stderr, "errors while parsing:\n")
|
|
for _, e := range errors {
|
|
fmt.Fprintf(os.Stderr, " - %v\n", e)
|
|
}
|
|
}
|
|
|
|
if debug {
|
|
b, _ := json.MarshalIndent(tasks, "", " ")
|
|
fmt.Fprintf(os.Stderr, "%s\n", string(b))
|
|
}
|
|
|
|
var out io.Writer = os.Stdout
|
|
if outputFile != "" {
|
|
f, err := os.Create(outputFile)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "error opening output file: %v\n", err)
|
|
os.Exit(2)
|
|
}
|
|
defer func() { _ = f.Close() }()
|
|
out = f
|
|
}
|
|
|
|
for _, t := range tasks {
|
|
rem := t.ToRemind()
|
|
if rem == "" {
|
|
continue
|
|
}
|
|
_, _ = fmt.Fprintf(out, "%s\n", rem)
|
|
}
|
|
},
|
|
}
|
|
|
|
rootCmd.Flags().StringVarP(&inputFile, "input", "i", "", "Input file (default: stdin)")
|
|
rootCmd.Flags().StringVarP(&outputFile, "output", "o", "", "Output file (default: stdout)")
|
|
rootCmd.Flags().BoolVar(&debug, "debug", false, "Print intermediate JSON to stderr")
|
|
rootCmd.Version = version
|
|
rootCmd.Flags().BoolP("version", "v", false, "Show version and exit")
|
|
rootCmd.SetVersionTemplate(fmt.Sprintf("todotxt2remind version %s\n", version))
|
|
|
|
_ = rootCmd.Execute()
|
|
}
|