-
Notifications
You must be signed in to change notification settings - Fork 0
/
importSubcmd.go
55 lines (43 loc) · 1.43 KB
/
importSubcmd.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
package main
import (
"database/sql"
"flag"
"fmt"
"os"
log "github.com/sirupsen/logrus"
)
// parse args of the import subcommand and exec it
func importSubcmd(args []string) {
importCmd := flag.NewFlagSet("import", flag.ExitOnError)
importCmd.SetOutput(flag.CommandLine.Output())
importCmd.Usage = func() {
fmt.Fprint(importCmd.Output(), "Import a word file into the database with optional tags\n\n")
fmt.Fprintln(importCmd.Output(), "Usage of orunmila import:")
fmt.Fprintf(importCmd.Output(), "orunmila [-db <db_path>] [-debug] import -tags [tag]... [filesname]...\n\n")
fmt.Fprintln(importCmd.Output(), " filename\n\tthe filename(s) to read the words from")
importCmd.PrintDefaults()
}
var (
tagsPtr = importCmd.String("tags", "", "a comma separated list of the tags to use")
)
importCmd.Parse(args)
if len(args) == 0 {
log.Error("[importSubcmd] you need to provide at least a filename")
importCmd.Usage()
os.Exit(1)
}
log.Println("performing an import on the given files:", importCmd.Args())
dsn := fmt.Sprintf("file:%s?mode=rw", *dbPtr)
db, err := sql.Open("sqlite3", dsn)
check(err)
defer db.Close()
Tags = stringToArray(*tagsPtr)
for i := 0; i < importCmd.NArg(); i++ {
log.Println("[importSubcmd] importing file:", importCmd.Arg(i))
if isFileExists(importCmd.Arg(i)) {
importFileWords(db, importCmd.Arg(i))
} else {
log.Warnf("[importSubcmd] %q does not exists.", importCmd.Arg(i))
}
}
}