Skip to content

Commit

Permalink
feat: imports = ["./path/or/url/to/dir"] for importing multiple var…
Browse files Browse the repository at this point in the history
…iant commands
  • Loading branch information
mumoshu committed Aug 9, 2020
1 parent cbb9ec6 commit 9ca9dc3
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 25 deletions.
6 changes: 6 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ func TestExamples(t *testing.T) {
args: []string{"variant", "test"},
wd: "./examples/advanced/import",
},
{
subject: "import-multi",
variantName: "",
args: []string{"variant", "test"},
wd: "./examples/advanced/import-multi",
},
{
subject: "options",
variantName: "",
Expand Down
63 changes: 43 additions & 20 deletions pkg/app/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,18 @@ func newConfigFromDir(dirPathOrURL string) (map[string]*hcl.File, *HCL2Config, e

us := forcePrefix + "::" + u.String()

cacheDir, err := remote.ResolveDir(us)
var cacheDir string

u2, err := depresolver.Parse(us)
if err != nil {
return nil, nil, err
}

if u2.File != "" {
cacheDir, err = remote.ResolveFile(us)
} else {
cacheDir, err = remote.ResolveDir(us)
}
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -237,17 +248,26 @@ func newApp(app *App, cc *HCL2Config, importDir func(string) (*App, error)) (*Ap
for _, j := range jobs {
jobByName[j.Name] = j

var importSources []string
if j.Imports != nil {
importSources = append(importSources, *j.Imports...)
}
if j.Import != nil {
a, err := importDir(*j.Import)
importSources = append(importSources, *j.Import)
}

if err != nil {
return nil, err
}
if len(importSources) > 0 {
for _, src := range importSources {
a, err := importDir(src)

if err != nil {
return nil, err
}

importedJobs := append([]JobSpec{a.Config.JobSpec}, a.Config.Jobs...)
for _, importedJob := range importedJobs {
var newJobName string

importedJobs := append([]JobSpec{a.Config.JobSpec}, a.Config.Jobs...)
for _, importedJob := range importedJobs {
var newJobName string
if j.Name == "" {
if importedJob.Name == "" {
// Do not override global parameters and options.
//
Expand All @@ -259,20 +279,23 @@ func newApp(app *App, cc *HCL2Config, importDir func(string) (*App, error)) (*Ap
}
importedJob = *merged
}
newJobName = importedJob.Name
} else if importedJob.Name != "" {
newJobName = fmt.Sprintf("%s %s", j.Name, importedJob.Name)
} else {
// Import the top-level job in the library as the non-top-level job on the user side
newJobName = j.Name
}

importedJob.Name = newJobName
if j.Name == "" {
newJobName = importedJob.Name
} else if importedJob.Name != "" {
newJobName = fmt.Sprintf("%s %s", j.Name, importedJob.Name)
} else {
// Import the top-level job in the library as the non-top-level job on the user side
newJobName = j.Name
}

importedJob.Name = newJobName

jobByName[newJobName] = importedJob
jobByName[newJobName] = importedJob

if j.Name == "" && importedJob.Name == "" {
conf = a.Config
if j.Name == "" && importedJob.Name == "" {
conf = a.Config
}
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/app/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,12 @@ type JobSpec struct {

SourceLocator hcl.Expression `hcl:"__source_locator,attr"`

Deps []DependsOn `hcl:"depends_on,block"`
Exec *Exec `hcl:"exec,block"`
Assert []Assert `hcl:"assert,block"`
Fail hcl.Expression `hcl:"fail,attr"`
Import *string `hcl:"import,attr"`
Deps []DependsOn `hcl:"depends_on,block"`
Exec *Exec `hcl:"exec,block"`
Assert []Assert `hcl:"assert,block"`
Fail hcl.Expression `hcl:"fail,attr"`
Import *string `hcl:"import,attr"`
Imports *[]string `hcl:"imports,attr"`

// Private hides the job from `variant run -h` when set to true
Private *bool `hcl:"private,attr"`
Expand Down

0 comments on commit 9ca9dc3

Please sign in to comment.