From 4be9dfd1838ab09650083549b51a4762ac797fcb Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Thu, 3 Jun 2021 13:50:34 +0200 Subject: [PATCH] Say so when an only or an except option did not match anything (#11050) * Say so when an only or an except option did not match anything --- hcl2template/types.packer_config.go | 20 ++++++++++++++++++++ packer/run_interfaces.go | 3 +++ 2 files changed, 23 insertions(+) diff --git a/hcl2template/types.packer_config.go b/hcl2template/types.packer_config.go index e09623901..4401d6813 100644 --- a/hcl2template/types.packer_config.go +++ b/hcl2template/types.packer_config.go @@ -422,6 +422,7 @@ func (cfg *PackerConfig) getCoreBuildPostProcessors(source SourceUseBlock, block func (cfg *PackerConfig) GetBuilds(opts packer.GetBuildsOptions) ([]packersdk.Build, hcl.Diagnostics) { res := []packersdk.Build{} var diags hcl.Diagnostics + possibleBuildNames := []string{} cfg.debug = opts.Debug cfg.force = opts.Force @@ -447,6 +448,7 @@ func (cfg *PackerConfig) GetBuilds(opts packer.GetBuildsOptions) ([]packersdk.Bu // Apply the -only and -except command-line options to exclude matching builds. buildName := pcb.Name() + possibleBuildNames = append(possibleBuildNames, buildName) // -only if len(opts.Only) > 0 { onlyGlobs, diags := convertFilterOption(opts.Only, "only") @@ -464,6 +466,7 @@ func (cfg *PackerConfig) GetBuilds(opts packer.GetBuildsOptions) ([]packersdk.Bu if !include { continue } + opts.OnlyMatches++ } // -except @@ -481,6 +484,7 @@ func (cfg *PackerConfig) GetBuilds(opts packer.GetBuildsOptions) ([]packersdk.Bu } } if exclude { + opts.ExceptMatches++ continue } } @@ -550,6 +554,22 @@ func (cfg *PackerConfig) GetBuilds(opts packer.GetBuildsOptions) ([]packersdk.Bu res = append(res, pcb) } } + if len(opts.Only) > opts.OnlyMatches { + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagWarning, + Summary: "an 'only' option was passed, but not all matches were found for the given build.", + Detail: fmt.Sprintf("Possible build names: %v.\n"+ + "These could also be matched with a glob pattern like: 'happycloud.*'", possibleBuildNames), + }) + } + if len(opts.Except) > opts.ExceptMatches { + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagWarning, + Summary: "an 'except' option was passed, but did not match any build.", + Detail: fmt.Sprintf("Possible build names: %v.\n"+ + "These could also be matched with a glob pattern like: 'happycloud.*'", possibleBuildNames), + }) + } return res, diags } diff --git a/packer/run_interfaces.go b/packer/run_interfaces.go index f7701edb9..c8c777d31 100644 --- a/packer/run_interfaces.go +++ b/packer/run_interfaces.go @@ -12,6 +12,9 @@ type GetBuildsOptions struct { Except, Only []string Debug, Force bool OnError string + + // count only/except match count; so say something when nothing matched. + ExceptMatches, OnlyMatches int } type BuildGetter interface {