-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
93 lines (79 loc) · 1.76 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"database/sql"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/masakichi/tango/dict"
"github.com/masakichi/tango/utils"
_ "modernc.org/sqlite"
)
var (
appName = "tango"
version = "1.2.0-dev"
gitCommit string
buildDate string
)
var (
dataDir = utils.GetDataDir(appName)
databasePath = filepath.Join(dataDir, "tango.db")
)
func initDatabase() *sql.DB {
if _, err := os.Stat(dataDir); os.IsNotExist(err) {
os.MkdirAll(dataDir, os.ModePerm)
}
db, err := sql.Open("sqlite", databasePath)
if err != nil {
log.Fatal(err)
}
if _, err := os.Stat(databasePath); os.IsNotExist(err) {
dict.InitDictDB(db)
}
return db
}
func main() {
listFlag := flag.Bool("list", false, "list all dictionaries")
importFlag := flag.String("import", "", "import yomichan's dictionary zip file")
versionFlag := flag.Bool("version", false, "print tango app version")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "%s: A CLI Japanese Dictionary Tool\n", appName)
fmt.Fprintf(os.Stderr, "Usage: %s %s\n\n", os.Args[0], "単語")
fmt.Fprintf(os.Stderr, "Other Commands of %s:\n", os.Args[0])
flag.PrintDefaults()
}
flag.Parse()
if len(os.Args) < 2 {
flag.Usage()
return
}
db := initDatabase()
defer db.Close()
if !strings.HasPrefix(os.Args[1], "-") {
dict.PrintTerms(db)
return
}
if *importFlag != "" {
dict.ImportDictDB(db, *importFlag)
return
}
if *listFlag {
dict.PrintDicts(db)
return
}
if *versionFlag {
if gitCommit != "" {
version += "-" + gitCommit
}
if buildDate == "" {
buildDate = "unknown"
}
osArch := runtime.GOOS + "/" + runtime.GOARCH
versionString := fmt.Sprintf("%s %s %s BuildDate=%s",
appName, version, osArch, buildDate)
fmt.Println(versionString)
}
}