- Add --input/-i and --output/-o flags for file selection - Add --debug flag to print intermediate JSON to stderr - Print errors during parsing
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/internal/parser"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var (
|
|
inputFile string
|
|
outputFile string
|
|
debug bool
|
|
version = "1"
|
|
)
|
|
|
|
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 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()
|
|
}
|