/
fscan.go
93 lines (76 loc) · 1.74 KB
/
fscan.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
// fscan.go - display new rss feeds
package main
import (
"fmt"
"os"
"strconv"
"time"
"github.com/dhn/feedme/lib/config"
"github.com/dhn/feedme/thirdparty/getopt"
"github.com/dhn/feedme/thirdparty/sqlite3"
"github.com/dhn/feedme/thirdparty/strftime"
)
// A Args set getopt arguments
type Args struct {
limit int
}
var cursor *sqlite3.Conn
func checkFileExist(filename string) bool {
if _, err := os.Stat(filename); os.IsNotExist(err) {
return false
}
return true
}
func initSQL() {
if !checkFileExist(config.SQLDatabase) {
fmt.Printf("%s not found, please run first ffetch\n", config.SQLDatabase)
} else {
cursor, _ = sqlite3.Open(config.SQLDatabase)
}
}
func showSQL(limit int) {
query := "SELECT * from feed ORDER BY date DESC LIMIT $limit;"
sql := sqlite3.NamedArgs{"$limit": limit}
row := make(sqlite3.RowMap)
for s, err := cursor.Query(query, sql); err == nil; err = s.Next() {
var symb string
var rowid int64
s.Scan(&rowid, row)
date := strftime.Format("%Y-%m-%d %H:%M:%S", time.Unix(row["date"].(int64), 0))
title := row["title"]
author := row["site"]
read := row["read"].(int64)
// Use * for read
if read == 0 {
symb = " "
} else {
symb = "*"
}
fmt.Printf(" %3.d%s %s %-21s %s\n", rowid, symb, date, author, title)
}
}
func usage() {
fmt.Println("usage: fscan [options]")
fmt.Println(" -l, Show n new RSS feeds (default 10).")
fmt.Println(" -h, Show this help and exit.")
}
func main() {
args := Args{10}
var c int
getopt.OptErr = 0
for {
if c = getopt.Getopt("l:h"); c == getopt.EOF {
break
}
switch c {
case 'l':
args.limit, _ = strconv.Atoi(getopt.OptArg)
case 'h':
usage()
os.Exit(0)
}
}
initSQL()
showSQL(args.limit)
}
/* vim: set noet sw=4 sts=4: */