forked from toolshed/abra
refactor: formatter gets own package
This commit is contained in:
@ -8,7 +8,7 @@ import (
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"coopcloud.tech/abra/cli/formatter"
|
||||
"coopcloud.tech/abra/pkg/formatter"
|
||||
"coopcloud.tech/abra/pkg/upstream/convert"
|
||||
loader "coopcloud.tech/abra/pkg/upstream/stack"
|
||||
stack "coopcloud.tech/abra/pkg/upstream/stack"
|
||||
|
@ -5,7 +5,7 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
abraFormatter "coopcloud.tech/abra/cli/formatter"
|
||||
"coopcloud.tech/abra/pkg/formatter"
|
||||
"github.com/AlecAivazis/survey/v2"
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
@ -33,7 +33,7 @@ func GetContainer(c context.Context, cl *client.Client, filters filters.Args, pr
|
||||
for _, container := range containers {
|
||||
containerName := strings.Join(container.Names, " ")
|
||||
trimmed := strings.TrimPrefix(containerName, "/")
|
||||
created := abraFormatter.HumanDuration(container.Created)
|
||||
created := formatter.HumanDuration(container.Created)
|
||||
containersRaw = append(containersRaw, fmt.Sprintf("%s (created %v)", trimmed, created))
|
||||
}
|
||||
|
||||
|
57
pkg/formatter/formatter.go
Normal file
57
pkg/formatter/formatter.go
Normal file
@ -0,0 +1,57 @@
|
||||
package formatter
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/docker/cli/cli/command/formatter"
|
||||
"github.com/docker/go-units"
|
||||
"github.com/olekukonko/tablewriter"
|
||||
"github.com/schollz/progressbar/v3"
|
||||
)
|
||||
|
||||
func ShortenID(str string) string {
|
||||
return str[:12]
|
||||
}
|
||||
|
||||
func Truncate(str string) string {
|
||||
return fmt.Sprintf(`"%s"`, formatter.Ellipsis(str, 19))
|
||||
}
|
||||
|
||||
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) *tablewriter.Table {
|
||||
table := tablewriter.NewWriter(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),
|
||||
)
|
||||
}
|
@ -10,10 +10,10 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"coopcloud.tech/abra/cli/formatter"
|
||||
"coopcloud.tech/abra/pkg/client"
|
||||
"coopcloud.tech/abra/pkg/compose"
|
||||
"coopcloud.tech/abra/pkg/config"
|
||||
"coopcloud.tech/abra/pkg/formatter"
|
||||
gitPkg "coopcloud.tech/abra/pkg/git"
|
||||
"coopcloud.tech/abra/pkg/upstream/stack"
|
||||
loader "coopcloud.tech/abra/pkg/upstream/stack"
|
||||
|
Reference in New Issue
Block a user