From 72765ff961f98f46b91ba9398aee92b65058ff42 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Fri, 30 Dec 2022 15:30:40 +0100 Subject: [PATCH] improve default output --- main.go | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index b3e1571..806d4e8 100644 --- a/main.go +++ b/main.go @@ -136,19 +136,41 @@ func main() { os.Exit(1) } + var defaultOutput string + fi, err := os.Stat(opt.Input) + if err != nil { + fmt.Fprintln(os.Stderr, err) + flag.Usage() + os.Exit(1) + } + inputBase := filepath.Clean(opt.Input) + if fi.IsDir() { + defaultOutput = fmt.Sprintf("%s.epub", inputBase) + } else { + ext := filepath.Ext(inputBase) + defaultOutput = fmt.Sprintf("%s.epub", inputBase[0:len(inputBase)-len(ext)]) + } + if opt.Output == "" { - fi, err := os.Stat(opt.Input) + opt.Output = defaultOutput + } + + if filepath.Ext(opt.Output) != ".epub" { + fo, err := os.Stat(opt.Output) if err != nil { fmt.Fprintln(os.Stderr, err) flag.Usage() os.Exit(1) } - if fi.IsDir() { - opt.Output = fmt.Sprintf("%s.epub", filepath.Clean(opt.Input)) - } else { - ext := filepath.Ext(opt.Input) - opt.Output = fmt.Sprintf("%s.epub", opt.Input[0:len(opt.Input)-len(ext)]) + if !fo.IsDir() { + fmt.Fprintln(os.Stderr, "output must be an existing dir or end with .epub") + flag.Usage() + os.Exit(1) } + opt.Output = filepath.Join( + opt.Output, + filepath.Base(defaultOutput), + ) } profileIdx, profileMatch := ProfilesIdx[opt.Profile]