2015-05-04 12:49:49 -05:00
|
|
|
package setup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/mholt/caddy/middleware"
|
|
|
|
"github.com/mholt/caddy/middleware/git"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Git configures a new Git service routine.
|
|
|
|
func Git(c *Controller) (middleware.Middleware, error) {
|
|
|
|
repo, err := gitParse(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Startup = append(c.Startup, func() error {
|
2015-05-25 22:44:47 -05:00
|
|
|
// Start service routine in background
|
|
|
|
git.Start(repo)
|
2015-05-04 12:49:49 -05:00
|
|
|
|
|
|
|
// Do a pull right away to return error
|
|
|
|
return repo.Pull()
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func gitParse(c *Controller) (*git.Repo, error) {
|
|
|
|
repo := &git.Repo{Branch: "master", Interval: git.DefaultInterval, Path: c.Root}
|
|
|
|
|
|
|
|
for c.Next() {
|
|
|
|
args := c.RemainingArgs()
|
|
|
|
|
|
|
|
switch len(args) {
|
|
|
|
case 2:
|
|
|
|
repo.Path = filepath.Clean(c.Root + string(filepath.Separator) + args[1])
|
|
|
|
fallthrough
|
|
|
|
case 1:
|
2015-05-24 21:52:34 -05:00
|
|
|
repo.URL = args[0]
|
2015-05-04 12:49:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for c.NextBlock() {
|
|
|
|
switch c.Val() {
|
|
|
|
case "repo":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return nil, c.ArgErr()
|
|
|
|
}
|
2015-05-24 21:52:34 -05:00
|
|
|
repo.URL = c.Val()
|
2015-05-04 12:49:49 -05:00
|
|
|
case "path":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return nil, c.ArgErr()
|
|
|
|
}
|
|
|
|
repo.Path = filepath.Clean(c.Root + string(filepath.Separator) + c.Val())
|
|
|
|
case "branch":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return nil, c.ArgErr()
|
|
|
|
}
|
|
|
|
repo.Branch = c.Val()
|
|
|
|
case "key":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return nil, c.ArgErr()
|
|
|
|
}
|
|
|
|
repo.KeyPath = c.Val()
|
|
|
|
case "interval":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return nil, c.ArgErr()
|
|
|
|
}
|
|
|
|
t, _ := strconv.Atoi(c.Val())
|
|
|
|
if t > 0 {
|
|
|
|
repo.Interval = time.Duration(t) * time.Second
|
|
|
|
}
|
|
|
|
case "then":
|
|
|
|
thenArgs := c.RemainingArgs()
|
|
|
|
if len(thenArgs) == 0 {
|
|
|
|
return nil, c.ArgErr()
|
|
|
|
}
|
|
|
|
repo.Then = strings.Join(thenArgs, " ")
|
2015-05-21 19:38:07 -05:00
|
|
|
default:
|
|
|
|
return nil, c.ArgErr()
|
2015-05-04 12:49:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if repo is not specified, return error
|
2015-05-24 21:52:34 -05:00
|
|
|
if repo.URL == "" {
|
2015-05-04 12:49:49 -05:00
|
|
|
return nil, c.ArgErr()
|
|
|
|
}
|
|
|
|
|
2015-05-24 21:52:34 -05:00
|
|
|
// if private key is not specified, convert repository URL to https
|
2015-05-04 12:49:49 -05:00
|
|
|
// to avoid ssh authentication
|
2015-05-24 21:52:34 -05:00
|
|
|
// else validate git URL
|
2015-05-04 12:49:49 -05:00
|
|
|
// Note: private key support not yet available on Windows
|
|
|
|
var err error
|
|
|
|
if repo.KeyPath == "" {
|
2015-05-24 21:52:34 -05:00
|
|
|
repo.URL, repo.Host, err = sanitizeHTTP(repo.URL)
|
2015-05-04 12:49:49 -05:00
|
|
|
} else {
|
2015-05-24 21:52:34 -05:00
|
|
|
repo.URL, repo.Host, err = sanitizeGit(repo.URL)
|
2015-05-04 12:49:49 -05:00
|
|
|
// TODO add Windows support for private repos
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return nil, fmt.Errorf("Private repository not yet supported on Windows")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-05-21 19:38:07 -05:00
|
|
|
// validate git requirements
|
|
|
|
if err = git.Init(); err != nil {
|
2015-05-04 12:49:49 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return repo, repo.Prepare()
|
|
|
|
}
|
|
|
|
|
2015-05-24 21:52:34 -05:00
|
|
|
// sanitizeHTTP cleans up repository URL and converts to https format
|
2015-05-04 12:49:49 -05:00
|
|
|
// if currently in ssh format.
|
|
|
|
// Returns sanitized url, hostName (e.g. github.com, bitbucket.com)
|
|
|
|
// and possible error
|
2015-05-24 21:52:34 -05:00
|
|
|
func sanitizeHTTP(repoURL string) (string, string, error) {
|
|
|
|
url, err := url.Parse(repoURL)
|
2015-05-04 12:49:49 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if url.Host == "" && strings.HasPrefix(url.Path, "git@") {
|
|
|
|
url.Path = url.Path[len("git@"):]
|
|
|
|
i := strings.Index(url.Path, ":")
|
|
|
|
if i < 0 {
|
2015-05-24 21:52:34 -05:00
|
|
|
return "", "", fmt.Errorf("Invalid git url %s", repoURL)
|
2015-05-04 12:49:49 -05:00
|
|
|
}
|
|
|
|
url.Host = url.Path[:i]
|
|
|
|
url.Path = "/" + url.Path[i+1:]
|
|
|
|
}
|
|
|
|
|
2015-05-24 21:52:34 -05:00
|
|
|
repoURL = "https://" + url.Host + url.Path
|
2015-05-21 19:38:07 -05:00
|
|
|
|
|
|
|
// add .git suffix if missing
|
2015-05-24 21:52:34 -05:00
|
|
|
if !strings.HasSuffix(repoURL, ".git") {
|
|
|
|
repoURL += ".git"
|
2015-05-21 19:38:07 -05:00
|
|
|
}
|
|
|
|
|
2015-05-24 21:52:34 -05:00
|
|
|
return repoURL, url.Host, nil
|
2015-05-04 12:49:49 -05:00
|
|
|
}
|
|
|
|
|
2015-05-21 19:38:07 -05:00
|
|
|
// sanitizeGit cleans up repository url and converts to ssh format for private
|
|
|
|
// repositories if required.
|
2015-05-04 12:49:49 -05:00
|
|
|
// Returns sanitized url, hostName (e.g. github.com, bitbucket.com)
|
|
|
|
// and possible error
|
2015-05-24 21:52:34 -05:00
|
|
|
func sanitizeGit(repoURL string) (string, string, error) {
|
|
|
|
repoURL = strings.TrimSpace(repoURL)
|
2015-05-21 19:38:07 -05:00
|
|
|
|
|
|
|
// check if valid ssh format
|
2015-05-24 21:52:34 -05:00
|
|
|
if !strings.HasPrefix(repoURL, "git@") || strings.Index(repoURL, ":") < len("git@a:") {
|
2015-05-21 19:38:07 -05:00
|
|
|
// check if valid http format and convert to ssh
|
2015-05-24 21:52:34 -05:00
|
|
|
if url, err := url.Parse(repoURL); err == nil && strings.HasPrefix(url.Scheme, "http") {
|
|
|
|
repoURL = fmt.Sprintf("git@%v:%v", url.Host, url.Path[1:])
|
2015-05-21 19:38:07 -05:00
|
|
|
} else {
|
2015-05-24 21:52:34 -05:00
|
|
|
return "", "", fmt.Errorf("Invalid git url %s", repoURL)
|
2015-05-21 19:38:07 -05:00
|
|
|
}
|
2015-05-04 12:49:49 -05:00
|
|
|
}
|
2015-05-24 21:52:34 -05:00
|
|
|
hostURL := repoURL[len("git@"):]
|
|
|
|
i := strings.Index(hostURL, ":")
|
|
|
|
host := hostURL[:i]
|
2015-05-21 19:38:07 -05:00
|
|
|
|
|
|
|
// add .git suffix if missing
|
2015-05-24 21:52:34 -05:00
|
|
|
if !strings.HasSuffix(repoURL, ".git") {
|
|
|
|
repoURL += ".git"
|
2015-05-21 19:38:07 -05:00
|
|
|
}
|
|
|
|
|
2015-05-24 21:52:34 -05:00
|
|
|
return repoURL, host, nil
|
2015-05-04 12:49:49 -05:00
|
|
|
}
|