diff --git a/epub.go b/epub.go index b93d2cd..ac93cd2 100644 --- a/epub.go +++ b/epub.go @@ -4,9 +4,8 @@ import ( "bytes" "fmt" "github.com/bmaupin/go-epub" - "io/ioutil" "os" - "path" + "path/filepath" "time" ) @@ -25,15 +24,15 @@ func (convert EpubConverter) Build(book Book) error { fmt.Println("正在生成epub") start := time.Now() // 写入样式 - tempDir, err := ioutil.TempDir("", "kaf-cli") + tempDir, err := os.MkdirTemp("", "kaf-cli") defer func() { if err := os.RemoveAll(tempDir); err != nil { panic(fmt.Sprintf("创建临时文件夹失败: %s", err)) } }() - pageStylesFile := path.Join(tempDir, "page_styles.css") + pageStylesFile := filepath.Join(tempDir, "page_styles.css") - err = ioutil.WriteFile(pageStylesFile, []byte(fmt.Sprintf(cssContent, book.Align, book.Bottom, book.Indent)), 0666) + err = os.WriteFile(pageStylesFile, []byte(fmt.Sprintf(cssContent, book.Align, book.Bottom, book.Indent)), 0666) if err != nil { return fmt.Errorf("无法写入样式文件: %w", err) } diff --git a/tools.go b/tools.go index e8fb6c9..7203370 100644 --- a/tools.go +++ b/tools.go @@ -2,12 +2,11 @@ package kafcli import ( "fmt" - "golang.org/x/sys/execabs" "io/ioutil" "math/rand" "os" "os/exec" - "path" + "path/filepath" "runtime" "strings" "time" @@ -27,6 +26,7 @@ func parseLang(lang string) string { func run(command string, args ...string) error { cmd := exec.Command(command, args...) cmd.Stderr = os.Stderr + cmd.Stdout = os.Stdout return cmd.Run() } @@ -35,16 +35,17 @@ func lookKindlegen() string { if runtime.GOOS == "windows" { command = "kindlegen.exe" } - kindlegen, err := execabs.LookPath(command) + kindlegen, err := exec.LookPath(command) if err != nil { currentDir, err := os.Executable() if err != nil { return "" } - kindlegen = path.Join(path.Dir(currentDir), command) + kindlegen = filepath.Join(filepath.Dir(currentDir), command) if exist, _ := isExists(kindlegen); !exist { return "" } + fmt.Println("kindlegen: ", kindlegen) } return kindlegen } @@ -54,7 +55,10 @@ func converToMobi(bookname, lang string) { fmt.Printf("\n检测到Kindle格式转换器: %s,正在把书籍转换成Kindle格式...\n", command) fmt.Println("转换mobi比较花时间, 大约耗时1-10分钟, 请等待...") start := time.Now() - run(command, "-dont_append_source", "-locale", lang, "-c1", bookname) + err := run(command, "-dont_append_source", "-locale", lang, "-c1", bookname) + if err != nil { + panic(err) + } // 计算耗时 end := time.Now().Sub(start) fmt.Println("转换为mobi格式耗时:", end)