allow issues on command line to automatically prefix with project when defined

This commit is contained in:
Cory Bennett
2019-12-01 16:20:43 -08:00
parent 789886c68e
commit d002d7fe74
33 changed files with 179 additions and 55 deletions
+28
View File
@@ -12,6 +12,7 @@ import (
"os/exec"
"reflect"
"runtime/debug"
"strconv"
"strings"
"github.com/coryb/figtree"
@@ -458,3 +459,30 @@ func EditLoop(opts *CommonOptions, input interface{}, output interface{}, submit
}
return nil
}
func FormatIssue(issueKey string, project string) string {
if issueKey == "" {
return ""
}
// expect PROJ-1234 issue format, this will split and
// reassemble, converting proj-1234 to PROJ-1234
parts := strings.SplitN(issueKey, "-", 2)
if len(parts) > 1 {
return fmt.Sprintf("%s-%s", strings.ToUpper(parts[0]), parts[1])
}
// if issue is not PROJ-1234 then it might just be 1234, so verify
// it is a number here otherwise warn and return input
if _, err := strconv.Atoi(issueKey); err != nil {
log.Warningf("Unexpected issue format %q, expected PROJ-1234", issueKey)
return issueKey
}
if project == "" {
log.Warningf("Using abbreviated issue %q but `project` property is not defined", issueKey)
return issueKey
}
return fmt.Sprintf("%s-%s", strings.ToUpper(project), issueKey)
}
+30 -25
View File
@@ -42,8 +42,10 @@ func (o *GlobalOptions) keyName() string {
}
func (o *GlobalOptions) GetPass() string {
log.Debugf("Getting Password")
passwd := ""
if o.PasswordSource.Value != "" {
log.Debugf("password-source: %s", o.PasswordSource)
if o.PasswordSource.Value == "keyring" {
var err error
passwd, err = keyringGet(o.keyName())
@@ -53,18 +55,23 @@ func (o *GlobalOptions) GetPass() string {
} else if o.PasswordSource.Value == "gopass" {
if o.PasswordDirectory.Value != "" {
orig := os.Getenv("PASSWORD_STORE_DIR")
log.Debugf("using password-directory: %s", o.PasswordDirectory)
os.Setenv("PASSWORD_STORE_DIR", o.PasswordDirectory.Value)
defer os.Setenv("PASSWORD_STORE_DIR", orig)
}
if passDir := os.Getenv("PASSWORD_STORE_DIR"); passDir != "" {
log.Debugf("using PASSWORD_STORE_DIR=%s", passDir)
}
if bin, err := exec.LookPath("gopass"); err == nil {
log.Debugf("found gopass at: %s", bin)
buf := bytes.NewBufferString("")
cmd := exec.Command(bin, "show", "-o", o.keyName())
cmd.Stdout = buf
cmd.Stderr = buf
cmd.Stderr = os.Stderr
if err := cmd.Run(); err == nil {
passwd = strings.TrimSpace(buf.String())
} else {
panic(err)
log.Warningf("gopass command failed with:\n%s", buf.String())
}
} else {
log.Warning("Gopass binary was not found! Fallback to default password behaviour!")
@@ -72,21 +79,26 @@ func (o *GlobalOptions) GetPass() string {
} else if o.PasswordSource.Value == "pass" {
if o.PasswordDirectory.Value != "" {
orig := os.Getenv("PASSWORD_STORE_DIR")
log.Debugf("using password-directory: %s", o.PasswordDirectory)
os.Setenv("PASSWORD_STORE_DIR", o.PasswordDirectory.Value)
defer os.Setenv("PASSWORD_STORE_DIR", orig)
}
if passDir := os.Getenv("PASSWORD_STORE_DIR"); passDir != "" {
log.Debugf("using PASSWORD_STORE_DIR=%s", passDir)
}
if bin, err := exec.LookPath("pass"); err == nil {
log.Debugf("found pass at: %s", bin)
buf := bytes.NewBufferString("")
cmd := exec.Command(bin, o.keyName())
cmd.Stdout = buf
cmd.Stderr = buf
cmd.Stderr = os.Stderr
if err := cmd.Run(); err == nil {
passwd = strings.TrimSpace(buf.String())
} else {
panic(err)
log.Warningf("pass command failed with:\n%s", buf.String())
}
} else {
log.Warning("Pass binary was not found! Fallback to default password behaviour!")
log.Warning("pass binary was not found! Fallback to default password behaviour!")
}
} else if o.PasswordSource.Value == "stdin" {
allBytes, err := ioutil.ReadAll(os.Stdin)
@@ -132,6 +144,11 @@ func (o *GlobalOptions) GetPass() string {
}
func (o *GlobalOptions) SetPass(passwd string) error {
// dont reset password to empty string
if passwd == "" {
return nil
}
if o.PasswordSource.Value == "keyring" {
// save password in keychain so that it can be used for subsequent http requests
err := keyringSet(o.keyName(), passwd)
@@ -158,11 +175,6 @@ func (o *GlobalOptions) SetPass(passwd string) error {
if err := cmd.Run(); err != nil {
return fmt.Errorf("Failed to insert password: %s", out.String())
}
} else {
// clear the `pass` entry on empty password
if err := exec.Command(bin, "rm", "--force", passName).Run(); err != nil {
return fmt.Errorf("Failed to clear password for %s", passName)
}
}
} else {
return fmt.Errorf("Gopass binary not found!")
@@ -176,21 +188,14 @@ func (o *GlobalOptions) SetPass(passwd string) error {
if bin, err := exec.LookPath("pass"); err == nil {
log.Debugf("using %s", bin)
passName := o.keyName()
if passwd != "" {
in := bytes.NewBufferString(fmt.Sprintf("%s\n%s\n", passwd, passwd))
out := bytes.NewBufferString("")
cmd := exec.Command(bin, "insert", "--force", passName)
cmd.Stdin = in
cmd.Stdout = out
cmd.Stderr = out
if err := cmd.Run(); err != nil {
return fmt.Errorf("Failed to insert password: %s", out.String())
}
} else {
// clear the `pass` entry on empty password
if err := exec.Command(bin, "rm", "--force", passName).Run(); err != nil {
return fmt.Errorf("Failed to clear password for %s", passName)
}
in := bytes.NewBufferString(fmt.Sprintf("%s\n%s\n", passwd, passwd))
out := bytes.NewBufferString("")
cmd := exec.Command(bin, "insert", "--force", passName)
cmd.Stdin = in
cmd.Stdout = out
cmd.Stderr = out
if err := cmd.Run(); err != nil {
return fmt.Errorf("Failed to insert password: %s", out.String())
}
} else {
return fmt.Errorf("Pass binary not found!")
+1 -1
View File
@@ -173,7 +173,7 @@ func ParseCommandLine(app *kingpin.Application, args []string) {
if ctx.SelectedCommand == nil {
next := ctx.Next()
if next != nil {
if ok, err := regexp.MatchString("^[A-Z]+-[0-9]+$", next.Value); err != nil {
if ok, err := regexp.MatchString("^([A-Z]+-)?[0-9]+$", next.Value); err != nil {
log.Errorf("Invalid Regex: %s", err)
} else if ok {
// insert "view" at i=1 (2nd position)