-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
129 lines (121 loc) · 2.99 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"log"
"os"
"github.com/LevInteractive/allwrite-docs/api"
"github.com/LevInteractive/allwrite-docs/gdrive"
"github.com/LevInteractive/allwrite-docs/store/postgres"
"github.com/LevInteractive/allwrite-docs/util"
"github.com/joeshaw/envdecode"
"github.com/urfave/cli"
)
var debugInfoMessage = `
------------------------------------------------------
INFO:
Client Secret: %s
Active Directory: %s
Storage Drive: %s
Address: %s
------------------------------------------------------
`
func main() {
app := cli.NewApp()
app.Name = "Allwrite Docs | Publish your documentation with Drive."
app.Version = "0.0.3"
app.Commands = []cli.Command{
{
Name: "start",
Aliases: []string{"s"},
Usage: "Start the server in the foreground. This will authenticate with Google if it's the first time you're running.",
Action: func(c *cli.Context) error {
if env := setupConf(); env != nil {
client := gdrive.DriveClient(env.CFG.ClientSecret)
gdrive.WatchDrive(client, env)
api.Listen(env)
}
return nil
},
},
{
Name: "setup",
Usage: "Only authenticate with Google and do not run the allwrite server.",
Action: func(c *cli.Context) error {
if env := setupConf(); env != nil {
gdrive.DriveClient(env.CFG.ClientSecret)
}
return nil
},
},
{
Name: "pull",
Aliases: []string{"p"},
Usage: "Pull the latest content from Google Drive.",
Action: func(c *cli.Context) error {
if env := setupConf(); env != nil {
client := gdrive.DriveClient(env.CFG.ClientSecret)
gdrive.UpdateMenu(client, env)
}
return nil
},
},
{
Name: "reset",
Aliases: []string{"r"},
Usage: "Reset any saved authentication credentials for Google. You will need to re-authenticate after doing this.",
Action: func(c *cli.Context) error {
if env := setupConf(); env != nil {
if err := gdrive.RemoveCacheFile(); err != nil {
return err
}
}
return nil
},
},
{
Name: "info",
Aliases: []string{"i"},
Usage: "Display environmental variables. Useful for making sure everything is setup correctly.",
Action: func(c *cli.Context) error {
if env := setupConf(); env != nil {
log.Printf(
debugInfoMessage,
env.CFG.ClientSecret,
env.CFG.ActiveDir,
env.CFG.StoreType,
env.CFG.Port,
)
}
return nil
},
},
}
app.Run(os.Args)
}
func setupConf() *util.Env {
var cfg util.Conf
if err := envdecode.Decode(&cfg); err != nil {
log.Println("Please make sure the environmental variables are set first.")
return nil
}
env := &util.Env{
CFG: &cfg,
}
switch cfg.StoreType {
case "postgres":
if db, err := postgres.Init(
cfg.PostgresUser,
cfg.PostgresPassword,
cfg.PostgresHost,
cfg.PostgresDBName,
); err == nil {
env.DB = db
} else {
log.Printf("Could not connect to postgres: %s", err.Error())
return nil
}
default:
log.Printf("You must specify a storage system. (postgres)")
return nil
}
return env
}