aboutsummaryrefslogtreecommitdiffstats
path: root/bridge/github
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2019-09-02 11:34:47 +0200
committerGitHub <noreply@github.com>2019-09-02 11:34:47 +0200
commit16af70894c0348ec90dfee69274f7d44ef2eb079 (patch)
tree5018a7356afa8ade6a69eb8767aa7a3439aa01da /bridge/github
parentc7792a5d1279a92c8d0caf67a0cf688cc9e32e09 (diff)
parentf3d8da10750d58fce482042cff87455a1e6f36e0 (diff)
downloadgit-bug-16af70894c0348ec90dfee69274f7d44ef2eb079.tar.gz
Merge pull request #200 from MichaelMure/bridge-token-stdin
bridge: support --token-stdin
Diffstat (limited to 'bridge/github')
-rw-r--r--bridge/github/config.go40
1 files changed, 39 insertions, 1 deletions
diff --git a/bridge/github/config.go b/bridge/github/config.go
index 45c078f6..16abfa09 100644
--- a/bridge/github/config.go
+++ b/bridge/github/config.go
@@ -21,6 +21,7 @@ import (
"github.com/MichaelMure/git-bug/bridge/core"
"github.com/MichaelMure/git-bug/repository"
+ "github.com/MichaelMure/git-bug/util/interrupt"
)
const (
@@ -37,13 +38,18 @@ var (
ErrBadProjectURL = errors.New("bad project url")
)
-func (*Github) Configure(repo repository.RepoCommon, params core.BridgeParams) (core.Configuration, error) {
+func (g *Github) Configure(repo repository.RepoCommon, params core.BridgeParams) (core.Configuration, error) {
conf := make(core.Configuration)
var err error
var token string
var owner string
var project string
+ if (params.Token != "" || params.TokenStdin) &&
+ (params.URL == "" && (params.Project == "" || params.Owner == "")) {
+ return nil, fmt.Errorf("you must provide a project URL or Owner/Name to configure this bridge with a token")
+ }
+
// getting owner and project name
if params.Owner != "" && params.Project != "" {
// first try to use params if both or project and owner are provided
@@ -85,6 +91,13 @@ func (*Github) Configure(repo repository.RepoCommon, params core.BridgeParams) (
if params.Token != "" {
token = params.Token
+ } else if params.TokenStdin {
+ reader := bufio.NewReader(os.Stdin)
+ token, err = reader.ReadString('\n')
+ if err != nil {
+ return nil, fmt.Errorf("reading from stdin: %v", err)
+ }
+ token = strings.TrimSuffix(token, "\n")
} else {
token, err = promptTokenOptions(owner, project)
if err != nil {
@@ -106,6 +119,11 @@ func (*Github) Configure(repo repository.RepoCommon, params core.BridgeParams) (
conf[keyOwner] = owner
conf[keyProject] = project
+ err = g.ValidateConfig(conf)
+ if err != nil {
+ return nil, err
+ }
+
return conf, nil
}
@@ -505,6 +523,16 @@ func validateProject(owner, project, token string) (bool, error) {
}
func promptPassword() (string, error) {
+ termState, err := terminal.GetState(int(syscall.Stdin))
+ if err != nil {
+ return "", err
+ }
+
+ cancel := interrupt.RegisterCleaner(func() error {
+ return terminal.Restore(int(syscall.Stdin), termState)
+ })
+ defer cancel()
+
for {
fmt.Print("password: ")
@@ -526,6 +554,16 @@ func promptPassword() (string, error) {
}
func prompt2FA() (string, error) {
+ termState, err := terminal.GetState(int(syscall.Stdin))
+ if err != nil {
+ return "", err
+ }
+
+ cancel := interrupt.RegisterCleaner(func() error {
+ return terminal.Restore(int(syscall.Stdin), termState)
+ })
+ defer cancel()
+
for {
fmt.Print("two-factor authentication code: ")