From cf0720d732282bcfa28fee490ef14c04baf9c342 Mon Sep 17 00:00:00 2001 From: Suraj Deshmukh Date: Wed, 22 Apr 2020 17:51:58 +0530 Subject: [PATCH 1/2] cmd: Specify logs file location When user is running `lokoctl cluster apply/destroy` without verbose flag `-v` then show user what is the location of log files. Signed-off-by: Suraj Deshmukh --- pkg/terraform/executor.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/terraform/executor.go b/pkg/terraform/executor.go index f6900f0c6..777b90225 100644 --- a/pkg/terraform/executor.go +++ b/pkg/terraform/executor.go @@ -211,6 +211,8 @@ func (ex *Executor) execute(verbose bool, args ...string) error { wg.Add(1) go tailFile(p, done, &wg) + } else { + fmt.Printf("\nYou can find the logs in %q\n", p) } wg.Wait() From 3fbacfa8dd80fd41605a2a5f2632d191a0534312 Mon Sep 17 00:00:00 2001 From: Suraj Deshmukh Date: Wed, 22 Apr 2020 17:49:49 +0530 Subject: [PATCH 2/2] cmd: Show logs on terraform error If a user is running `lokoctl cluster apply/destroy` without verbose flag `-v` and if there is an error in terraform execution then this will show last twenty lines from the logs. This will help user in knowing what went wrong. Signed-off-by: Suraj Deshmukh --- pkg/terraform/executor.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/pkg/terraform/executor.go b/pkg/terraform/executor.go index 777b90225..3b1b651a9 100644 --- a/pkg/terraform/executor.go +++ b/pkg/terraform/executor.go @@ -44,6 +44,8 @@ const ( failFileSuffix = ".fail" requiredVersion = ">= 0.12, < 0.13" + + noOfLinesOnError = 20 ) // ErrBinaryNotFound denotes the fact that the Terraform binary could not be @@ -219,16 +221,47 @@ func (ex *Executor) execute(verbose bool, args ...string) error { s, err := ex.Status(pid) if err != nil { + if !verbose { + showError(p, noOfLinesOnError) + } return fmt.Errorf("failed checking execution status: %w", err) } if s != ExecutionStatusSuccess { + if !verbose { + showError(p, noOfLinesOnError) + } return fmt.Errorf("executing Terraform failed, check %s for details", p) } return nil } +func showError(path string, noOfLines int) { + // nolint: gosec + data, err := ioutil.ReadFile(path) + if err != nil { + fmt.Printf("error reading file: %v", err) + return + } + + text := string(data) + lines := strings.Split(text, "\n") + + // Deletion by one is done here to adjust the difference between the user provided number which + // starts counting from 1 and array indices which start counting from 0. + // nolint: gomnd + offset := len(lines) - noOfLines - 1 + + if offset > 0 { + lines = lines[offset:] + } + + for _, line := range lines { + fmt.Println(line) + } +} + // LoadVars is a convenience function to load the tfvars file into memory // as a JSON object. func (ex *Executor) LoadVars() (map[string]interface{}, error) {