Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,10 @@ _testmain.go
*.exe
*.test
*.prof

# dep management files
glide.lock
vendor

# don't accidentally commit a build of main
boilr
9 changes: 9 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package: github.com/tmrts/boilr
import:
#- package: github.com/src-d/go-git
- package: github.com/spf13/cobra
- package: github.com/docker/go-units
- package: github.com/olekukonko/tablewriter
- package: github.com/fatih/color
- package: gopkg.in/src-d/go-git.v4
- package: github.com/src-d/crypto/ssh/knownhosts
7 changes: 5 additions & 2 deletions pkg/cmd/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"

cli "github.com/spf13/cobra"
//"github.com/src-d/go-git/plumbing/transport/ssh"

"github.com/tmrts/boilr/pkg/boilr"
"github.com/tmrts/boilr/pkg/host"
Expand All @@ -21,7 +22,7 @@ var Download = &cli.Command{
// FIXME Half-Updates leave messy templates
Run: func(c *cli.Command, args []string) {
MustValidateArgs(args, []validate.Argument{
{"template-repo", validate.UnixPath},
{"template-repo", validate.RepoURL},
{"template-tag", validate.AlphanumericExt},
})

Expand Down Expand Up @@ -50,7 +51,9 @@ var Download = &cli.Command{

// TODO(tmrts): allow fetching other branches than 'master'
if err := git.Clone(targetDir, git.CloneOptions{
URL: host.URL(templateURL),
URL: templateURL,
Auth: host.AuthMethodForURL(templateURL),
Progress: os.Stdout,
}); err != nil {
exit.Error(fmt.Errorf("download: %s", err))
}
Expand Down
49 changes: 0 additions & 49 deletions pkg/host/github.go

This file was deleted.

83 changes: 83 additions & 0 deletions pkg/host/host.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package host

import (
"bufio"
"fmt"
"os"
"regexp"
"strings"

"github.com/howeyc/gopass"
"gopkg.in/src-d/go-git.v4/plumbing/transport/http"

"gopkg.in/src-d/go-git.v4/plumbing/transport"
"gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
)

var (
scpLikeUrlRegExp = regexp.MustCompile("^(ssh://)?(?:(?P<user>[^@]+)(?:@))?(?P<host>[^:|/]+)(?::|/)?(?P<path>.+)$")
isHttpSchemeRegExp = regexp.MustCompile("^(http|https)://")
)

func IsRepoURL(url string) bool {
return isHttpSchemeRegExp.MatchString(url) || scpLikeUrlRegExp.MatchString(url)
}

func IsSSH(url string) bool {
if !isHttpSchemeRegExp.MatchString(url) && scpLikeUrlRegExp.MatchString(url) {
return true
}

return false
}

func AuthMethodForURL(url string) transport.AuthMethod {

if IsSSH(url) {
// 1:scheme, 2:user, 3:host, 4:path
m := scpLikeUrlRegExp.FindStringSubmatch(url)
a, _ := ssh.NewSSHAgentAuth(m[2])
return a
}

ba := http.NewBasicAuth("", "")
ba.CredentialsProvider = promptForCredentials

return ba
}

func promptForCredentials() (string, string) {
var u = ""
var p = ""

uname, err := promptForUsername()

if err == nil {
u = uname
}

pbytes, err := gopass.GetPasswdPrompt("password: ", true, os.Stdin, os.Stdout)

if err == nil {
p = string(pbytes)
}

return u, p
}

func promptForUsername() (string, error) {
consolereader := bufio.NewReader(os.Stdin)

fmt.Print("username: ")
response, err := consolereader.ReadString('\n')

if err != nil {
return "", err
}

if len(response) < 1 {
return promptForUsername()
}

return strings.TrimSuffix(response, "\n"), nil
}
31 changes: 31 additions & 0 deletions pkg/util/validate/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ package validate

import (
"reflect"
"regexp"
"runtime"
"strings"

"github.com/tmrts/boilr/pkg/util/validate/pattern"
)

var (
repoUrlRegExp = regexp.MustCompile("^(ssh://|https://)?(?:(?P<user>[^@]+)(?:@))?(?P<host>[^:|/]+)(?::|/)?(?P<path>.+)$")
)

// String is the validation function used for checking whether a string is valid or not.
type String func(string) bool

Expand Down Expand Up @@ -43,3 +48,29 @@ func Alphanumeric(s string) bool {
func AlphanumericExt(s string) bool {
return pattern.AlphanumericExt.MatchString(s)
}

// RepoURL validates whether a string is a valid repo url.
func RepoURL(s string) bool {
isurl := false
if repoUrlRegExp.MatchString(s) {
isurl = true
m := repoUrlRegExp.FindStringSubmatch(s)

//bad scheme
if m[1] == "http://" {
isurl = false
}

//blank host
if len(strings.TrimSpace(m[3])) < 1 {
isurl = false
}

//blank path
if len(strings.TrimSpace(m[4])) < 1 {
isurl = false
}
}

return isurl
}