2021-08-02 01:10:41 +00:00
|
|
|
package formatter
|
2021-07-17 08:27:34 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-07-18 02:24:48 +00:00
|
|
|
"os"
|
2021-07-17 08:27:34 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
|
|
|
"github.com/docker/go-units"
|
2021-07-18 02:24:48 +00:00
|
|
|
"github.com/olekukonko/tablewriter"
|
2021-07-17 08:27:34 +00:00
|
|
|
)
|
|
|
|
|
2021-08-02 01:10:41 +00:00
|
|
|
func ShortenID(str string) string {
|
2021-07-17 08:27:34 +00:00
|
|
|
return str[:12]
|
|
|
|
}
|
|
|
|
|
2021-08-02 01:10:41 +00:00
|
|
|
func Truncate(str string) string {
|
2021-07-17 08:27:34 +00:00
|
|
|
return fmt.Sprintf(`"%s"`, formatter.Ellipsis(str, 19))
|
|
|
|
}
|
|
|
|
|
2021-08-02 01:10:41 +00:00
|
|
|
// RemoveSha remove image sha from a string that are added in some docker outputs
|
|
|
|
func RemoveSha(str string) string {
|
2021-07-17 08:27:34 +00:00
|
|
|
return strings.Split(str, "@")[0]
|
|
|
|
}
|
|
|
|
|
2021-08-13 13:13:56 +00:00
|
|
|
// HumanDuration from docker/cli RunningFor() to be accessible outside of the class
|
2021-08-02 01:10:41 +00:00
|
|
|
func HumanDuration(timestamp int64) string {
|
2021-07-17 08:27:34 +00:00
|
|
|
date := time.Unix(timestamp, 0)
|
|
|
|
now := time.Now().UTC()
|
|
|
|
return units.HumanDuration(now.Sub(date)) + " ago"
|
|
|
|
}
|
2021-07-18 02:24:48 +00:00
|
|
|
|
2021-08-02 01:10:41 +00:00
|
|
|
func CreateTable(columns []string) *tablewriter.Table {
|
2021-07-18 02:24:48 +00:00
|
|
|
table := tablewriter.NewWriter(os.Stdout)
|
|
|
|
table.SetHeader(columns)
|
|
|
|
return table
|
|
|
|
}
|