/
cmd_login.go
84 lines (68 loc) · 1.79 KB
/
cmd_login.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
package main
import (
"fmt"
"net/http"
"github.com/gonuts/commander"
"github.com/gonuts/flag"
"github.com/mewpkg/gopass"
"github.com/sbinet/go-github-client/client"
)
func git_make_cmd_login() *commander.Command {
cmd := &commander.Command{
Run: git_run_cmd_login,
UsageLine: "login [options]",
Short: "login stores the github authentication data",
Long: `
login stores the github authentication data
ex:
$ goctogit login
$ goctogit login -u mylogin
`,
Flag: *flag.NewFlagSet("git-create", flag.ExitOnError),
}
cmd.Flag.String("u", "", "github user account")
return cmd
}
func git_run_cmd_login(cmd *commander.Command, args []string) {
n := "github-" + cmd.Name()
user := cmd.Flag.Lookup("u").Value.Get().(string)
fmt.Printf("github username: ")
if user == "" {
_, err := fmt.Scanln(&user)
handle_err(err)
} else {
fmt.Printf("%s\n", user)
}
password, err := getpasswd("github password: ")
handle_err(err)
section := "go-octogit"
for k, v := range map[string]string{
"username": user,
"password": password,
} {
if Cfg.HasOption(section, k) {
Cfg.RemoveOption(section, k)
}
if !Cfg.AddOption(section, k, v) {
err := fmt.Errorf("%s: could not add option [%s] to section [%s]", n, k, section)
panic(err.Error())
}
}
// check credentials
ghc, err := client.NewGithubClient(user, password, client.AUTH_USER_PASSWORD)
handle_err(err)
req, err := ghc.NewAPIRequest("GET", "authorizations", nil)
handle_err(err)
resp, err := ghc.RunRequest(req, new(http.Client))
handle_err(err)
if !resp.IsSuccess() {
err = fmt.Errorf("%s: authentication failed\n%v\n", n, resp.RawHttpResponse)
handle_err(err)
}
err = Cfg.WriteFile(CfgFname, 0600, "")
handle_err(err)
}
func getpasswd(prompt string) (string, error) {
return gopass.GetPass(prompt)
}
// EOF