@ -7,8 +7,6 @@ import (
"github.com/hashicorp/packer/helper/common"
"github.com/hashicorp/packer/packer"
"github.com/zclconf/go-cty/cty"
"github.com/gobwas/glob"
)
// PackerConfig represents a loaded Packer HCL config. It will contain
@ -253,10 +251,10 @@ func (cfg *PackerConfig) getCoreBuildPostProcessors(source *SourceBlock, blocks
return res , diags
}
// getBuilds will return a list of packer Build based on the HCL2 parsed build
// GetBuilds returns a list of packer Build based on the HCL2 parsed build
// blocks. All Builders, Provisioners and Post Processors will be started and
// configured.
func ( cfg * PackerConfig ) getBuilds( onlyGlobs [ ] glob . Glob , exceptGlobs [ ] glob . Glob ) ( [ ] packer . Build , hcl . Diagnostics ) {
func ( cfg * PackerConfig ) GetBuilds( only , except [ ] string ) ( [ ] packer . Build , hcl . Diagnostics ) {
res := [ ] packer . Build { }
var diags hcl . Diagnostics
@ -276,7 +274,11 @@ func (cfg *PackerConfig) getBuilds(onlyGlobs []glob.Glob, exceptGlobs []glob.Glo
buildName := fmt . Sprintf ( "%s.%s" , src . Type , src . Name )
// -only
if len ( onlyGlobs ) > 0 {
if len ( only ) > 0 {
onlyGlobs , diags := convertFilterOption ( only , "only" )
if diags . HasErrors ( ) {
return nil , diags
}
include := false
for _ , onlyGlob := range onlyGlobs {
if onlyGlob . Match ( buildName ) {
@ -290,7 +292,11 @@ func (cfg *PackerConfig) getBuilds(onlyGlobs []glob.Glob, exceptGlobs []glob.Glo
}
// -except
if len ( exceptGlobs ) > 0 {
if len ( except ) > 0 {
exceptGlobs , diags := convertFilterOption ( except , "except" )
if diags . HasErrors ( ) {
return nil , diags
}
exclude := false
for _ , exceptGlob := range exceptGlobs {
if exceptGlob . Match ( buildName ) {
@ -356,60 +362,3 @@ func (cfg *PackerConfig) getBuilds(onlyGlobs []glob.Glob, exceptGlobs []glob.Glo
}
return res , diags
}
// Convert -only and -except globs to glob.Glob instances.
func convertFilterOption ( patterns [ ] string , optionName string ) ( [ ] glob . Glob , hcl . Diagnostics ) {
var globs [ ] glob . Glob
var diags hcl . Diagnostics
for _ , pattern := range patterns {
g , err := glob . Compile ( pattern )
if err != nil {
diags = append ( diags , & hcl . Diagnostic {
Summary : fmt . Sprintf ( "Invalid -%s pattern %s: %s" , optionName , pattern , err ) ,
Severity : hcl . DiagError ,
} )
}
globs = append ( globs , g )
}
return globs , diags
}
// Parse will parse HCL file(s) in path. Path can be a folder or a file.
//
// Parse will first parse variables and then the rest; so that interpolation
// can happen.
//
// For each build block a packer.Build will be started, and for each builder,
// all provisioners and post-processors will be started.
//
// Parse then return a slice of packer.Builds; which are what packer core uses
// to run builds.
func ( p * Parser ) Parse ( path string , varFiles [ ] string , argVars map [ string ] string , onlyBuilds [ ] string , exceptBuilds [ ] string ) ( [ ] packer . Build , hcl . Diagnostics ) {
var onlyGlobs [ ] glob . Glob
if len ( onlyBuilds ) > 0 {
og , diags := convertFilterOption ( onlyBuilds , "only" )
if diags . HasErrors ( ) {
return nil , diags
}
onlyGlobs = og
}
var exceptGlobs [ ] glob . Glob
if len ( exceptBuilds ) > 0 {
eg , diags := convertFilterOption ( exceptBuilds , "except" )
if diags . HasErrors ( ) {
return nil , diags
}
exceptGlobs = eg
}
cfg , diags := p . parse ( path , varFiles , argVars )
if diags . HasErrors ( ) {
return nil , diags
}
builds , moreDiags := cfg . getBuilds ( onlyGlobs , exceptGlobs )
return builds , append ( diags , moreDiags ... )
}