improve default output

This commit is contained in:
Celogeek 2022-12-30 15:30:40 +01:00
parent cae2f01dba
commit 72765ff961
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A

34
main.go
View File

@ -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]