-
Notifications
You must be signed in to change notification settings - Fork 815
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
Add log-level flag for skopeo #2514
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,10 +19,15 @@ import ( | |
// and will be populated by the Makefile | ||
var gitCommit = "" | ||
|
||
// Supported logrus log levels | ||
var logLevels = []string{"trace", "debug", "info", "warn", "warning", "error", "fatal", "panic"} | ||
var defaultLogLevel = "warn" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The default of |
||
|
||
var defaultUserAgent = "skopeo/" + version.Version | ||
|
||
type globalOptions struct { | ||
debug bool // Enable debug output | ||
logLevel string // Set log level at or above debug (trace, etc) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comment is incorrect – |
||
tlsVerify commonFlag.OptionalBool // Require HTTPS and verify certificates (for docker: and docker-daemon:) | ||
policyPath string // Path to a signature verification policy file | ||
insecurePolicy bool // Use an "allow everything" signature verification policy | ||
|
@@ -79,6 +84,7 @@ func createApp() (*cobra.Command, *globalOptions) { | |
var dummyVersion bool | ||
rootCommand.Flags().BoolVarP(&dummyVersion, "version", "v", false, "Version for Skopeo") | ||
rootCommand.PersistentFlags().BoolVar(&opts.debug, "debug", false, "enable debug output") | ||
rootCommand.PersistentFlags().StringVar(&opts.logLevel, "log-level", defaultLogLevel, fmt.Sprintf("Log messages above specified level (%s)", strings.Join(logLevels, ", "))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Contrary to Podman’s practice, I think it makes much more sense to not set default option values. With option values defaulted at the CLI level, the code later has difficulty telling apart actual user input from the hard-coded defaults (or from values that come from config files). |
||
rootCommand.PersistentFlags().StringVar(&opts.policyPath, "policy", "", "Path to a trust policy file") | ||
rootCommand.PersistentFlags().BoolVar(&opts.insecurePolicy, "insecure-policy", false, "run the tool without any policy check") | ||
rootCommand.PersistentFlags().StringVar(&opts.registriesDirPath, "registries.d", "", "use registry configuration files in `DIR` (e.g. for container signature storage)") | ||
|
@@ -93,6 +99,7 @@ func createApp() (*cobra.Command, *globalOptions) { | |
rootCommand.PersistentFlags().StringVar(&opts.tmpDir, "tmpdir", "", "directory used to store temporary files") | ||
flag := commonFlag.OptionalBoolFlag(rootCommand.Flags(), &opts.tlsVerify, "tls-verify", "Require HTTPS and verify certificates when accessing the registry") | ||
flag.Hidden = true | ||
rootCommand.MarkFlagsMutuallyExclusive("log-level", "debug") | ||
rootCommand.AddCommand( | ||
copyCmd(&opts), | ||
deleteCmd(&opts), | ||
|
@@ -114,9 +121,34 @@ func createApp() (*cobra.Command, *globalOptions) { | |
|
||
// before is run by the cli package for any command, before running the command-specific handler. | ||
func (opts *globalOptions) before(cmd *cobra.Command, args []string) error { | ||
// Set logging level based on debug or logLevel flags | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using both One way to do that would be to use |
||
logLevel := opts.logLevel | ||
if opts.debug { | ||
logrus.SetLevel(logrus.DebugLevel) | ||
logLevel = "debug" | ||
} | ||
|
||
var found bool | ||
for _, l := range logLevels { | ||
if l == strings.ToLower(logLevel) { | ||
found = true | ||
break | ||
} | ||
} | ||
|
||
if !found { | ||
logrus.Fatal("Log Level %s is not supported, choose from: %s\n", logLevel, strings.Join(logLevels, ", ")) | ||
} | ||
|
||
Comment on lines
+130
to
+141
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is unnecessary, |
||
level, err := logrus.ParseLevel(logLevel) | ||
if err != nil { | ||
logrus.Fatal(err.Error()) | ||
} | ||
|
||
logrus.SetLevel(level) | ||
if logrus.IsLevelEnabled(logrus.InfoLevel) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
logrus.Infof("Filtering at log level %s", logrus.GetLevel()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’d prefer not starting every command output with such a line. (“info” is the default level).
With a strict enough parsing, the users should be confident that the output exactly matches their expectations. We don’t have |
||
} | ||
|
||
if opts.tlsVerify.Present() { | ||
logrus.Warn("'--tls-verify' is deprecated, please set this on the specific subcommand") | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be built from
logrus.AllLevels
, not hard-coded in Skopeo.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
… second thought, all of
logrus
is deprecated. Nowadays the standard-library logging defines fewer levels: https://pkg.go.dev/log/slog#Level .We probably do want “trace”; but maybe we can avoid committing to
fatal
/panic
for now?