package formatter import ( "fmt" "os" "strings" "time" "github.com/docker/go-units" // "github.com/olekukonko/tablewriter" "coopcloud.tech/abra/pkg/jsontable" "github.com/schollz/progressbar/v3" "github.com/sirupsen/logrus" ) func ShortenID(str string) string { return str[:12] } func SmallSHA(hash string) string { return hash[:8] } // RemoveSha remove image sha from a string that are added in some docker outputs func RemoveSha(str string) string { return strings.Split(str, "@")[0] } // HumanDuration from docker/cli RunningFor() to be accessible outside of the class func HumanDuration(timestamp int64) string { date := time.Unix(timestamp, 0) now := time.Now().UTC() return units.HumanDuration(now.Sub(date)) + " ago" } // CreateTable prepares a table layout for output. func CreateTable(columns []string) *jsontable.JSONTable { table := jsontable.NewJSONTable(os.Stdout) table.SetAutoWrapText(false) table.SetHeader(columns) return table } // CreateProgressbar generates a progress bar func CreateProgressbar(length int, title string) *progressbar.ProgressBar { return progressbar.NewOptions( length, progressbar.OptionClearOnFinish(), progressbar.OptionSetPredictTime(false), progressbar.OptionShowCount(), progressbar.OptionFullWidth(), progressbar.OptionSetDescription(title), ) } // StripTagMeta strips front-matter image tag data that we don't need for parsing. func StripTagMeta(image string) string { originalImage := image if strings.Contains(image, "docker.io") { image = strings.Split(image, "/")[1] } if strings.Contains(image, "library") { image = strings.Split(image, "/")[1] } if originalImage != image { logrus.Debugf("stripped %s to %s for parsing", originalImage, image) } return image } // ByteCountSI presents a human friendly representation of a byte count. See // https://yourbasic.org/golang/formatting-byte-size-to-human-readable-format. func ByteCountSI(b uint64) string { const unit = 1000 if b < unit { return fmt.Sprintf("%d B", b) } div, exp := uint64(unit), 0 for n := b / unit; n >= unit; n /= unit { div *= unit exp++ } return fmt.Sprintf("%.1f %cB", float64(b)/float64(div), "kMGTPE"[exp]) }