-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathapp.go
255 lines (226 loc) · 5.75 KB
/
app.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package cmd
import (
"fmt"
"io"
"log"
"os"
"os/exec"
fp "path/filepath"
"github.com/harrybrwn/apizza/cmd/cli"
"github.com/harrybrwn/apizza/cmd/client"
"github.com/harrybrwn/apizza/cmd/internal"
"github.com/harrybrwn/apizza/cmd/internal/data"
"github.com/harrybrwn/apizza/cmd/internal/obj"
"github.com/harrybrwn/apizza/cmd/opts"
"github.com/harrybrwn/apizza/dawg"
"github.com/harrybrwn/apizza/pkg/cache"
"github.com/harrybrwn/apizza/pkg/config"
"github.com/harrybrwn/apizza/pkg/errs"
"github.com/spf13/cobra"
)
// App is the main app, the root command, and the Builder.
type App struct {
cli.CliCommand // this is also the root command
client.StoreFinder // for app.store()
db *cache.DataBase
conf *cli.Config
addr *obj.Address
logf *os.File
// global apizza options
gOpts opts.CliFlags
// root specific options
opts opts.ApizzaFlags
}
// NewApp creates a new app for the main cli.
func NewApp(out io.Writer) *App {
app := &App{
db: nil,
conf: &cli.Config{},
gOpts: opts.CliFlags{},
opts: opts.ApizzaFlags{},
}
app.CliCommand = cli.NewCommand("apizza", "Dominos pizza from the command line.", app.Run)
app.StoreFinder = client.NewStoreGetterFunc(app.getService, app.Address)
cmd := app.Cmd()
cmd.PersistentPreRunE = app.prerun
cmd.PostRunE = app.postrun
app.SetOutput(out)
return app
}
// CreateApp from a pre-created database and config.
func CreateApp(db *cache.DataBase, conf *cli.Config, out io.Writer) *App {
app := NewApp(out)
app.db = db
app.conf = conf
return app
}
// Init wil setup the app.
func (a *App) Init(dir string) error {
if a.conf == nil {
a.conf = &cli.Config{}
}
a.initflags()
return errs.Pair(a.SetConfig(dir), a.InitDB())
}
// SetConfig for the the app
func (a *App) SetConfig(dir string) error {
return config.SetConfig(dir, a.conf)
}
// InitDB for the app.
func (a *App) InitDB() (err error) {
a.db, err = data.OpenDatabase()
return
}
// DB returns the database
func (a *App) DB() *cache.DataBase {
return a.db
}
// Build builds commands.
func (a *App) Build(use, short string, r cli.Runner) *cli.Command {
return cli.NewCommand(use, short, r.Run)
}
// Config returns the config struct.
func (a *App) Config() *cli.Config {
return a.conf
}
// Address returns the address.
func (a *App) Address() dawg.Address {
if a.addr != nil {
return a.addr
}
if a.conf.DefaultAddressName != "" {
addr, err := a.getDBAddress(a.conf.DefaultAddressName)
if err != nil {
fmt.Fprintf(os.Stderr,
"Warning: could not find an address named '%s'\n",
a.conf.DefaultAddressName)
if obj.AddrIsEmpty(&a.conf.Address) {
errs.StopNow(internal.ErrNoAddress, "Error", 1)
}
return &a.conf.Address
}
a.addr = addr
return addr
}
return &a.conf.Address
}
// GlobalOptions returns the variables for the app's global flags
func (a *App) GlobalOptions() *opts.CliFlags {
return &a.gOpts
}
// Cleanup cleans everything up.
func (a *App) Cleanup() (err error) {
return errs.Pair(a.db.Close(), config.Save())
}
func (a *App) getService() string {
if len(a.gOpts.Service) == 0 {
return a.conf.Service
}
return a.gOpts.Service
}
var _ cli.Builder = (*App)(nil)
// Run the app.
func (a *App) Run(cmd *cobra.Command, args []string) (err error) {
if a.opts.Openlogs {
editor := os.Getenv("EDITOR")
c := exec.Command(editor, fp.Join(config.Folder(), "logs", "dev.log"))
c.Stdin = os.Stdin
c.Stdout = a.Output()
return c.Run()
}
if a.gOpts.ClearCache {
a.Printf("removing %s\n", a.db.Path())
return a.db.Destroy()
}
if a.opts.StoreLocation {
store := a.Store()
a.Println(store.Address)
a.Println("\nStore id:", store.ID)
a.Printf("Coordinates: %s, %s\n",
store.StoreCoords["StoreLatitude"],
store.StoreCoords["StoreLongitude"],
)
return nil
}
if a.opts.Dumpdb {
data, err := a.db.Map()
if err != nil {
return err
}
log.Println("dumping database to stdout")
fmt.Print("{")
for k, v := range data {
fmt.Printf("\"%s\":%v,", k, string(v))
}
fmt.Print("}")
return nil
}
return cmd.Usage()
}
func (a *App) initflags() {
cmd := a.Cmd()
flags := cmd.Flags()
persistflags := cmd.PersistentFlags()
a.gOpts.Install(persistflags)
a.opts.Install(flags)
persistflags.BoolVar(&test, "test", false, "testing flag (for development)")
persistflags.MarkHidden("test")
}
func (a *App) prerun(*cobra.Command, []string) (err error) {
if a.gOpts.ResetMenu {
err = a.DB().Delete("menu")
}
var e error
if a.gOpts.Address != "" {
// First look in the database as if the flag was a named address.
// Else check if the flag is a parsable address.
if a.db.WithBucket("addresses").Exists(a.gOpts.Address) {
newaddr, err := a.getDBAddress(a.gOpts.Address)
if err != nil {
return err
}
a.addr = newaddr
} else {
parsed, err := dawg.ParseAddress(a.gOpts.Address)
if err != nil {
return err
}
a.addr = obj.FromAddress(parsed)
}
}
if a.gOpts.Service != "" {
if !(a.gOpts.Service == dawg.Delivery || a.gOpts.Service == dawg.Carryout) {
return dawg.ErrBadService
}
// BUG: setting the config field will implicitly change the config file
a.conf.Service = a.gOpts.Service
}
if a.gOpts.LogFile != "" {
dir := fp.Join(config.Folder(), "logs")
if _, err := os.Stat(dir); os.IsNotExist(err) {
err = os.MkdirAll(dir, 0700)
if err != nil {
return err
}
}
a.logf, e = os.Create(fp.Join(dir, a.gOpts.LogFile))
log.SetOutput(a.logf)
}
return errs.Pair(err, e)
}
func (a *App) getDBAddress(key string) (*obj.Address, error) {
raw, err := a.db.WithBucket("addresses").Get(key)
if err != nil {
return nil, err
}
return obj.FromGob(raw)
}
func (a *App) postrun(*cobra.Command, []string) (err error) {
if a.logf != nil {
return a.logf.Close()
}
return nil
}
func logfile(name string) string {
return fp.Join(config.Folder(), "logs", name)
}