mirror of
https://github.com/grdl/git-get.git
synced 2026-03-22 18:05:07 +00:00
Make the URL scheme default to ssh instead of https and make it configurable
This commit is contained in:
@@ -17,21 +17,22 @@ const GitgetPrefix = "gitget"
|
||||
|
||||
// CLI flag keys.
|
||||
var (
|
||||
KeyBranch = "branch"
|
||||
KeyDump = "dump"
|
||||
KeyDefaultHost = "host"
|
||||
KeyFetch = "fetch"
|
||||
KeyOutput = "out"
|
||||
KeySkipHost = "skip-host"
|
||||
KeyReposRoot = "root"
|
||||
KeyBranch = "branch"
|
||||
KeyDump = "dump"
|
||||
KeyDefaultHost = "host"
|
||||
KeyFetch = "fetch"
|
||||
KeyOutput = "out"
|
||||
KeyDefaultScheme = "scheme"
|
||||
KeySkipHost = "skip-host"
|
||||
KeyReposRoot = "root"
|
||||
)
|
||||
|
||||
// Defaults is a map of default values for config keys.
|
||||
var Defaults = map[string]string{
|
||||
KeyDefaultHost: "github.com",
|
||||
KeyOutput: OutTree,
|
||||
KeyReposRoot: fmt.Sprintf("~%c%s", filepath.Separator, "repositories"),
|
||||
// KeySkipHost: "false",
|
||||
KeyDefaultHost: "github.com",
|
||||
KeyOutput: OutTree,
|
||||
KeyReposRoot: fmt.Sprintf("~%c%s", filepath.Separator, "repositories"),
|
||||
KeyDefaultScheme: "ssh",
|
||||
}
|
||||
|
||||
// Values for the --out flag.
|
||||
|
||||
17
pkg/get.go
17
pkg/get.go
@@ -8,12 +8,13 @@ import (
|
||||
|
||||
// GetCfg provides configuration for the Get command.
|
||||
type GetCfg struct {
|
||||
Branch string
|
||||
DefHost string
|
||||
Dump string
|
||||
Root string
|
||||
SkipHost bool
|
||||
URL string
|
||||
Branch string
|
||||
DefHost string
|
||||
DefScheme string
|
||||
Dump string
|
||||
Root string
|
||||
SkipHost bool
|
||||
URL string
|
||||
}
|
||||
|
||||
// Get executes the "git get" command.
|
||||
@@ -33,7 +34,7 @@ func Get(c *GetCfg) error {
|
||||
}
|
||||
|
||||
func cloneSingleRepo(c *GetCfg) error {
|
||||
url, err := ParseURL(c.URL, c.DefHost)
|
||||
url, err := ParseURL(c.URL, c.DefHost, c.DefScheme)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -56,7 +57,7 @@ func cloneDumpFile(c *GetCfg) error {
|
||||
}
|
||||
|
||||
for _, line := range parsedLines {
|
||||
url, err := ParseURL(line.rawurl, c.DefHost)
|
||||
url, err := ParseURL(line.rawurl, c.DefHost, c.DefScheme)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
29
pkg/url.go
29
pkg/url.go
@@ -17,8 +17,9 @@ var errEmptyURLPath = errors.New("parsed URL path is empty")
|
||||
var scpSyntax = regexp.MustCompile(`^([a-zA-Z0-9_]+)@([a-zA-Z0-9._-]+):(.*)$`)
|
||||
|
||||
// ParseURL parses given rawURL string into a URL.
|
||||
// The defaultHost argument defines the host to use (eg, github.com) in case parsed URL has an empty host.
|
||||
func ParseURL(rawURL string, defaultHost string) (url *urlpkg.URL, err error) {
|
||||
// When the parsed URL has an empty host, use the defaultHost.
|
||||
// When the parsed URL has an empty scheme, use the defaultScheme.
|
||||
func ParseURL(rawURL string, defaultHost string, defaultScheme string) (url *urlpkg.URL, err error) {
|
||||
// If rawURL matches the SCP-like syntax, convert it into a standard ssh Path.
|
||||
// eg, git@github.com:user/repo => ssh://git@github.com/user/repo
|
||||
if m := scpSyntax.FindStringSubmatch(rawURL); m != nil {
|
||||
@@ -26,7 +27,7 @@ func ParseURL(rawURL string, defaultHost string) (url *urlpkg.URL, err error) {
|
||||
Scheme: "ssh",
|
||||
User: urlpkg.User(m[1]),
|
||||
Host: m[2],
|
||||
Path: m[3],
|
||||
Path: path.Join("/", m[3]),
|
||||
}
|
||||
} else {
|
||||
url, err = urlpkg.Parse(rawURL)
|
||||
@@ -43,14 +44,21 @@ func ParseURL(rawURL string, defaultHost string) (url *urlpkg.URL, err error) {
|
||||
url.Scheme = "ssh"
|
||||
}
|
||||
|
||||
// Default to "git" user when using ssh and no user is provided
|
||||
if url.Scheme == "ssh" && url.User == nil {
|
||||
url.User = urlpkg.User("git")
|
||||
}
|
||||
|
||||
// Default to configured defaultHost when host is empty
|
||||
if url.Host == "" {
|
||||
url.Host = defaultHost
|
||||
// Add a leading slash to path when host is missing. It's needed to correctly compare urlpkg.URL structs.
|
||||
url.Path = path.Join("/", url.Path)
|
||||
}
|
||||
|
||||
// Default to configured defaultScheme when scheme is empty
|
||||
if url.Scheme == "" {
|
||||
url.Scheme = defaultScheme
|
||||
}
|
||||
|
||||
// Default to "git" user when using ssh and no user is provided
|
||||
if url.Scheme == "ssh" && url.User == nil {
|
||||
url.User = urlpkg.User("git")
|
||||
}
|
||||
|
||||
// Don't use host when scheme is file://. The fragment detected as url.Host should be a first directory of url.Path
|
||||
@@ -59,11 +67,6 @@ func ParseURL(rawURL string, defaultHost string) (url *urlpkg.URL, err error) {
|
||||
url.Host = ""
|
||||
}
|
||||
|
||||
// Default to https when scheme is empty
|
||||
if url.Scheme == "" {
|
||||
url.Scheme = "https"
|
||||
}
|
||||
|
||||
return url, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,8 @@ package pkg
|
||||
import (
|
||||
"git-get/pkg/cfg"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// Following URLs are considered valid according to https://git-scm.com/docs/git-clone#_git_urls:
|
||||
@@ -50,16 +52,11 @@ func TestURLParse(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
url, err := ParseURL(test.in, cfg.Defaults[cfg.KeyDefaultHost])
|
||||
if err != nil {
|
||||
t.Fatalf("got error: %+v", err)
|
||||
}
|
||||
url, err := ParseURL(test.in, cfg.Defaults[cfg.KeyDefaultHost], cfg.Defaults[cfg.KeyDefaultScheme])
|
||||
assert.NoError(t, err)
|
||||
|
||||
got := URLToPath(*url, false)
|
||||
|
||||
if got != test.want {
|
||||
t.Errorf("wrong result for %q; expected %q; got %q", test.in, test.want, got)
|
||||
}
|
||||
assert.Equal(t, test.want, got)
|
||||
}
|
||||
}
|
||||
func TestURLParseSkipHost(t *testing.T) {
|
||||
@@ -95,16 +92,39 @@ func TestURLParseSkipHost(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
url, err := ParseURL(test.in, cfg.Defaults[cfg.KeyDefaultHost])
|
||||
if err != nil {
|
||||
t.Fatalf("got error: %+v", err)
|
||||
}
|
||||
url, err := ParseURL(test.in, cfg.Defaults[cfg.KeyDefaultHost], cfg.Defaults[cfg.KeyDefaultScheme])
|
||||
assert.NoError(t, err)
|
||||
|
||||
got := URLToPath(*url, true)
|
||||
assert.Equal(t, test.want, got)
|
||||
}
|
||||
}
|
||||
|
||||
if got != test.want {
|
||||
t.Errorf("wrong result for %q; expected %q; got %q", test.in, test.want, got)
|
||||
}
|
||||
func TestDefaultScheme(t *testing.T) {
|
||||
tests := []struct {
|
||||
in string
|
||||
scheme string
|
||||
want string
|
||||
}{
|
||||
{"grdl/git-get", "ssh", "ssh://git@github.com/grdl/git-get"},
|
||||
{"grdl/git-get", "https", "https://github.com/grdl/git-get"},
|
||||
{"https://github.com/grdl/git-get", "ssh", "https://github.com/grdl/git-get"},
|
||||
{"https://github.com/grdl/git-get", "https", "https://github.com/grdl/git-get"},
|
||||
{"ssh://github.com/grdl/git-get", "ssh", "ssh://git@github.com/grdl/git-get"},
|
||||
{"ssh://github.com/grdl/git-get", "https", "ssh://git@github.com/grdl/git-get"},
|
||||
{"git+ssh://github.com/grdl/git-get", "https", "ssh://git@github.com/grdl/git-get"},
|
||||
{"git@github.com:grdl/git-get", "ssh", "ssh://git@github.com/grdl/git-get"},
|
||||
{"git@github.com:grdl/git-get", "https", "ssh://git@github.com/grdl/git-get"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
url, err := ParseURL(test.in, cfg.Defaults[cfg.KeyDefaultHost], test.scheme)
|
||||
assert.NoError(t, err)
|
||||
|
||||
want, err := url.Parse(test.want)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, url, want)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,9 +139,8 @@ func TestInvalidURLParse(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, test := range invalidURLs {
|
||||
got, err := ParseURL(test, cfg.Defaults[cfg.KeyDefaultHost])
|
||||
if err == nil {
|
||||
t.Errorf("expected error; got %q", got)
|
||||
}
|
||||
_, err := ParseURL(test, cfg.Defaults[cfg.KeyDefaultHost], cfg.Defaults[cfg.KeyDefaultScheme])
|
||||
|
||||
assert.Error(t, err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user