diff --git a/cmd/notary/integration_test.go b/cmd/notary/integration_test.go index eec3b1f52c..1a79eaf65e 100644 --- a/cmd/notary/integration_test.go +++ b/cmd/notary/integration_test.go @@ -822,6 +822,28 @@ func TestDefaultRootKeyGeneration(t *testing.T) { assertNumKeys(t, tempDir, 1, 0, true) } +// Tests the interaction with the verbose and log-level flags +func TestLogLevelFlags(t *testing.T) { + // Test default to fatal + setVerbosityLevel() + assert.Equal(t, "fatal", logrus.GetLevel().String()) + + // Test that verbose (-v) sets to error + verbose = true + setVerbosityLevel() + assert.Equal(t, "error", logrus.GetLevel().String()) + + // Test that debug (-D) sets to debug + debug = true + setVerbosityLevel() + assert.Equal(t, "debug", logrus.GetLevel().String()) + + // Test that unsetting verboseError still uses verboseDebug + verbose = false + setVerbosityLevel() + assert.Equal(t, "debug", logrus.GetLevel().String()) +} + func tempDirWithConfig(t *testing.T, config string) string { tempDir, err := ioutil.TempDir("/tmp", "repo") assert.NoError(t, err) diff --git a/cmd/notary/main.go b/cmd/notary/main.go index 1065204ee9..29f41eaacd 100644 --- a/cmd/notary/main.go +++ b/cmd/notary/main.go @@ -20,6 +20,7 @@ const ( ) var ( + debug bool verbose bool trustDir string configFile string @@ -37,10 +38,7 @@ func init() { } func parseConfig() *viper.Viper { - if verbose { - logrus.SetLevel(logrus.DebugLevel) - logrus.SetOutput(os.Stderr) - } + setVerbosityLevel() // Get home directory for current user homeDir, err := homedir.Dir() @@ -112,6 +110,7 @@ func setupCommand(notaryCmd *cobra.Command) { notaryCmd.PersistentFlags().StringVarP(&trustDir, "trustDir", "d", "", "Directory where the trust data is persisted to") notaryCmd.PersistentFlags().StringVarP(&configFile, "configFile", "c", "", "Path to the configuration file to use") notaryCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Verbose output") + notaryCmd.PersistentFlags().BoolVarP(&debug, "debug", "D", false, "Debug output") notaryCmd.PersistentFlags().StringVarP(&remoteTrustServer, "server", "s", "", "Remote trust server location") cmdKeyGenerator := &keyCommander{ @@ -180,3 +179,15 @@ func getPassphraseRetriever() passphrase.Retriever { return baseRetriever(keyName, alias, createNew, numAttempts) } } + +// Set the logging level to fatal on default, or the most specific level the user specified (debug or error) +func setVerbosityLevel() { + if debug { + logrus.SetLevel(logrus.DebugLevel) + } else if verbose { + logrus.SetLevel(logrus.ErrorLevel) + } else { + logrus.SetLevel(logrus.FatalLevel) + } + logrus.SetOutput(os.Stderr) +}