Skip to content

Commit

Permalink
Merge pull request #60 from ministryofjustice/fix-nil-pointer
Browse files Browse the repository at this point in the history
- Added trimming for the workspaces, so they don't include whitespaces - Fix the nil pointer when terraform fail to excute.
  • Loading branch information
Alejandro Garrido Mota authored Nov 10, 2020
2 parents 2b915dd + f9985b9 commit 91fcf2e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pkg/commands/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

// This MUST match the number of the latest release on github
var Version = "1.6.2"
var Version = "1.6.3"

const owner = "ministryofjustice"
const repoName = "cloud-platform-cli"
Expand Down
27 changes: 13 additions & 14 deletions pkg/terraform/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ func (s *Commander) Terraform(args ...string) (*CmdOutput, error) {
ws := exitError.Sys().(syscall.WaitStatus)
exitCode = ws.ExitStatus()
}
contextLogger.Error("cmd.Run() failed")
contextLogger.Error("cmd.Run() failed with:")
contextLogger.Error(cmd.Stderr)
return nil, err
} else {
ws := cmd.ProcessState.Sys().(syscall.WaitStatus)
Expand Down Expand Up @@ -189,13 +190,11 @@ func (s *Commander) Apply() error {
)

output, err := s.Terraform(arg...)

if err != nil {
log.Error(err)
log.Error(output.Stderr)
return err
}

if s.DisplayTfOutput {
if s.DisplayTfOutput && output != nil {
output.redacted()
}

Expand Down Expand Up @@ -235,13 +234,11 @@ func (s *Commander) Plan() error {
)

output, err := s.Terraform(arg...)

if err != nil {
log.Error(err)
log.Error(output.Stderr)
return err
}

if s.DisplayTfOutput {
if s.DisplayTfOutput && output != nil {
output.redacted()
}

Expand All @@ -262,11 +259,13 @@ func (c *Commander) workspaces() ([]string, error) {

output, err := c.Terraform(arg...)
if err != nil {
log.Error(output.Stderr)
return nil, err
}

ws := strings.Split(output.Stdout, "\n")
for i := range ws {
ws[i] = strings.TrimSpace(ws[i])
}

return ws, nil
}
Expand Down Expand Up @@ -295,7 +294,7 @@ func (c *Commander) BulkPlan() error {
return err
}

if contains(ws, " live-1") {
if contains(ws, "live-1") {
log.WithFields(log.Fields{"dir": dir}).Info("Using live-1 context with: KUBE_CTX=live-1.cloud-platform.service.justice.gov.uk")

c.cmdEnv = append(os.Environ(), "KUBE_CTX=live-1.cloud-platform.service.justice.gov.uk")
Expand All @@ -305,7 +304,7 @@ func (c *Commander) BulkPlan() error {
if err != nil {
return err
}
} else if contains(ws, " manager") {
} else if contains(ws, "manager") {
log.WithFields(log.Fields{"dir": dir}).Info("Using manager context with: KUBE_CTX=manager.cloud-platform.service.justice.gov.uk")

c.cmdEnv = append(os.Environ(), "KUBE_CTX=manager.cloud-platform.service.justice.gov.uk")
Expand Down Expand Up @@ -351,7 +350,7 @@ func (c *Commander) BulkApply() error {
return err
}

if contains(ws, " live-1") {
if contains(ws, "live-1") {
log.WithFields(log.Fields{"dir": dir}).Info("Using live-1 context with: KUBE_CTX=live-1.cloud-platform.service.justice.gov.uk")

c.cmdEnv = append(os.Environ(), "KUBE_CTX=live-1.cloud-platform.service.justice.gov.uk")
Expand All @@ -361,7 +360,7 @@ func (c *Commander) BulkApply() error {
if err != nil {
return err
}
} else if contains(ws, " manager") {
} else if contains(ws, "manager") {
log.WithFields(log.Fields{"dir": dir}).Info("Using manager context with: KUBE_CTX=manager.cloud-platform.service.justice.gov.uk")

c.cmdEnv = append(os.Environ(), "KUBE_CTX=manager.cloud-platform.service.justice.gov.uk")
Expand Down

0 comments on commit 91fcf2e

Please sign in to comment.