-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
366 lines (330 loc) · 9.57 KB
/
main.go
File metadata and controls
366 lines (330 loc) · 9.57 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package main
import (
"encoding/json"
"fmt"
"io"
"os"
"strings"
"github.com/urfave/cli/v2"
whatphone "samhofi.us/x/whatphone/pkg/api"
)
const (
// Current version string
version = "0.2.0"
// Exit code on failure
exitFail = 1
)
type configFunc func() (*whatphone.API, error)
type configReader struct {
reader configFunc
}
func newConfigReader(f configFunc) configReader {
return configReader{
reader: f,
}
}
func main() {
if err := run(os.Args, os.Stdout, newConfigReader(readConfig)); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(exitFail)
}
}
func run(args []string, stdout io.Writer, cr configReader) error {
app := cli.App{
Name: "WhatPhone",
HelpName: "whatphone",
Usage: "Phone number lookup via EveryoneAPI",
UseShortOptionHandling: true,
Writer: stdout,
Version: version,
Metadata: map[string]interface{}{"configReader": cr},
Commands: []*cli.Command{
{
Name: "lookup",
Usage: "Perform a phone number lookup",
Action: cmdLookup,
ArgsUsage: "<phone number>",
Flags: []cli.Flag{
&cli.StringFlag{
// TODO: implement this
Name: "json",
Aliases: []string{"j"},
Usage: "Output JSON data",
Hidden: true,
},
&cli.BoolFlag{
Name: "pricing-breakdown",
Aliases: []string{"b"},
Usage: "Include pricing breakdown of request",
},
&cli.BoolFlag{
Name: "all",
Usage: "Request all data points",
},
&cli.BoolFlag{
Name: "name",
Aliases: []string{"n"},
Usage: "Request name data",
},
&cli.BoolFlag{
Name: "profile",
Aliases: []string{"p"},
Usage: "Request profile data",
},
&cli.BoolFlag{
Name: "cnam",
Aliases: []string{"i"},
Usage: "Request CNAM data",
},
&cli.BoolFlag{
Name: "gender",
Aliases: []string{"g"},
Usage: "Request gender data",
},
&cli.BoolFlag{
Name: "image",
Aliases: []string{"m"},
Usage: "Request image data",
},
&cli.BoolFlag{
Name: "address",
Aliases: []string{"a"},
Usage: "Request address data",
},
&cli.BoolFlag{
Name: "location",
Aliases: []string{"l"},
Usage: "Request location data",
},
&cli.BoolFlag{
Name: "line-provider",
Aliases: []string{"r"},
Usage: "Request line provider data",
},
&cli.BoolFlag{
Name: "carrier",
Aliases: []string{"c"},
Usage: "Request carrier data",
},
&cli.BoolFlag{
Name: "original-carrier",
Aliases: []string{"o"},
Usage: "Request original carrier data",
},
&cli.BoolFlag{
Name: "linetype",
Aliases: []string{"t"},
Usage: "Request linetype data",
},
},
},
{
Name: "init",
Usage: "Initialize the app with your EveryoneAPI credentials",
Action: cmdInit,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "accountsid",
Aliases: []string{"s"},
Usage: "EveryoneAPI Account SID",
Required: true,
},
&cli.StringFlag{
Name: "authtoken",
Aliases: []string{"t"},
Usage: "EveryoneAPI Auth Token",
Required: true,
},
},
},
},
}
err := app.Run(args)
if err != nil {
return err
}
return nil
}
func cmdInit(c *cli.Context) error {
var configFile string
var err error
if configFile, err = getConfigFile(); err != nil {
return err
}
f, err := os.OpenFile(configFile, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
return err
}
defer f.Close()
api := whatphone.New(c.String("accountsid"), c.String("authtoken"))
err = json.NewEncoder(f).Encode(api)
if err != nil {
return err
}
fmt.Fprintf(c.App.Writer, "Config successfully written to %s\n", configFile)
return nil
}
func cmdLookup(c *cli.Context) error {
cr := c.App.Metadata["configReader"].(configReader)
reader := cr.reader
config, err := reader()
if err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("unable to read config; you may need to run the init command")
}
return err
}
if config.AccountSID == "" || config.AuthToken == "" {
return fmt.Errorf("authentication strings not set")
}
if c.NArg() < 1 {
return fmt.Errorf("missing phone number")
}
opts := make([]whatphone.Option, 0)
if c.Bool("name") {
opts = append(opts, whatphone.WithName())
}
if c.Bool("profile") {
opts = append(opts, whatphone.WithProfile())
}
if c.Bool("cnam") {
opts = append(opts, whatphone.WithCNAM())
}
if c.Bool("gender") {
opts = append(opts, whatphone.WithGender())
}
if c.Bool("image") {
opts = append(opts, whatphone.WithImage())
}
if c.Bool("address") {
opts = append(opts, whatphone.WithAddress())
}
if c.Bool("location") {
opts = append(opts, whatphone.WithLocation())
}
if c.Bool("line-provider") {
opts = append(opts, whatphone.WithLineProvider())
}
if c.Bool("carrier") {
opts = append(opts, whatphone.WithCarrier())
}
if c.Bool("original-carrier") {
opts = append(opts, whatphone.WithOriginalCarrier())
}
if c.Bool("linetype") {
opts = append(opts, whatphone.WithLineType())
}
if len(opts) == 0 && !c.Bool("all") {
return fmt.Errorf("no data points selected; use --all to request all data points")
}
phonenumber := c.Args().Get(0)
result, err := config.Lookup(phonenumber, opts...)
if err != nil {
return err
}
if result.Data.Name != nil {
fmt.Fprintf(c.App.Writer, "Name: %s\n", *result.Data.Name)
}
if result.Data.Profile != nil {
profile := *result.Data.Profile
fmt.Fprintf(c.App.Writer, "Profile:\n")
fmt.Fprintf(c.App.Writer, " Edu: %s\n Job: %s\n Relationship: %s\n", profile.Edu, profile.Job, profile.Relationship)
}
if result.Data.Cnam != nil {
fmt.Fprintf(c.App.Writer, "CNAM: %s\n", *result.Data.Cnam)
}
if result.Data.Gender != nil {
fmt.Fprintf(c.App.Writer, "Gender: %s\n", *result.Data.Gender)
}
if result.Data.Image != nil {
image := *result.Data.Image
fmt.Fprintf(c.App.Writer, "Image:\n")
fmt.Fprintf(c.App.Writer, " Cover: %s\n Small: %s\n Medium: %s\n Large: %s\n", image.Cover, image.Small, image.Med, image.Large)
}
if result.Data.Address != nil {
fmt.Fprintf(c.App.Writer, "Address: %s\n", *result.Data.Address)
}
if result.Data.Location != nil {
location := *result.Data.Location
fmt.Fprintf(c.App.Writer, "Location:\n")
fmt.Fprintf(c.App.Writer, " City, State, Zip: %s, %s, %s\n", location.City, location.State, location.Zip)
fmt.Fprintf(c.App.Writer, " Lat, Long: %s, %s\n", location.Geo.Latitude, location.Geo.Longitude)
}
if result.Data.LineProvider != nil {
lineprovider := *result.Data.LineProvider
fmt.Fprintf(c.App.Writer, "Line Provider:\n")
fmt.Fprintf(c.App.Writer, " ID: %s\n Name: %s\n MMS E-mail: %s\n SMS E-mail: %s\n", lineprovider.ID, lineprovider.Name, lineprovider.MmsEmail, lineprovider.SmsEmail)
}
if result.Data.Carrier != nil {
carrier := *result.Data.Carrier
fmt.Fprintf(c.App.Writer, "Carrier:\n")
fmt.Fprintf(c.App.Writer, " ID: %s\n Name: %s\n", carrier.ID, carrier.Name)
}
if result.Data.CarrierO != nil {
carriero := *result.Data.CarrierO
fmt.Fprintf(c.App.Writer, "Original Carrier:\n")
fmt.Fprintf(c.App.Writer, " ID: %s\n Name: %s\n", carriero.ID, carriero.Name)
}
if result.Data.Linetype != nil {
fmt.Fprintf(c.App.Writer, "Linetype: %s\n", *result.Data.Linetype)
}
if result.Note != "" {
fmt.Fprintf(c.App.Writer, "Note: %s\n", result.Note)
}
fmt.Fprintf(c.App.Writer, "Price Total: %.4f\n", result.Pricing.Total)
if c.Bool("pricing-breakdown") {
fmt.Fprintf(c.App.Writer, " Name: %.4f\n", result.Pricing.Breakdown.Name)
fmt.Fprintf(c.App.Writer, " Profile: %.4f\n", result.Pricing.Breakdown.Profile)
fmt.Fprintf(c.App.Writer, " CNAM: %.4f\n", result.Pricing.Breakdown.Cnam)
fmt.Fprintf(c.App.Writer, " Gender: %.4f\n", result.Pricing.Breakdown.Gender)
fmt.Fprintf(c.App.Writer, " Image: %.4f\n", result.Pricing.Breakdown.Image)
fmt.Fprintf(c.App.Writer, " Address: %.4f\n", result.Pricing.Breakdown.Address)
fmt.Fprintf(c.App.Writer, " Location: %.4f\n", result.Pricing.Breakdown.Location)
fmt.Fprintf(c.App.Writer, " Line Provider: %.4f\n", result.Pricing.Breakdown.LineProvider)
fmt.Fprintf(c.App.Writer, " Carrier: %.4f\n", result.Pricing.Breakdown.Carrier)
fmt.Fprintf(c.App.Writer, " Original Carrier: %.4f\n", result.Pricing.Breakdown.Carrier0)
fmt.Fprintf(c.App.Writer, " Linetype: %.4f\n", result.Pricing.Breakdown.Linetype)
}
if len(result.Missed) > 0 {
fmt.Fprintf(c.App.Writer, "\nMissed: %s\n", strings.Join(result.Missed, ", "))
}
return nil
}
// getconfigfile determines the appropriate path to read and write the config file
func getConfigFile() (string, error) {
configDir, err := os.UserConfigDir()
if err != nil {
return "", err
}
appDir := configDir + "/whatphone"
if _, err := os.Stat(appDir); os.IsNotExist(err) {
err = os.Mkdir(appDir, 0744)
if err != nil {
return "", err
}
}
return appDir + "/config.json", nil
}
// loadConfig loads a config from a reader and returns an api object
func loadConfig(r io.Reader) (*whatphone.API, error) {
var config whatphone.API
var err error
err = json.NewDecoder(r).Decode(&config)
if err != nil {
return nil, err
}
return &config, nil
}
// readConfig gets the config location, opens it, and returns an api object
func readConfig() (*whatphone.API, error) {
configFile, err := getConfigFile()
if err != nil {
return nil, err
}
f, err := os.Open(configFile)
if err != nil {
return nil, err
}
defer f.Close()
return loadConfig(f)
}