Skip to content

Commit

Permalink
Adds: logger in case of flag parsing fails (#1115)
Browse files Browse the repository at this point in the history
* adds: logger in case of flag parsing fails

* fix: error check

Co-authored-by: Suvarna Rokade <suvarna.rokade@accurics.com>
  • Loading branch information
Rchanger and Suvarna Rokade authored Jan 11, 2022
1 parent 9f75f75 commit b0259e8
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/cli/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io/ioutil"
"log"
"os"
"strings"

"github.com/accurics/terrascan/pkg/config"
"github.com/accurics/terrascan/pkg/logging"
Expand All @@ -40,6 +41,9 @@ func Execute() {
rootCmd.PersistentFlags().StringVarP(&OutputType, "output", "o", "human", "output type (human, json, yaml, xml, junit-xml, sarif, github-sarif)")
rootCmd.PersistentFlags().StringVarP(&ConfigFile, "config-path", "c", "", "config file path")

//Added init here in case flag parsing failed we should log which flag was incorrect.
logging.Init(LogType, LogLevel)

// Function to execute before processing commands
cobra.OnInitialize(func() {
// Set up the logger
Expand Down Expand Up @@ -77,6 +81,11 @@ func Execute() {
}

if err := rootCmd.Execute(); err != nil {
// check if the error is related to flag argument missing and
// log it before terminating the process so user gets idea about the incorrect flag value
if strings.Contains(err.Error(), "flag needs an argument") {
zap.S().Error("error while executing command ", zap.Error(err))
}
os.Exit(1)
}
}

0 comments on commit b0259e8

Please sign in to comment.