Skip to content

Commit 0556e50

Browse files
committed
Add log-level flag for skopeo
Keep the debug flag for backwards compatibility, but support other log levels. Signed-off-by: Robb Manes <[email protected]>
1 parent 2b3701b commit 0556e50

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

cmd/skopeo/main.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,15 @@ import (
1919
// and will be populated by the Makefile
2020
var gitCommit = ""
2121

22+
// Supported logrus log levels
23+
var logLevels = []string{"trace", "debug", "info", "warn", "warning", "error", "fatal", "panic"}
24+
var defaultLogLevel = "warn"
25+
2226
var defaultUserAgent = "skopeo/" + version.Version
2327

2428
type globalOptions struct {
2529
debug bool // Enable debug output
30+
logLevel string // Set log level at or above debug (trace, etc)
2631
tlsVerify commonFlag.OptionalBool // Require HTTPS and verify certificates (for docker: and docker-daemon:)
2732
policyPath string // Path to a signature verification policy file
2833
insecurePolicy bool // Use an "allow everything" signature verification policy
@@ -79,6 +84,7 @@ func createApp() (*cobra.Command, *globalOptions) {
7984
var dummyVersion bool
8085
rootCommand.Flags().BoolVarP(&dummyVersion, "version", "v", false, "Version for Skopeo")
8186
rootCommand.PersistentFlags().BoolVar(&opts.debug, "debug", false, "enable debug output")
87+
rootCommand.PersistentFlags().StringVar(&opts.logLevel, "log-level", defaultLogLevel, fmt.Sprintf("Log messages above specified level (%s)", strings.Join(logLevels, ", ")))
8288
rootCommand.PersistentFlags().StringVar(&opts.policyPath, "policy", "", "Path to a trust policy file")
8389
rootCommand.PersistentFlags().BoolVar(&opts.insecurePolicy, "insecure-policy", false, "run the tool without any policy check")
8490
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) {
9399
rootCommand.PersistentFlags().StringVar(&opts.tmpDir, "tmpdir", "", "directory used to store temporary files")
94100
flag := commonFlag.OptionalBoolFlag(rootCommand.Flags(), &opts.tlsVerify, "tls-verify", "Require HTTPS and verify certificates when accessing the registry")
95101
flag.Hidden = true
102+
rootCommand.MarkFlagsMutuallyExclusive("log-level", "debug")
96103
rootCommand.AddCommand(
97104
copyCmd(&opts),
98105
deleteCmd(&opts),
@@ -114,9 +121,34 @@ func createApp() (*cobra.Command, *globalOptions) {
114121

115122
// before is run by the cli package for any command, before running the command-specific handler.
116123
func (opts *globalOptions) before(cmd *cobra.Command, args []string) error {
124+
// Set logging level based on debug or logLevel flags
125+
logLevel := opts.logLevel
117126
if opts.debug {
118-
logrus.SetLevel(logrus.DebugLevel)
127+
logLevel = "debug"
128+
}
129+
130+
var found bool
131+
for _, l := range logLevels {
132+
if l == strings.ToLower(logLevel) {
133+
found = true
134+
break
135+
}
136+
}
137+
138+
if !found {
139+
logrus.Fatal("Log Level %q is not supported, choose from: %s\n", logLevel, strings.Join(logLevels, ", "))
119140
}
141+
142+
level, err := logrus.ParseLevel(logLevel)
143+
if err != nil {
144+
logrus.Fatal(err.Error())
145+
}
146+
147+
logrus.SetLevel(level)
148+
if logrus.IsLevelEnabled(logrus.InfoLevel) {
149+
logrus.Infof("Filtering at log level %s", logrus.GetLevel())
150+
}
151+
120152
if opts.tlsVerify.Present() {
121153
logrus.Warn("'--tls-verify' is deprecated, please set this on the specific subcommand")
122154
}

0 commit comments

Comments
 (0)