From b0259e8b0650a2a4b9a6a6f3b8ca31745cfa9515 Mon Sep 17 00:00:00 2001 From: Suvarna Rokade Date: Tue, 11 Jan 2022 20:24:42 +0530 Subject: [PATCH] Adds: logger in case of flag parsing fails (#1115) * adds: logger in case of flag parsing fails * fix: error check Co-authored-by: Suvarna Rokade --- pkg/cli/register.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pkg/cli/register.go b/pkg/cli/register.go index e2eab5268..b3e2f6b0e 100644 --- a/pkg/cli/register.go +++ b/pkg/cli/register.go @@ -21,6 +21,7 @@ import ( "io/ioutil" "log" "os" + "strings" "github.com/accurics/terrascan/pkg/config" "github.com/accurics/terrascan/pkg/logging" @@ -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 @@ -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) } }