-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from deexithparand/issue-3-image-size-threshold
Completed Feature: Image Size Threshold (Issue : #3)
- Loading branch information
Showing
5 changed files
with
161 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package docker | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// Helper function to truncate docker image ID with ellipsis | ||
func FormatDockerImageID(s string, maxLen int) string { | ||
if len(s) <= maxLen { | ||
return s | ||
} | ||
return s[:maxLen-3] + "..." | ||
} | ||
|
||
// Helper function to converts bytes to human-readable format | ||
func FormatSize(bytes int64) string { | ||
const unit = 1024.0 | ||
if bytes < unit { | ||
return fmt.Sprintf("%d B", bytes) | ||
} | ||
div, exp := int64(unit), 0 | ||
for n := bytes / unit; n >= unit; n /= unit { | ||
div *= unit | ||
exp++ | ||
} | ||
return fmt.Sprintf("%.1f %cB", float64(bytes)/float64(div), "KMGTPE"[exp]) | ||
} | ||
|
||
// Helper function used to format the docker image labels Key Value Pairs | ||
func FormatLabels(labels map[string]string) string { | ||
if len(labels) == 0 { | ||
return "No Labels Found" | ||
} | ||
|
||
var labelStore []string | ||
for labelKey, labelValue := range labels { | ||
// Handling empty values | ||
if labelValue == "" { | ||
labelStore = append(labelStore, labelKey) | ||
continue | ||
} | ||
labelStore = append(labelStore, fmt.Sprintf("%s:%s", labelKey, labelValue)) | ||
} | ||
|
||
return strings.Join(labelStore, ", ") | ||
} | ||
|
||
// converts value to bytes | ||
func ToBytes(size float64, unit string) float64 { | ||
multipliers := map[string]float64{ | ||
"B": 1.0, | ||
"KB": 1024.0, | ||
"MB": 1024.0 * 1024.0, | ||
"GB": 1024.0 * 1024.0 * 1024.0, | ||
} | ||
|
||
return size * multipliers[unit] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,50 @@ | ||
package utils | ||
|
||
import "flag" | ||
import ( | ||
"flag" | ||
) | ||
|
||
type Size struct { | ||
Value float64 | ||
Unit string | ||
} | ||
|
||
type Flags struct { | ||
DryRun bool | ||
RemoveStopped bool | ||
VerboseMode bool | ||
SizeLimit Size | ||
B bool | ||
KB bool | ||
MB bool | ||
GB bool | ||
} | ||
|
||
func (f *Flags) GetSizeUnit() string { | ||
if f.B { | ||
return "B" | ||
} else if f.KB { | ||
return "KB" | ||
} else if f.MB { | ||
return "MB" | ||
} else if f.GB { | ||
return "GB" | ||
} else { | ||
return "" | ||
} | ||
} | ||
|
||
func ParseFlags() *Flags { | ||
f := &Flags{} | ||
flag.BoolVar(&f.DryRun, "dry-run", false, "List unused Docker images without deleting them") | ||
flag.BoolVar(&f.RemoveStopped, "remove-stopped", false, "Remove Images Associated with Stopped Containers") | ||
flag.BoolVar(&f.VerboseMode, "verbose", false, "Verbose mode provides additional details about each image during cleanup") | ||
flag.Float64Var(&f.SizeLimit.Value, "size-limit", 0, "Specify the size limit to filter images (e.g., 500MB, 1GB)") | ||
flag.BoolVar(&f.B, "B", false, "Specify the size unit as bytes") | ||
flag.BoolVar(&f.KB, "KB", false, "Specify the size unit as kilobytes") | ||
flag.BoolVar(&f.MB, "MB", false, "Specify the size unit as megabytes") | ||
flag.BoolVar(&f.GB, "GB", false, "Specify the size unit as gigabytes") | ||
|
||
flag.Parse() | ||
return f | ||
} |