Skip to content

Commit

Permalink
Remove unnecessary logging package and set log level in root (#156)
Browse files Browse the repository at this point in the history
Signed-off-by: Jeff McCoy <code@jeffm.us>
  • Loading branch information
YrrepNoj authored and jeff-mccoy committed Nov 5, 2021
1 parent 6e328b4 commit 6ba1f35
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 29 deletions.
28 changes: 25 additions & 3 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"os"
"strings"

"github.com/defenseunicorns/zarf/cli/internal/log"
"github.com/defenseunicorns/zarf/cli/internal/packager"
"github.com/sirupsen/logrus"

"github.com/spf13/cobra"
)
Expand All @@ -16,7 +16,10 @@ var zarfLogLevel = ""
var rootCmd = &cobra.Command{
Use: "zarf COMMAND|ZARF-PACKAGE|ZARF-YAML",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
log.SetLogLevel(zarfLogLevel)
setLogLevel(zarfLogLevel)
if logrus.GetLevel() != logrus.InfoLevel {
fmt.Printf("The log level has been changed to: %s\n", logrus.GetLevel())
}
},
Short: "Small tool to bundle dependencies with K3s for airgapped deployments",
Args: cobra.MaximumNArgs(1),
Expand All @@ -43,5 +46,24 @@ func Execute() {

func init() {
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
rootCmd.PersistentFlags().StringVarP(&zarfLogLevel, "log-level", "l", "info", "Log level when runnning Zarf. Valid options are: debug, info, warn, error, fatal")
rootCmd.PersistentFlags().StringVarP(&zarfLogLevel, "log-level", "l", "info", "Log level when running Zarf. Valid options are: debug, info, warn, error, fatal")
}

func setLogLevel(logLevel string) {
switch logLevel {
case "debug":
logrus.SetLevel(logrus.DebugLevel)
case "info":
logrus.SetLevel(logrus.InfoLevel)
case "warn":
logrus.SetLevel(logrus.WarnLevel)
case "error":
logrus.SetLevel(logrus.ErrorLevel)
case "fatal":
logrus.SetLevel(logrus.FatalLevel)
case "panic":
logrus.SetLevel(logrus.PanicLevel)
default:
logrus.Fatal("Unrecognized log level entry: %s", logLevel)
}
}
26 changes: 0 additions & 26 deletions cli/internal/log/logger.go

This file was deleted.

7 changes: 7 additions & 0 deletions test/e2e/e2e_general_cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package test

import (
"fmt"
"strings"
"testing"

"github.com/gruntwork-io/terratest/modules/aws"
Expand Down Expand Up @@ -102,4 +103,10 @@ func testGeneralCliStuff(t *testing.T, terraformOptions *terraform.Options, keyP
// Test that `zarf package deploy` gives an error if deploying a remote package without the --insecure or --shasum flags
output, err = ssh.CheckSshCommandE(t, publicHost, fmt.Sprintf("sudo bash -c 'cd /home/%s/build && ./zarf package deploy https://zarf-examples.s3.amazonaws.com/zarf-package-appliance-demo-doom.tar.zst --confirm'", username))
require.Error(t, err, output)

// Test that changing the log level actually applies the requested level
output, err = ssh.CheckSshCommandE(t, publicHost, fmt.Sprintf("cd /home/%s/build && ./zarf version --log-level warn 2> /dev/null", username))
expectedOutString := "The log level has been changed to: warning"
logLevelOutput := strings.Split(output, "\n")[0]
require.Equal(t, expectedOutString, logLevelOutput, "The log level should be changed to 'warn'")
}

0 comments on commit 6ba1f35

Please sign in to comment.