mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Update the stream formatter to display custom unit numbers.
Signed-off-by: Ying Li <ying.li@docker.com>
This commit is contained in:
parent
1847bb899a
commit
a771c16834
5 changed files with 63 additions and 15 deletions
|
@ -123,7 +123,7 @@ func MergeSwarmSpecToGRPC(s types.Spec, spec swarmapi.ClusterSpec) (swarmapi.Clu
|
||||||
spec.CAConfig.SigningCACert = []byte(s.CAConfig.SigningCACert)
|
spec.CAConfig.SigningCACert = []byte(s.CAConfig.SigningCACert)
|
||||||
}
|
}
|
||||||
if s.CAConfig.SigningCAKey != "" {
|
if s.CAConfig.SigningCAKey != "" {
|
||||||
// do prpagate the signing CA key here because we want to provide it TO the swarm APIs
|
// do propagate the signing CA key here because we want to provide it TO the swarm APIs
|
||||||
spec.CAConfig.SigningCAKey = []byte(s.CAConfig.SigningCAKey)
|
spec.CAConfig.SigningCAKey = []byte(s.CAConfig.SigningCAKey)
|
||||||
}
|
}
|
||||||
spec.CAConfig.ForceRotate = s.CAConfig.ForceRotate
|
spec.CAConfig.ForceRotate = s.CAConfig.ForceRotate
|
||||||
|
|
|
@ -36,7 +36,8 @@ type JSONProgress struct {
|
||||||
Total int64 `json:"total,omitempty"`
|
Total int64 `json:"total,omitempty"`
|
||||||
Start int64 `json:"start,omitempty"`
|
Start int64 `json:"start,omitempty"`
|
||||||
// If true, don't show xB/yB
|
// If true, don't show xB/yB
|
||||||
HideCounts bool `json:"hidecounts,omitempty"`
|
HideCounts bool `json:"hidecounts,omitempty"`
|
||||||
|
Units string `json:"units,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *JSONProgress) String() string {
|
func (p *JSONProgress) String() string {
|
||||||
|
@ -55,11 +56,16 @@ func (p *JSONProgress) String() string {
|
||||||
if p.Current <= 0 && p.Total <= 0 {
|
if p.Current <= 0 && p.Total <= 0 {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
current := units.HumanSize(float64(p.Current))
|
|
||||||
if p.Total <= 0 {
|
if p.Total <= 0 {
|
||||||
return fmt.Sprintf("%8v", current)
|
switch p.Units {
|
||||||
|
case "":
|
||||||
|
current := units.HumanSize(float64(p.Current))
|
||||||
|
return fmt.Sprintf("%8v", current)
|
||||||
|
default:
|
||||||
|
return fmt.Sprintf("%d %s", p.Current, p.Units)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
total := units.HumanSize(float64(p.Total))
|
|
||||||
percentage := int(float64(p.Current)/float64(p.Total)*100) / 2
|
percentage := int(float64(p.Current)/float64(p.Total)*100) / 2
|
||||||
if percentage > 50 {
|
if percentage > 50 {
|
||||||
percentage = 50
|
percentage = 50
|
||||||
|
@ -73,13 +79,25 @@ func (p *JSONProgress) String() string {
|
||||||
pbBox = fmt.Sprintf("[%s>%s] ", strings.Repeat("=", percentage), strings.Repeat(" ", numSpaces))
|
pbBox = fmt.Sprintf("[%s>%s] ", strings.Repeat("=", percentage), strings.Repeat(" ", numSpaces))
|
||||||
}
|
}
|
||||||
|
|
||||||
if !p.HideCounts {
|
switch {
|
||||||
|
case p.HideCounts:
|
||||||
|
case p.Units == "": // no units, use bytes
|
||||||
|
current := units.HumanSize(float64(p.Current))
|
||||||
|
total := units.HumanSize(float64(p.Total))
|
||||||
|
|
||||||
numbersBox = fmt.Sprintf("%8v/%v", current, total)
|
numbersBox = fmt.Sprintf("%8v/%v", current, total)
|
||||||
|
|
||||||
if p.Current > p.Total {
|
if p.Current > p.Total {
|
||||||
// remove total display if the reported current is wonky.
|
// remove total display if the reported current is wonky.
|
||||||
numbersBox = fmt.Sprintf("%8v", current)
|
numbersBox = fmt.Sprintf("%8v", current)
|
||||||
}
|
}
|
||||||
|
default:
|
||||||
|
numbersBox = fmt.Sprintf("%d/%d %s", p.Current, p.Total, p.Units)
|
||||||
|
|
||||||
|
if p.Current > p.Total {
|
||||||
|
// remove total display if the reported current is wonky.
|
||||||
|
numbersBox = fmt.Sprintf("%d %s", p.Current, p.Units)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.Current > 0 && p.Start > 0 && percentage < 50 {
|
if p.Current > 0 && p.Start > 0 && percentage < 50 {
|
||||||
|
|
|
@ -65,22 +65,50 @@ func TestProgress(t *testing.T) {
|
||||||
if jp5.String() != expected {
|
if jp5.String() != expected {
|
||||||
t.Fatalf("Expected %q, got %q", expected, jp5.String())
|
t.Fatalf("Expected %q, got %q", expected, jp5.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
expected = "[=========================> ] 50/100 units"
|
||||||
|
if termsz != nil && termsz.Width <= 110 {
|
||||||
|
expected = " 50/100 units"
|
||||||
|
}
|
||||||
|
jp6 := JSONProgress{Current: 50, Total: 100, Units: "units"}
|
||||||
|
if jp6.String() != expected {
|
||||||
|
t.Fatalf("Expected %q, got %q", expected, jp6.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
// this number can't be negative
|
||||||
|
expected = "[==================================================>] 50 units"
|
||||||
|
if termsz != nil && termsz.Width <= 110 {
|
||||||
|
expected = " 50 units"
|
||||||
|
}
|
||||||
|
jp7 := JSONProgress{Current: 50, Total: 40, Units: "units"}
|
||||||
|
if jp7.String() != expected {
|
||||||
|
t.Fatalf("Expected %q, got %q", expected, jp7.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
expected = "[=========================> ] "
|
||||||
|
if termsz != nil && termsz.Width <= 110 {
|
||||||
|
expected = ""
|
||||||
|
}
|
||||||
|
jp8 := JSONProgress{Current: 50, Total: 100, HideCounts: true}
|
||||||
|
if jp8.String() != expected {
|
||||||
|
t.Fatalf("Expected %q, got %q", expected, jp8.String())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestJSONMessageDisplay(t *testing.T) {
|
func TestJSONMessageDisplay(t *testing.T) {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
messages := map[JSONMessage][]string{
|
messages := map[JSONMessage][]string{
|
||||||
// Empty
|
// Empty
|
||||||
JSONMessage{}: {"\n", "\n"},
|
{}: {"\n", "\n"},
|
||||||
// Status
|
// Status
|
||||||
JSONMessage{
|
{
|
||||||
Status: "status",
|
Status: "status",
|
||||||
}: {
|
}: {
|
||||||
"status\n",
|
"status\n",
|
||||||
"status\n",
|
"status\n",
|
||||||
},
|
},
|
||||||
// General
|
// General
|
||||||
JSONMessage{
|
{
|
||||||
Time: now.Unix(),
|
Time: now.Unix(),
|
||||||
ID: "ID",
|
ID: "ID",
|
||||||
From: "From",
|
From: "From",
|
||||||
|
@ -90,7 +118,7 @@ func TestJSONMessageDisplay(t *testing.T) {
|
||||||
fmt.Sprintf("%v ID: (from From) status\n", time.Unix(now.Unix(), 0).Format(jsonlog.RFC3339NanoFixed)),
|
fmt.Sprintf("%v ID: (from From) status\n", time.Unix(now.Unix(), 0).Format(jsonlog.RFC3339NanoFixed)),
|
||||||
},
|
},
|
||||||
// General, with nano precision time
|
// General, with nano precision time
|
||||||
JSONMessage{
|
{
|
||||||
TimeNano: now.UnixNano(),
|
TimeNano: now.UnixNano(),
|
||||||
ID: "ID",
|
ID: "ID",
|
||||||
From: "From",
|
From: "From",
|
||||||
|
@ -100,7 +128,7 @@ func TestJSONMessageDisplay(t *testing.T) {
|
||||||
fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(jsonlog.RFC3339NanoFixed)),
|
fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(jsonlog.RFC3339NanoFixed)),
|
||||||
},
|
},
|
||||||
// General, with both times Nano is preferred
|
// General, with both times Nano is preferred
|
||||||
JSONMessage{
|
{
|
||||||
Time: now.Unix(),
|
Time: now.Unix(),
|
||||||
TimeNano: now.UnixNano(),
|
TimeNano: now.UnixNano(),
|
||||||
ID: "ID",
|
ID: "ID",
|
||||||
|
@ -111,7 +139,7 @@ func TestJSONMessageDisplay(t *testing.T) {
|
||||||
fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(jsonlog.RFC3339NanoFixed)),
|
fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(jsonlog.RFC3339NanoFixed)),
|
||||||
},
|
},
|
||||||
// Stream over status
|
// Stream over status
|
||||||
JSONMessage{
|
{
|
||||||
Status: "status",
|
Status: "status",
|
||||||
Stream: "stream",
|
Stream: "stream",
|
||||||
}: {
|
}: {
|
||||||
|
@ -119,7 +147,7 @@ func TestJSONMessageDisplay(t *testing.T) {
|
||||||
"stream",
|
"stream",
|
||||||
},
|
},
|
||||||
// With progress message
|
// With progress message
|
||||||
JSONMessage{
|
{
|
||||||
Status: "status",
|
Status: "status",
|
||||||
ProgressMessage: "progressMessage",
|
ProgressMessage: "progressMessage",
|
||||||
}: {
|
}: {
|
||||||
|
@ -127,7 +155,7 @@ func TestJSONMessageDisplay(t *testing.T) {
|
||||||
"status progressMessage",
|
"status progressMessage",
|
||||||
},
|
},
|
||||||
// With progress, stream empty
|
// With progress, stream empty
|
||||||
JSONMessage{
|
{
|
||||||
Status: "status",
|
Status: "status",
|
||||||
Stream: "",
|
Stream: "",
|
||||||
Progress: &JSONProgress{Current: 1},
|
Progress: &JSONProgress{Current: 1},
|
||||||
|
|
|
@ -18,6 +18,8 @@ type Progress struct {
|
||||||
|
|
||||||
// If true, don't show xB/yB
|
// If true, don't show xB/yB
|
||||||
HideCounts bool
|
HideCounts bool
|
||||||
|
// If not empty, use units instead of bytes for counts
|
||||||
|
Units string
|
||||||
|
|
||||||
// Aux contains extra information not presented to the user, such as
|
// Aux contains extra information not presented to the user, such as
|
||||||
// digests for push signing.
|
// digests for push signing.
|
||||||
|
|
|
@ -117,7 +117,7 @@ func (out *progressOutput) WriteProgress(prog progress.Progress) error {
|
||||||
if prog.Message != "" {
|
if prog.Message != "" {
|
||||||
formatted = out.sf.formatStatus(prog.ID, prog.Message)
|
formatted = out.sf.formatStatus(prog.ID, prog.Message)
|
||||||
} else {
|
} else {
|
||||||
jsonProgress := jsonmessage.JSONProgress{Current: prog.Current, Total: prog.Total, HideCounts: prog.HideCounts}
|
jsonProgress := jsonmessage.JSONProgress{Current: prog.Current, Total: prog.Total, HideCounts: prog.HideCounts, Units: prog.Units}
|
||||||
formatted = out.sf.formatProgress(prog.ID, prog.Action, &jsonProgress, prog.Aux)
|
formatted = out.sf.formatProgress(prog.ID, prog.Action, &jsonProgress, prog.Aux)
|
||||||
}
|
}
|
||||||
_, err := out.out.Write(formatted)
|
_, err := out.out.Write(formatted)
|
||||||
|
|
Loading…
Reference in a new issue