forked from markpasc/cares
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
101 lines (91 loc) · 2.41 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
94
95
96
97
98
99
100
101
package main
import (
"bufio"
"flag"
"log"
"os"
"strings"
)
func Prompt(prompt string) (ret string) {
input := bufio.NewReader(os.Stdin)
os.Stdout.Write([]byte(prompt))
for {
line, isPrefix, err := input.ReadLine()
if err != nil {
return
}
ret += string(line)
if !isPrefix {
break
}
}
ret = strings.TrimSpace(ret)
return
}
func MakeAccount() {
var err error
name := Prompt("Name: ")
pass := Prompt("Password: ")
displayName := Prompt("Display name: ")
author := NewAuthor()
author.Name = displayName
author.Url = "/"
err = author.Save()
if err != nil {
logr.Errln("Error saving new account:", err.Error())
return
}
account := NewAccount()
account.Name = name
account.DisplayName = displayName
account.SetPassword(pass)
err = account.Save()
if err != nil {
logr.Errln("Error saving new account:", err.Error())
return
}
}
func main() {
var dsn string
var makeaccount, initdb, upgradedb bool
var importthinkup, importjson, backup, importbackup string
var port int
flag.StringVar(&dsn, "database", "dbname=cares sslmode=disable", "database connection info")
flag.BoolVar(&makeaccount, "make-account", false, "create a new account interactively")
flag.BoolVar(&initdb, "init-db", false, "initialize the database")
flag.BoolVar(&upgradedb, "upgrade-db", false, "upgrade the database schema")
flag.StringVar(&importthinkup, "import-thinkup", "", "path to a Thinkup CSV export to import")
flag.StringVar(&importjson, "import-json", "", "path to a directory of Twitter JSON to import")
flag.StringVar(&backup, "backup", "", "path to which to save a backup of the current tweets")
flag.StringVar(&importbackup, "import-backup", "", "path to a cares backup to import")
flag.IntVar(&port, "port", 8080, "port on which to serve the web interface")
flag.Parse()
err := SetUpLogger()
if err != nil {
log.Println("Error setting up logging:", err.Error())
return
}
defer logr.Close()
err = OpenDatabase(dsn, initdb || upgradedb)
if err != nil {
logr.Errln("Error connecting to database:", err.Error())
return
}
if initdb {
InitializeDatabase()
} else if upgradedb {
UpgradeDatabase()
} else if makeaccount {
MakeAccount()
} else if importjson != "" {
ImportJson(importjson)
} else if importthinkup != "" {
ImportThinkup(importthinkup)
} else if importbackup != "" {
ImportBackup(importbackup)
} else if backup != "" {
ExportBackup(backup)
} else {
ServeWeb(port)
}
}