From 7e94f7d4a912cf1ce35c1822e0c65a16bac1ef50 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 16 Sep 2014 09:30:31 -0700 Subject: [PATCH] config/module: Mercurial support --- config/module/get.go | 1 + config/module/get_hg.go | 51 ++++++++++++++++++ config/module/get_hg_test.go | 41 ++++++++++++++ .../test-fixtures/basic-hg/.hg/00changelog.i | Bin 0 -> 57 bytes .../basic-hg/.hg/cache/branch2-served | 2 + .../test-fixtures/basic-hg/.hg/dirstate | Bin 0 -> 64 bytes .../basic-hg/.hg/last-message.txt | 2 + .../test-fixtures/basic-hg/.hg/requires | 4 ++ .../basic-hg/.hg/store/00changelog.i | Bin 0 -> 176 bytes .../basic-hg/.hg/store/00manifest.i | Bin 0 -> 114 bytes .../basic-hg/.hg/store/data/main.tf.i | Bin 0 -> 112 bytes .../test-fixtures/basic-hg/.hg/store/fncache | 1 + .../basic-hg/.hg/store/phaseroots | 1 + .../test-fixtures/basic-hg/.hg/store/undo | Bin 0 -> 58 bytes .../basic-hg/.hg/store/undo.phaseroots | 0 .../test-fixtures/basic-hg/.hg/undo.bookmarks | 0 .../test-fixtures/basic-hg/.hg/undo.branch | 1 + .../test-fixtures/basic-hg/.hg/undo.desc | 2 + .../test-fixtures/basic-hg/.hg/undo.dirstate | Bin 0 -> 64 bytes config/module/test-fixtures/basic-hg/main.tf | 5 ++ 20 files changed, 111 insertions(+) create mode 100644 config/module/get_hg.go create mode 100644 config/module/get_hg_test.go create mode 100644 config/module/test-fixtures/basic-hg/.hg/00changelog.i create mode 100644 config/module/test-fixtures/basic-hg/.hg/cache/branch2-served create mode 100644 config/module/test-fixtures/basic-hg/.hg/dirstate create mode 100644 config/module/test-fixtures/basic-hg/.hg/last-message.txt create mode 100644 config/module/test-fixtures/basic-hg/.hg/requires create mode 100644 config/module/test-fixtures/basic-hg/.hg/store/00changelog.i create mode 100644 config/module/test-fixtures/basic-hg/.hg/store/00manifest.i create mode 100644 config/module/test-fixtures/basic-hg/.hg/store/data/main.tf.i create mode 100644 config/module/test-fixtures/basic-hg/.hg/store/fncache create mode 100644 config/module/test-fixtures/basic-hg/.hg/store/phaseroots create mode 100644 config/module/test-fixtures/basic-hg/.hg/store/undo create mode 100644 config/module/test-fixtures/basic-hg/.hg/store/undo.phaseroots create mode 100644 config/module/test-fixtures/basic-hg/.hg/undo.bookmarks create mode 100644 config/module/test-fixtures/basic-hg/.hg/undo.branch create mode 100644 config/module/test-fixtures/basic-hg/.hg/undo.desc create mode 100644 config/module/test-fixtures/basic-hg/.hg/undo.dirstate create mode 100644 config/module/test-fixtures/basic-hg/main.tf 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 0000000000000000000000000000000000000000..d3a8311050e54c57c5be7cfe169e60a95768812c GIT binary patch literal 57 zcmWN_K?=Yi3=^>}vhYRG!~=>ejxFxg7V^;JV-L`jXd&PhOer=+rTh@j}%%hBW{9?W+v?SN{IR J{@{bE4*+3SNfH16 literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..25ad8dc2a4d87b479772db8075e653f37c0a1e30 GIT binary patch literal 114 zcmZQzWME`~03#q}2xT+;hXRIf+oy9R`GtjZS|7jmaf#kx&QzG literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..f45ddc33f19db6ef2a8f723ce4e36e10db12a436 GIT binary patch literal 112 zcmZQzWME`~00SVU4`nm_hk{)i%ZqQMPS2Zr>zb=wCx2OxuW=Ym4TvsPR`5v8$;s#9 n%FRzH%}G^IO3TkzQmE!q0D|KD(xT*41zQCrJ$;ZcS1lI+ym=or literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..9dd8c80976c979b46d2068ac7111685daae78167 GIT binary patch literal 58 zcmYdEEJ@VQP0Y;GD@oJKWH8_|Fvv~J%S=lxE`f5BGZORCQ*-ju!Thwmlt D9$*ox literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..b48d181be69e29d1966256b75b6883b252674ee0 GIT binary patch literal 64 acmZQzAPyvgl>LVSAd@{eF*8rEBn<$M@(T_C literal 0 HcmV?d00001 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" +}