-
Notifications
You must be signed in to change notification settings - Fork 11
feat: add default output logs to create #63
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
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 |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ import ( | |
| var ( | ||
| createVerbose bool | ||
| createDebug bool | ||
| createQuiet bool | ||
| createParallelism int | ||
| createSignatures []string | ||
| ) | ||
|
|
@@ -37,6 +38,12 @@ func init() { | |
| createCmd.Flags().BoolVar(&createDebug, "debug", false, "show detailed progress information") | ||
| createCmd.Flags().IntVarP(&createParallelism, "jobs", "j", 0, "max parallel tar-diff workers (default: number of CPUs)") | ||
| createCmd.Flags().StringArrayVar(&createSignatures, "signature", nil, "signature OCI artifact to embed (can be specified multiple times)") | ||
| createCmd.Flags().BoolVarP(&createQuiet, "quiet", "q", false, "silences all default output") | ||
| } | ||
|
|
||
| // bytesToMB formats a byte count as a human-readable megabyte value. Used in default output for simpler feedback. | ||
| func bytesToMB(b int64) string { | ||
|
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 am just curious - is there a plan/ticket/issue to add tests for
Contributor
Author
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 can add tests for |
||
| return fmt.Sprintf("%.2f MB", float64(b)/(1024*1024)) | ||
|
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.
|
||
| } | ||
|
|
||
| func runCreate(cmd *cobra.Command, args []string) error { | ||
|
|
@@ -46,7 +53,7 @@ func runCreate(cmd *cobra.Command, args []string) error { | |
| } | ||
| defer os.RemoveAll(tmpDir) | ||
|
|
||
| log := &cmdLogger{debug: createDebug} | ||
| log := &cmdLogger{debug: createDebug, quiet: createQuiet} | ||
|
|
||
| log.Debug("Opening old image: %s", args[0]) | ||
|
|
||
|
|
@@ -66,6 +73,10 @@ func runCreate(cmd *cobra.Command, args []string) error { | |
|
|
||
| sigReaders := ocidelta.ExtractedSignatures(newReader) | ||
|
|
||
| if len(createSignatures) > 0 { | ||
| log.Default("Embedding %d signature(s)", len(createSignatures)) | ||
| } | ||
|
|
||
| for _, sigPath := range createSignatures { | ||
| log.Debug("Opening signature: %s", sigPath) | ||
|
|
||
|
|
@@ -96,6 +107,14 @@ func runCreate(cmd *cobra.Command, args []string) error { | |
| return fmt.Errorf("failed to write output: %w", err) | ||
| } | ||
|
|
||
| if !createVerbose && stats.ProcessedLayerBytes > 0 { | ||
|
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 new default output block dereferences |
||
| saved := stats.ProcessedLayerBytes - stats.TarDiffLayerBytes - stats.OriginalLayerBytes | ||
| log.Default("\nDelta complete: %d layer(s) diffed, %d reused, bytes saved %.2f%% (%s → %s)", stats.ProcessedLayers, stats.SkippedLayers, | ||
| float64(saved)/float64(stats.ProcessedLayerBytes)*100, bytesToMB(stats.ProcessedLayerBytes), bytesToMB(stats.ProcessedLayerBytes-saved)) | ||
| } else if !createVerbose { | ||
| log.Default("\nDelta complete: %d layer(s) diffed, %d reused", stats.ProcessedLayers, stats.SkippedLayers) | ||
| } | ||
|
|
||
| if createVerbose && stats != nil { | ||
|
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.
Contributor
Author
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 didn't have the quiet flag affect the verbose flag's output purposefully so that the user could obtain the stats if they really wanted to without any progress information, such as if they're piping the output to a file. It's a niche use case, but I would rather include that functionality since I don't see any other reason why a user would attempt to use If you still think it would be good change that, I can implement it. I just wanted to explain that reasoning before I made that change |
||
| fmt.Printf("\nDelta creation statistics:\n") | ||
| fmt.Printf(" Old image layers: %d\n", stats.OldLayers) | ||
|
|
@@ -109,9 +128,11 @@ func runCreate(cmd *cobra.Command, args []string) error { | |
| if stats.ProcessedLayerBytes > 0 { | ||
| saved := stats.ProcessedLayerBytes - stats.TarDiffLayerBytes - stats.OriginalLayerBytes | ||
| pct := float64(saved) / float64(stats.ProcessedLayerBytes) * 100 | ||
| fmt.Printf(" Bytes saved: %d (%.1f%%)\n", saved, pct) | ||
| fmt.Printf(" Bytes saved: %d (%.1f%%)\n\n", saved, pct) | ||
| } | ||
| } | ||
|
|
||
| log.Default("Delta written to %s", args[2]) | ||
|
|
||
| return nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,11 +40,20 @@ func Root() *cobra.Command { | |
| type Logger interface { | ||
| Debug(format string, args ...interface{}) | ||
| Warning(format string, args ...interface{}) | ||
| Default(format string, args ...interface{}) | ||
| } | ||
|
|
||
| // cmdLogger implements the Logger interface | ||
| type cmdLogger struct { | ||
| debug bool | ||
| quiet bool | ||
| } | ||
|
|
||
| func (l *cmdLogger) Default(format string, args ...interface{}) { | ||
| // Debug is a much more detailed version of the default logs, no need for both to exist at the same time. | ||
|
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
Contributor
Author
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. It was a simple comment I thought to add to the default logs initially, but you're right there is no debug equivalent. I will add a line to the |
||
| if !l.quiet && !l.debug { | ||
| fmt.Printf(format+"\n", args...) | ||
| } | ||
| } | ||
|
|
||
| func (l *cmdLogger) Debug(format string, args ...interface{}) { | ||
|
|
||
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.
I would write
"suppress all default output"to match the imperative style of the other flags.