add logout command

modify password prompt to echo masked password
This commit is contained in:
Cory Bennett
2016-08-02 20:04:11 -07:00
parent f93fe7909c
commit 8ad91be1c6
3 changed files with 30 additions and 5 deletions
+1
View File
@@ -15,6 +15,7 @@ import (
"net/url"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strings"
+25 -5
View File
@@ -4,9 +4,8 @@ import (
"bytes"
"errors"
"fmt"
"golang.org/x/crypto/ssh/terminal"
"github.com/howeyc/gopass"
"net/http"
"net/http/httputil"
"net/url"
"os"
"strings"
@@ -19,9 +18,12 @@ func (c *Cli) CmdLogin() error {
req, _ := http.NewRequest("GET", uri, nil)
user, _ := c.opts["user"].(string)
fmt.Printf("Enter Password for %s: ", user)
pwbytes, _ := terminal.ReadPassword(int(os.Stdin.Fd()))
passwd := string(pwbytes)
fmt.Printf("Jira Password [%s]: ", user)
pw, err := gopass.GetPasswdMasked()
if err != nil {
return err
}
passwd := string(pw)
req.SetBasicAuth(user, passwd)
log.Infof("%s %s", req.Method, req.URL.String())
@@ -57,6 +59,24 @@ func (c *Cli) CmdLogin() error {
return nil
}
func (c *Cli) CmdLogout() error {
uri := fmt.Sprintf("%s/rest/auth/1/session", c.endpoint)
req, _ := http.NewRequest("DELETE", uri, nil)
if resp, err := c.makeRequest(req); err != nil {
return err
} else {
if resp.StatusCode == 401 || resp.StatusCode == 204 {
// 401 == no active session
// 204 == successfully logged out
} else {
err := fmt.Errorf("Failed to Logout: %s", err)
return err
}
}
log.Notice("OK")
return nil
}
func (c *Cli) CmdFields() error {
log.Debugf("fields called")
uri := fmt.Sprintf("%s/rest/api/2/field", c.endpoint)
+4
View File
@@ -84,6 +84,7 @@ Usage:
jira export-templates [-d DIR] [-t template]
jira (b|browse) ISSUE
jira login
jira logout
jira request [-M METHOD] URI [DATA]
jira ISSUE
@@ -166,6 +167,7 @@ Command Options:
"export-templates": "export-templates",
"browse": "browse",
"login": "login",
"logout": "logout",
"req": "request",
"request": "request",
"vote": "vote",
@@ -313,6 +315,8 @@ Command Options:
switch command {
case "login":
err = c.CmdLogin()
case "logout":
err = c.CmdLogout()
case "fields":
err = c.CmdFields()
case "list":