-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgonetact.go
66 lines (53 loc) · 1.77 KB
/
gonetact.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
// -*- golang -*-
package main
import (
"fmt"
"log"
"os"
"os/user"
"github.com/docopt/docopt-go"
)
type CmdLineOpts struct {
client_id string // filename
cache_file string // filename
username string // gmail email address
query string // substring to match
no_browser bool // don't open the browser
}
func readCommandLine(argv []string) *CmdLineOpts {
usr, err := user.Current()
rc_dir := fmt.Sprintf("%s/.gonetact", usr.HomeDir)
if err != nil {
log.Fatal( err )
}
opts := new(CmdLineOpts)
docstring := fmt.Sprintf(`Limited interaction with google contacts
Usage:
gonetact [--client-id=<filename>] [--cache=<cache-file>] [--query=<query>] [--no-browser]
Options:
--client-id=<filename> file containing a json client_id [default: %[1]s/client.json]
--cache=<filename> file to cache the access token[default: %[1]s/cache.json]
--query=<query> match this string in the email and name of the contact
--no-browser Don't open the auth link a browser [default: false]
-h --help Show this message
The client_id is a file containing a json document per
https://code.google.com/apis/console#access`, rc_dir)
args, _ := docopt.Parse(docstring, argv[1:], true, "goneact 0.1", false)
opts.client_id = string(args["--client-id"].(string))
opts.cache_file = string(args["--cache"].(string))
opts.no_browser = bool(args["--no-browser"].(bool))
if args["--query"] != nil {
opts.query = string(args["--query"].(string))
}
return opts
}
func main() {
opts := readCommandLine(os.Args)
transport := get_oauth_token(opts.client_id, opts.cache_file, opts.no_browser)
// fmt.Println(transport)
if opts.query == "" {
print_all_contacts(transport)
} else {
print_matching_contacts(transport, opts.query)
}
}