diff --git a/config/module/get.go b/config/module/get.go index 9a459760c..5131d7384 100644 --- a/config/module/get.go +++ b/config/module/get.go @@ -33,6 +33,7 @@ func init() { Getters = map[string]Getter{ "file": new(FileGetter), "git": new(GitGetter), + "hg": new(HgGetter), } } diff --git a/config/module/get_hg.go b/config/module/get_hg.go new file mode 100644 index 000000000..2bad0143a --- /dev/null +++ b/config/module/get_hg.go @@ -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) +} diff --git a/config/module/get_hg_test.go b/config/module/get_hg_test.go new file mode 100644 index 000000000..71631a715 --- /dev/null +++ b/config/module/get_hg_test.go @@ -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) + } +} diff --git a/config/module/test-fixtures/basic-hg/.hg/00changelog.i b/config/module/test-fixtures/basic-hg/.hg/00changelog.i new file mode 100644 index 000000000..d3a831105 Binary files /dev/null and b/config/module/test-fixtures/basic-hg/.hg/00changelog.i differ diff --git a/config/module/test-fixtures/basic-hg/.hg/cache/branch2-served b/config/module/test-fixtures/basic-hg/.hg/cache/branch2-served new file mode 100644 index 000000000..1bae0f6bc --- /dev/null +++ b/config/module/test-fixtures/basic-hg/.hg/cache/branch2-served @@ -0,0 +1,2 @@ +dcaed7754d58264cb9a5916215a5442377307bd1 0 +dcaed7754d58264cb9a5916215a5442377307bd1 o default diff --git a/config/module/test-fixtures/basic-hg/.hg/dirstate b/config/module/test-fixtures/basic-hg/.hg/dirstate new file mode 100644 index 000000000..53f3a9bc6 Binary files /dev/null and b/config/module/test-fixtures/basic-hg/.hg/dirstate differ diff --git a/config/module/test-fixtures/basic-hg/.hg/last-message.txt b/config/module/test-fixtures/basic-hg/.hg/last-message.txt new file mode 100644 index 000000000..e8f25bcfc --- /dev/null +++ b/config/module/test-fixtures/basic-hg/.hg/last-message.txt @@ -0,0 +1,2 @@ +Commit + diff --git a/config/module/test-fixtures/basic-hg/.hg/requires b/config/module/test-fixtures/basic-hg/.hg/requires new file mode 100644 index 000000000..f634f664b --- /dev/null +++ b/config/module/test-fixtures/basic-hg/.hg/requires @@ -0,0 +1,4 @@ +dotencode +fncache +revlogv1 +store diff --git a/config/module/test-fixtures/basic-hg/.hg/store/00changelog.i b/config/module/test-fixtures/basic-hg/.hg/store/00changelog.i new file mode 100644 index 000000000..64e5cb6dc Binary files /dev/null and b/config/module/test-fixtures/basic-hg/.hg/store/00changelog.i differ diff --git a/config/module/test-fixtures/basic-hg/.hg/store/00manifest.i b/config/module/test-fixtures/basic-hg/.hg/store/00manifest.i new file mode 100644 index 000000000..25ad8dc2a Binary files /dev/null and b/config/module/test-fixtures/basic-hg/.hg/store/00manifest.i differ diff --git a/config/module/test-fixtures/basic-hg/.hg/store/data/main.tf.i b/config/module/test-fixtures/basic-hg/.hg/store/data/main.tf.i new file mode 100644 index 000000000..f45ddc33f Binary files /dev/null and b/config/module/test-fixtures/basic-hg/.hg/store/data/main.tf.i differ diff --git a/config/module/test-fixtures/basic-hg/.hg/store/fncache b/config/module/test-fixtures/basic-hg/.hg/store/fncache new file mode 100644 index 000000000..af601aa05 --- /dev/null +++ b/config/module/test-fixtures/basic-hg/.hg/store/fncache @@ -0,0 +1 @@ +data/main.tf.i diff --git a/config/module/test-fixtures/basic-hg/.hg/store/phaseroots b/config/module/test-fixtures/basic-hg/.hg/store/phaseroots new file mode 100644 index 000000000..a08565294 --- /dev/null +++ b/config/module/test-fixtures/basic-hg/.hg/store/phaseroots @@ -0,0 +1 @@ +1 dcaed7754d58264cb9a5916215a5442377307bd1 diff --git a/config/module/test-fixtures/basic-hg/.hg/store/undo b/config/module/test-fixtures/basic-hg/.hg/store/undo new file mode 100644 index 000000000..9dd8c8097 Binary files /dev/null and b/config/module/test-fixtures/basic-hg/.hg/store/undo differ diff --git a/config/module/test-fixtures/basic-hg/.hg/store/undo.phaseroots b/config/module/test-fixtures/basic-hg/.hg/store/undo.phaseroots new file mode 100644 index 000000000..e69de29bb diff --git a/config/module/test-fixtures/basic-hg/.hg/undo.bookmarks b/config/module/test-fixtures/basic-hg/.hg/undo.bookmarks new file mode 100644 index 000000000..e69de29bb diff --git a/config/module/test-fixtures/basic-hg/.hg/undo.branch b/config/module/test-fixtures/basic-hg/.hg/undo.branch new file mode 100644 index 000000000..331d858ce --- /dev/null +++ b/config/module/test-fixtures/basic-hg/.hg/undo.branch @@ -0,0 +1 @@ +default \ No newline at end of file diff --git a/config/module/test-fixtures/basic-hg/.hg/undo.desc b/config/module/test-fixtures/basic-hg/.hg/undo.desc new file mode 100644 index 000000000..37970a278 --- /dev/null +++ b/config/module/test-fixtures/basic-hg/.hg/undo.desc @@ -0,0 +1,2 @@ +0 +commit diff --git a/config/module/test-fixtures/basic-hg/.hg/undo.dirstate b/config/module/test-fixtures/basic-hg/.hg/undo.dirstate new file mode 100644 index 000000000..b48d181be Binary files /dev/null and b/config/module/test-fixtures/basic-hg/.hg/undo.dirstate differ diff --git a/config/module/test-fixtures/basic-hg/main.tf b/config/module/test-fixtures/basic-hg/main.tf new file mode 100644 index 000000000..383063715 --- /dev/null +++ b/config/module/test-fixtures/basic-hg/main.tf @@ -0,0 +1,5 @@ +# Hello + +module "foo" { + source = "./foo" +}