forked from GoBootcamp/clirescue
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
83 lines (70 loc) · 1.88 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
// This package provides an executable command line tool accessing the
// tracker API.
package main
import (
"io/ioutil"
"log"
"os"
"os/user"
"path/filepath"
"github.com/campoy/cliRescue/cmdutil"
"github.com/campoy/clirescue/trackerapi"
"github.com/codegangsta/cli"
)
var tokenFile = cachePath()
// cachePath returns the path for the cache file, relative to the user's home
// directory. It panics if the user's information is not available.
func cachePath() string {
usr, err := user.Current()
if err != nil {
panic(err)
}
return filepath.Join(usr.HomeDir, ".tracker")
}
// getToken prints the authentication token for the user. It tries to find it
// in the cache file first, if not found it requests user and password to
// obtained from the tracker API.
func getToken(c *cli.Context) {
// try to find token in file first.
content, err := ioutil.ReadFile(tokenFile)
if err != nil {
// only log an error if the file existed and couldn't read it
if !os.IsNotExist(err) {
log.Printf("load token: %v\n", content)
}
} else {
log.Printf("token: %s", content)
return
}
usr, err := cmdutil.ReadLine("Username")
if err != nil {
log.Fatalf("read username: %v", err)
}
pwd, err := cmdutil.ReadSilentLine("Password")
if err != nil {
log.Fatalf("read password: %v", err)
}
// get the tracker API auth token or panic if it fails.
token, err := trackerapi.APIToken(usr, pwd)
if err != nil {
log.Fatalf("authentication: %v", err)
}
log.Printf("token: %s\n", token)
err = ioutil.WriteFile(tokenFile, []byte(token), os.ModePerm)
if err != nil {
log.Fatalf("save token: %v", err)
}
}
func main() {
app := cli.NewApp()
app.Name = "clirescue"
app.Usage = "CLI tool to talk to the Pivotal Tracker's API"
app.Commands = []cli.Command{
{
Name: "me",
Usage: "prints out Tracker's representation of your account",
Action: getToken,
},
}
app.Run(os.Args)
}