-
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#136] (fix) issue displaying token url when entering credentials
Also removes the old gitconfig based config and tests the newer lab.hcl configuration
- Loading branch information
1 parent
f5dcd02
commit db6bffb
Showing
4 changed files
with
173 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package config | ||
|
||
import ( | ||
"bufio" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net/url" | ||
"strings" | ||
"syscall" | ||
|
||
"github.com/spf13/viper" | ||
"golang.org/x/crypto/ssh/terminal" | ||
) | ||
|
||
const defaultGitLabHost = "https://gitlab.com" | ||
|
||
// New prompts the user for the default config values to use with lab, and save | ||
// them to the provided confpath (default: ~/.config/lab.hcl) | ||
func New(confpath string, r io.Reader) error { | ||
var ( | ||
reader = bufio.NewReader(r) | ||
host, user, token string | ||
err error | ||
) | ||
fmt.Printf("Enter default GitLab host (default: %s): ", defaultGitLabHost) | ||
host, err = reader.ReadString('\n') | ||
host = strings.TrimSpace(host) | ||
if err != nil { | ||
return err | ||
} | ||
if host == "" { | ||
host = defaultGitLabHost | ||
} | ||
|
||
fmt.Print("Enter default GitLab user: ") | ||
user, err = reader.ReadString('\n') | ||
user = strings.TrimSpace(user) | ||
if err != nil { | ||
return err | ||
} | ||
if user == "" { | ||
return errors.New("lab.hcl config core.user must be set") | ||
} | ||
|
||
tokenURL, err := url.Parse(host) | ||
if err != nil { | ||
return err | ||
} | ||
tokenURL.Path = "profile/personal_access_tokens" | ||
|
||
fmt.Printf("Create a token here: %s\nEnter default GitLab token (scope: api): ", tokenURL.String()) | ||
token, err = readPassword() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
viper.Set("core.host", host) | ||
viper.Set("core.user", user) | ||
viper.Set("core.token", token) | ||
if err := viper.WriteConfigAs(confpath); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
var readPassword = func() (string, error) { | ||
byteToken, err := terminal.ReadPassword(int(syscall.Stdin)) | ||
if err != nil { | ||
return "", err | ||
} | ||
return strings.TrimSpace(string(byteToken)), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package config | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"math/rand" | ||
"os" | ||
"path" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewConfig(t *testing.T) { | ||
testconf := "/tmp/testconf-" + strconv.Itoa(int(rand.Uint64())) | ||
os.Mkdir(testconf, os.FileMode(0700)) | ||
|
||
t.Run("create config", func(t *testing.T) { | ||
old := os.Stdout // keep backup of the real stdout | ||
r, w, _ := os.Pipe() | ||
os.Stdout = w | ||
|
||
var buf bytes.Buffer | ||
fmt.Fprintln(&buf, "https://gitlab.zaquestion.io") | ||
fmt.Fprintln(&buf, "zaq") | ||
|
||
oldreadPassword := readPassword | ||
readPassword = func() (string, error) { | ||
return "abcde12345", nil | ||
} | ||
defer func() { | ||
readPassword = oldreadPassword | ||
}() | ||
|
||
err := New(path.Join(testconf, "lab.hcl"), &buf) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
outC := make(chan string) | ||
// copy the output in a separate goroutine so printing can't block indefinitely | ||
go func() { | ||
var buf bytes.Buffer | ||
io.Copy(&buf, r) | ||
outC <- buf.String() | ||
}() | ||
|
||
// back to normal state | ||
w.Close() | ||
os.Stdout = old // restoring the real stdout | ||
out := <-outC | ||
|
||
assert.Contains(t, out, "Enter default GitLab host (default: https://gitlab.com): ") | ||
assert.Contains(t, out, "Enter default GitLab user:") | ||
assert.Contains(t, out, "Create a token here: https://gitlab.zaquestion.io/profile/personal_access_tokens\nEnter default GitLab token (scope: api):") | ||
|
||
cfg, err := os.Open(path.Join(testconf, "lab.hcl")) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
cfgData, err := ioutil.ReadAll(cfg) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
assert.Equal(t, string(cfgData), `"core" = { | ||
"host" = "https://gitlab.zaquestion.io" | ||
"token" = "abcde12345" | ||
"user" = "zaq" | ||
}`) | ||
}) | ||
os.RemoveAll(testconf) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters