From 6137c57c7dfd6e3a1e4fd320531c291f7a9f6621 Mon Sep 17 00:00:00 2001 From: Yusuke Kuoka Date: Tue, 15 Sep 2020 17:05:39 +0900 Subject: [PATCH] Enhance `import` and `imports` to support importing from absolute paths to directories (#25) `import` and `imports` only supported relative paths to local directories or Git URLs before. Since this change, it accepts a configuration like the below: ``` imports = [ "/variant/modules/mycommmand/myjob/mynestedjob" ] ``` --- README.md | 12 +++++++++- pkg/app/load.go | 10 +++++--- pkg/app/load_test.go | 55 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 pkg/app/load_test.go diff --git a/README.md b/README.md index 0732a0b..82cafb9 100644 --- a/README.md +++ b/README.md @@ -238,7 +238,9 @@ job "baz" { } ``` -Okay that works. But you ended up too many files in a single directory? `import = "path/to/dir"` can be used to load all the `*.variant` files in the directory into the current `job`. +Okay that works. But you ended up too many files in a single directory? + +A "parent" variant file containing `import` or `imports` can be used to load all the `*.variant` files in the directory into the current `job`. `path/to/yourcmd.variant`: ```hcl @@ -260,6 +262,14 @@ parameter "param1" { # snip ``` +Note that `imports` is the newer variant of `import` that supports multiple sources to be imported. + +Also, you can import following sources: + +- Relative path to local directory (A local path that doesn't start with `/`, like `foo/bar`) +- Absolute path to local directory (An absolute path that starts with `/`, like `/variant/modules/example.com/foo/bar`) +- URLs to Git repository (`REPO_URL@PATH/TO/DIR?ref=BRANCH`, e.g. `git::ssh://git@github.com/mumoshu/variant2@examples/advanced/import/foo?ref=master`) + See the [import](/examples/advanced/import) example for the full declaration of this command for reference. ## Concurrency diff --git a/pkg/app/load.go b/pkg/app/load.go index 9a02ed7..1d368e4 100644 --- a/pkg/app/load.go +++ b/pkg/app/load.go @@ -165,20 +165,24 @@ func New(setup Setup) (*App, error) { return app, err } - return newApp(app, cc, NewImportFunc(instance.Dir)) + return newApp(app, cc, NewImportFunc(instance.Dir, func(path string) (*App, error) { + return New(FromDir(path)) + })) } -func NewImportFunc(importBaseDir string) func(string) (*App, error) { +func NewImportFunc(importBaseDir string, f func(string) (*App, error)) func(string) (*App, error) { return func(dir string) (*App, error) { var d string if strings.Contains(dir, ":") { d = dir + } else if dir[0] == '/' { + d = dir } else { d = filepath.Join(importBaseDir, dir) } - return New(FromDir(d)) + return f(d) } } diff --git a/pkg/app/load_test.go b/pkg/app/load_test.go new file mode 100644 index 0000000..b9f5355 --- /dev/null +++ b/pkg/app/load_test.go @@ -0,0 +1,55 @@ +package app + +import ( + "testing" + + "github.com/google/go-cmp/cmp" +) + +func TestNewImportFunc(t *testing.T) { + type testcase struct { + subject string + path string + want string + } + + testcases := []testcase{ + { + subject: "relative path", + path: "foo/bar", + want: "sub/foo/bar", + }, + { + subject: "git url", + path: "git::ssh://git@github.com/mumoshu/variant2@examples/advanced/import/foo?ref=master", + want: "git::ssh://git@github.com/mumoshu/variant2@examples/advanced/import/foo?ref=master", + }, + { + subject: "absolute path", + path: "/a/b/c", + want: "/a/b/c", + }, + } + + for i := range testcases { + tc := testcases[i] + + t.Run(tc.subject, func(t *testing.T) { + a := &App{} + + f := NewImportFunc("sub", func(path string) (*App, error) { + if d := cmp.Diff(tc.want, path); d != "" { + t.Errorf("%s: %s", tc.subject, d) + } + + return a, nil + }) + + r, _ := f(tc.path) + + if r != a { + t.Fatalf("%s: unexpected result returned", tc.subject) + } + }) + } +}