Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary logging package and set log level in root #156

Merged
merged 2 commits into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'")
}