2014-09-14 02:45:56 +02:00
|
|
|
package module
|
|
|
|
|
|
|
|
import (
|
2014-09-16 08:32:30 +02:00
|
|
|
"bytes"
|
2014-09-14 02:45:56 +02:00
|
|
|
"fmt"
|
|
|
|
"net/url"
|
2014-09-16 08:32:30 +02:00
|
|
|
"os/exec"
|
2014-09-16 08:48:56 +02:00
|
|
|
"regexp"
|
2014-09-16 08:32:30 +02:00
|
|
|
"syscall"
|
2014-09-14 02:45:56 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Getter defines the interface that schemes must implement to download
|
|
|
|
// and update modules.
|
|
|
|
type Getter interface {
|
|
|
|
// Get downloads the given URL into the given directory. This always
|
|
|
|
// assumes that we're updating and gets the latest version that it can.
|
|
|
|
//
|
|
|
|
// The directory may already exist (if we're updating). If it is in a
|
|
|
|
// format that isn't understood, an error should be returned. Get shouldn't
|
|
|
|
// simply nuke the directory.
|
|
|
|
Get(string, *url.URL) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Getters is the mapping of scheme to the Getter implementation that will
|
|
|
|
// be used to get a dependency.
|
|
|
|
var Getters map[string]Getter
|
|
|
|
|
2014-09-16 08:48:56 +02:00
|
|
|
// forcedRegexp is the regular expression that finds forced getters. This
|
|
|
|
// syntax is schema::url, example: git::https://foo.com
|
|
|
|
var forcedRegexp = regexp.MustCompile(`^([A-Za-z]+)::(.+)$`)
|
|
|
|
|
2014-09-14 02:45:56 +02:00
|
|
|
func init() {
|
2014-09-16 22:44:12 +02:00
|
|
|
httpGetter := new(HttpGetter)
|
|
|
|
|
2014-09-14 02:45:56 +02:00
|
|
|
Getters = map[string]Getter{
|
2014-09-16 22:44:12 +02:00
|
|
|
"file": new(FileGetter),
|
|
|
|
"git": new(GitGetter),
|
|
|
|
"hg": new(HgGetter),
|
|
|
|
"http": httpGetter,
|
|
|
|
"https": httpGetter,
|
2014-09-14 02:45:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get downloads the module specified by src into the folder specified by
|
|
|
|
// dst. If dst already exists, Get will attempt to update it.
|
|
|
|
//
|
|
|
|
// src is a URL, whereas dst is always just a file path to a folder. This
|
|
|
|
// folder doesn't need to exist. It will be created if it doesn't exist.
|
|
|
|
func Get(dst, src string) error {
|
2014-09-16 08:48:56 +02:00
|
|
|
var force string
|
|
|
|
force, src = getForcedGetter(src)
|
|
|
|
|
2014-09-14 02:45:56 +02:00
|
|
|
u, err := url.Parse(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-09-16 08:48:56 +02:00
|
|
|
if force == "" {
|
|
|
|
force = u.Scheme
|
|
|
|
}
|
2014-09-14 02:45:56 +02:00
|
|
|
|
2014-09-16 08:48:56 +02:00
|
|
|
g, ok := Getters[force]
|
2014-09-14 02:45:56 +02:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(
|
2014-09-16 08:48:56 +02:00
|
|
|
"module download not supported for scheme '%s'", force)
|
2014-09-14 02:45:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = g.Get(dst, u)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("error downloading module '%s': %s", src, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2014-09-16 08:32:30 +02:00
|
|
|
|
|
|
|
// getRunCommand is a helper that will run a command and capture the output
|
|
|
|
// in the case an error happens.
|
|
|
|
func getRunCommand(cmd *exec.Cmd) error {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
cmd.Stdout = &buf
|
|
|
|
cmd.Stderr = &buf
|
|
|
|
err := cmd.Run()
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if exiterr, ok := err.(*exec.ExitError); ok {
|
|
|
|
// The program has exited with an exit code != 0
|
|
|
|
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"%s exited with %d: %s",
|
|
|
|
cmd.Path,
|
|
|
|
status.ExitStatus(),
|
|
|
|
buf.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("error running %s: %s", cmd.Path, buf.String())
|
|
|
|
}
|
2014-09-16 08:48:56 +02:00
|
|
|
|
|
|
|
// getForcedGetter takes a source and returns the tuple of the forced
|
|
|
|
// getter and the raw URL (without the force syntax).
|
|
|
|
func getForcedGetter(src string) (string, string) {
|
|
|
|
var forced string
|
|
|
|
if ms := forcedRegexp.FindStringSubmatch(src); ms != nil {
|
|
|
|
forced = ms[1]
|
|
|
|
src = ms[2]
|
|
|
|
}
|
|
|
|
|
|
|
|
return forced, src
|
|
|
|
}
|