config/module: Mercurial support

This commit is contained in:
Mitchell Hashimoto 2014-09-16 09:30:31 -07:00
parent fc71d7091f
commit 7e94f7d4a9
20 changed files with 111 additions and 0 deletions

View File

@ -33,6 +33,7 @@ func init() {
Getters = map[string]Getter{
"file": new(FileGetter),
"git": new(GitGetter),
"hg": new(HgGetter),
}
}

51
config/module/get_hg.go Normal file
View File

@ -0,0 +1,51 @@
package module
import (
"fmt"
"net/url"
"os"
"os/exec"
)
// HgGetter is a Getter implementation that will download a module from
// a Mercurial repository.
type HgGetter struct{}
func (g *HgGetter) Get(dst string, u *url.URL) error {
if _, err := exec.LookPath("hg"); err != nil {
return fmt.Errorf("hg must be available and on the PATH")
}
_, err := os.Stat(dst)
if err != nil && !os.IsNotExist(err) {
return err
}
if err != nil {
if err := g.clone(dst, u); err != nil {
return err
}
}
if err:= g.pull(dst, u); err != nil {
return err
}
return g.update(dst, u)
}
func (g *HgGetter) clone(dst string, u *url.URL) error {
cmd := exec.Command("hg", "clone", "-U", u.String(), dst)
return getRunCommand(cmd)
}
func (g *HgGetter) pull(dst string, u *url.URL) error {
cmd := exec.Command("hg", "pull")
cmd.Dir = dst
return getRunCommand(cmd)
}
func (g *HgGetter) update(dst string, u *url.URL) error {
cmd := exec.Command("hg", "update")
cmd.Dir = dst
return getRunCommand(cmd)
}

View File

@ -0,0 +1,41 @@
package module
import (
"os"
"os/exec"
"path/filepath"
"testing"
)
var testHasHg bool
func init() {
if _, err := exec.LookPath("hg"); err == nil {
testHasHg = true
}
}
func TestHgGetter_impl(t *testing.T) {
var _ Getter = new(HgGetter)
}
func TestHgGetter(t *testing.T) {
if !testHasHg {
t.Log("hg not found, skipping")
t.Skip()
}
g := new(HgGetter)
dst := tempDir(t)
// With a dir that doesn't exist
if err := g.Get(dst, testModuleURL("basic-hg")); err != nil {
t.Fatalf("err: %s", err)
}
// Verify the main file exists
mainPath := filepath.Join(dst, "main.tf")
if _, err := os.Stat(mainPath); err != nil {
t.Fatalf("err: %s", err)
}
}

Binary file not shown.

View File

@ -0,0 +1,2 @@
dcaed7754d58264cb9a5916215a5442377307bd1 0
dcaed7754d58264cb9a5916215a5442377307bd1 o default

Binary file not shown.

View File

@ -0,0 +1,2 @@
Commit

View File

@ -0,0 +1,4 @@
dotencode
fncache
revlogv1
store

View File

@ -0,0 +1 @@
data/main.tf.i

View File

@ -0,0 +1 @@
1 dcaed7754d58264cb9a5916215a5442377307bd1

Binary file not shown.

View File

@ -0,0 +1 @@
default

View File

@ -0,0 +1,2 @@
0
commit

Binary file not shown.

View File

@ -0,0 +1,5 @@
# Hello
module "foo" {
source = "./foo"
}