1

adding interactive mode for command line newbs

This commit is contained in:
Alex Yatskov 2017-01-01 12:53:39 -08:00
parent ce5ce1c902
commit 7477a44e2b
5 changed files with 53 additions and 15 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "zero-epwing"]
path = zero-epwing
url = https://github.com/FooSoft/zero-epwing

Binary file not shown.

Binary file not shown.

64
main.go
View File

@ -23,6 +23,7 @@
package main
import (
"bufio"
"errors"
"flag"
"fmt"
@ -31,6 +32,7 @@ import (
"net/http"
"os"
"path"
"strings"
)
func usage() {
@ -75,38 +77,70 @@ func main() {
flag.Usage = usage
flag.Parse()
if flag.NArg() != 1 && flag.NArg() != 2 {
usage()
os.Exit(2)
}
var (
interactive bool
inputPath string
outputDir string
)
inputPath := flag.Arg(0)
if *format == "" {
if *format = detectFormat(inputPath); *format == "" {
log.Fatal("failed to detect dictionary format")
if flag.NArg() == 0 {
fmt.Print("Specify path of dictionary to convert: ")
reader := bufio.NewReader(os.Stdin)
inputPath, _ = reader.ReadString('\n')
inputPath = strings.Replace(inputPath, "\n", "", -1)
interactive = true
fmt.Print("\n")
} else {
inputPath = flag.Arg(0)
if flag.NArg() > 1 {
outputDir = flag.Arg(1)
}
}
var outputDir string
if flag.NArg() == 2 {
outputDir = flag.Arg(1)
} else {
terminate := func() {
if interactive {
fmt.Print("\nPress [ENTER] to terminate...")
reader := bufio.NewReader(os.Stdin)
reader.ReadString('\n')
}
os.Exit(1)
}
if _, err := os.Stat(inputPath); err != nil {
log.Printf("dictionary path '%s' does not exist", inputPath)
terminate()
}
if *format == "" {
if *format = detectFormat(inputPath); *format == "" {
log.Print("failed to detect dictionary format")
terminate()
}
}
if outputDir == "" {
var err error
outputDir, err = ioutil.TempDir("", "yomichan_tmp_")
if err != nil {
log.Fatal(err)
log.Print(err)
terminate()
}
*serve = true
}
if err := exportDb(inputPath, outputDir, *format, *title, *stride, *pretty); err != nil {
log.Fatal(err)
log.Print(err)
terminate()
}
if *serve {
if err := serveDb(outputDir, *port); err != nil {
log.Fatal(err)
log.Print(err)
terminate()
}
}
}

1
zero-epwing Submodule

@ -0,0 +1 @@
Subproject commit 82158b0104880d462d47d5a27108cfc31f00208c