Skip to content

Commit

Permalink
Add support for supplying --var file (#15)
Browse files Browse the repository at this point in the history
* Add support for --vars
  • Loading branch information
petemounce authored and fishnix committed Jul 6, 2018
1 parent 48cfe42 commit 9e36bb8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ There is an example packer build with goss tests in the `example/` directory.
"use_sudo": false,
"format": "",
"goss_file": "",
"vars_file": "",
"username": "",
"password": "",
"debug": false
Expand Down
31 changes: 29 additions & 2 deletions packer-provisioner-goss.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ type GossConfig struct {
// The --gossfile flag
GossFile string `mapstructure:"goss_file"`

// The --vars flag
// Optional file containing variables, used within GOSS templating.
// Must be one of the files contained in the Tests array.
// Can be YAML or JSON.
VarsFile string `mapstructure:"vars_file"`

// The remote folder where the goss tests will be uploaded to.
// This should be set to a pre-existing directory, it defaults to /tmp
RemoteFolder string `mapstructure:"remote_folder"`
Expand Down Expand Up @@ -169,6 +175,20 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
return fmt.Errorf("Error creating remote directory: %s", err)
}

if p.config.VarsFile != "" {
vf, err := os.Stat(p.config.VarsFile)
if err != nil {
return fmt.Errorf("Error stating file: %s", err)
}
if vf.Mode().IsRegular() {
ui.Message(fmt.Sprintf("Uploading vars file %s", p.config.VarsFile))
varsDest := filepath.ToSlash(filepath.Join(p.config.RemotePath, filepath.Base(p.config.VarsFile)))
if err := p.uploadFile(ui, comm, varsDest, p.config.VarsFile); err != nil {
return fmt.Errorf("Error uploading vars file: %s", err)
}
}
}

for _, src := range p.config.Tests {
s, err := os.Stat(src)
if err != nil {
Expand Down Expand Up @@ -235,8 +255,8 @@ func (p *Provisioner) runGoss(ui packer.Ui, comm packer.Communicator) error {
goss := fmt.Sprintf("%s", p.config.DownloadPath)
cmd := &packer.RemoteCmd{
Command: fmt.Sprintf(
"cd %s && %s %s %s %s validate %s",
p.config.RemotePath, p.enableSudo(), goss, p.config.GossFile, p.debug(), p.format()),
"cd %s && %s %s %s %s %s validate %s",
p.config.RemotePath, p.enableSudo(), goss, p.config.GossFile, p.vars(), p.debug(), p.format()),
}
if err := cmd.StartWithUi(comm, ui); err != nil {
return err
Expand All @@ -263,6 +283,13 @@ func (p *Provisioner) format() string {
return ""
}

func (p *Provisioner) vars() string {
if p.config.VarsFile != "" {
return fmt.Sprintf("--vars %s", filepath.ToSlash(filepath.Join(p.config.RemotePath, filepath.Base(p.config.VarsFile))))
}
return ""
}

func (p *Provisioner) sslFlag(cmdType string) string {
if p.config.SkipSSLChk {
switch(cmdType) {
Expand Down

0 comments on commit 9e36bb8

Please sign in to comment.