/
config.go
214 lines (188 loc) · 5.25 KB
/
config.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
package conf
import (
"os"
"os/user"
"path/filepath"
db "github.com/rvflash/awql-db"
awql "github.com/rvflash/awql-driver"
"github.com/rvflash/awql/driver"
)
// Options represents all available parameters.
type Options interface {
AccountID() string
APIVersion() string
ExecuteStmt() string
IsInteractive() bool
SupportsZeroImpressions() bool
UseBatchMode() bool
UseVerboseMode() bool
WithAutoRehash() bool
WithCache() bool
}
// Settings represents all configurable options.
type Settings interface {
Options
Dsn() string
CacheDir() string
DatabaseDir() string
HistoryFile() string
Init() error
}
// Context represents the program properties.
type Context struct {
wrkDir, homeDir string
tk *Credentials
opts *Flag
}
// New returns an instance of Credentials.
// The string dir contains the path of the current workspace.
func New(dir string) *Context {
return &Context{wrkDir: dir, opts: Parse()}
}
// AccountID returns the Account ID.
func (c *Context) AccountID() string {
return *c.opts.AccountID
}
// APIVersion returns the API version.
func (c *Context) APIVersion() string {
return *c.opts.APIVersion
}
// CacheDir returns the path to store cache files.
func (c *Context) CacheDir() string {
if c.homeDir == "" {
return ""
}
return filepath.Join(c.homeDir, "cache")
}
// DatabaseDir returns the path to the database.
func (c *Context) DatabaseDir() string {
return filepath.Join(c.wrkDir, "vendor/github.com/rvflash/awql-db/internal/schema/src")
}
// Dsn outputs the data source name.
func (c *Context) Dsn() string {
// Data source name used to connect to Adwords.
dsn := awql.NewDsn(c.AccountID())
dsn.APIVersion = c.APIVersion()
dsn.SupportsZeroImpressions = c.SupportsZeroImpressions()
dsn.SkipColumnHeader = true
// Credentials.
dsn.AccessToken = c.tk.AccessToken
dsn.ClientID = c.tk.ClientID
dsn.ClientSecret = c.tk.ClientSecret
dsn.DeveloperToken = c.tk.DeveloperToken
dsn.RefreshToken = c.tk.RefreshToken
return driver.NewDsn(
c.DatabaseDir(), dsn.String(), c.CacheDir(), c.WithCache(),
).String()
}
// ExecuteStmt returns the statement to execute.
func (c *Context) ExecuteStmt() string {
return *c.opts.Query
}
// HistoryFile returns the path to the history file.
func (c *Context) HistoryFile() string {
if c.homeDir == "" {
return ""
}
return filepath.Join(c.homeDir, "history")
}
// Init retrieves and saves the default authenticate information.
func (c *Context) Init() error {
// Checks for required flags.
if err := c.opts.Check(); err != nil {
return err
}
// Checks if it's a supported Adwords API versions.
// Opens a connection to Awql DB without load anything.
if _, err := db.Open(c.APIVersion() + ":true"); err != nil {
return err
}
// Create the tool home directory.
if err := c.mkDirHome(); err != nil {
return err
}
// Checks credential to authenticate to Adwords.
switch {
case *c.opts.AccessToken != "":
// Use temporary credential.
c.tk = NewTmpCredentials(*c.opts.AccessToken, *c.opts.DeveloperToken)
case !c.useDefaultAuth():
// Aks authenticate credentials to user.
c.tk = AskCredentials()
if err := c.tk.Save(c.filePath()); err != nil {
return err
}
default:
// Get authenticate credentials from configuration file.
c.tk = NewCredentials()
if err := c.tk.Get(c.filePath()); err != nil {
return err
}
}
return nil
}
// IsInteractive returns true if query as passed as flag.
func (c *Context) IsInteractive() bool {
return *c.opts.Query == ""
}
// SupportsZeroImpressions returns true if the support of zero impressions is enable.
func (c *Context) SupportsZeroImpressions() bool {
return *c.opts.ZeroImpressions
}
// UseBatchMode returns true if raw mode is required.
// It will print results using colon as the column separator, with each row on a new line.
func (c *Context) UseBatchMode() bool {
return *c.opts.Batch
}
// UseVerboseMode returns true if more output about what the program does is required.
func (c *Context) UseVerboseMode() bool {
return *c.opts.Verbose
}
// WithAutoRehash returns true if automatic rehashing is enable.
func (c *Context) WithAutoRehash() bool {
return !*c.opts.NoRehash
}
// WithCache returns true if the cache is enable.
func (c *Context) WithCache() bool {
return *c.opts.Caching
}
// filePath returns the path to the config file.
func (c *Context) filePath() string {
if c.homeDir == "" {
return ""
}
return filepath.Join(c.homeDir, "config")
}
// mkDirHome creates if not already exists the home directory.
func (c *Context) mkDirHome() error {
if c.homeDir != "" {
// Directory already made.
return nil
}
// Gets user properties.
usr, err := user.Current()
if err != nil {
return err
}
// If not exists, create the user home directory of Awql tool: .awql
c.homeDir = filepath.Join(usr.HomeDir, ".awql")
if _, err := os.Stat(c.homeDir); os.IsNotExist(err) {
if err := os.Mkdir(c.homeDir, os.ModePerm); err != nil {
return err
}
}
// Tries to create the cache directory: .awql/cache
if err := os.Mkdir(c.CacheDir(), os.ModePerm); !os.IsExist(err) {
return err
}
return nil
}
// useDefaultAuth returns true if the config file exists.
func (c *Context) useDefaultAuth() bool {
if c.homeDir == "" {
return false
}
// Checks if config file exists.
_, err := os.Stat(c.filePath())
return err == nil
}