Skip to content

Commit

Permalink
show age instead of creation time to shrink output width
Browse files Browse the repository at this point in the history
  • Loading branch information
cppforlife committed Aug 11, 2018
1 parent a5216d0 commit e64c7db
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 12 deletions.
74 changes: 74 additions & 0 deletions pkg/knctl/cmd/age_value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
Copyright 2018 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
"fmt"
"time"

uitable "github.com/cppforlife/go-cli-ui/ui/table"
)

type ValueAge struct {
T time.Time
}

var _ uitable.Value = ValueAge{}

func NewValueAge(t time.Time) ValueAge { return ValueAge{T: t} }

func (t ValueAge) String() string {
if t.T.IsZero() {
return ""
}
return t.fmtDuration(time.Now().Sub(t.T))
}

func (t ValueAge) Value() uitable.Value { return t }

func (t ValueAge) Compare(other uitable.Value) int {
otherT := other.(ValueAge).T
switch {
case t.T.Equal(otherT):
return 0
case t.T.Before(otherT):
return -1
default:
return 1
}
}

func (ValueAge) fmtDuration(d time.Duration) string {
d = d.Round(time.Minute)

days := d / (24 * time.Hour)
d -= days * 24 * time.Hour

hrs := d / time.Hour
d -= hrs * time.Hour

mins := d / time.Minute

switch {
case days > 0:
return fmt.Sprintf("%dd", days)
case hrs > 0:
return fmt.Sprintf("%dh", hrs)
default:
return fmt.Sprintf("%dm", mins)
}
}
4 changes: 2 additions & 2 deletions pkg/knctl/cmd/list_builds.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (o *ListBuildsOptions) Run() error {
uitable.NewHeader("Name"),
uitable.NewHeader("Builder"),
uitable.NewHeader("Succeeded"),
uitable.NewHeader("Created At"),
uitable.NewHeader("Age"),
},

SortBy: []uitable.ColumnSort{
Expand All @@ -86,7 +86,7 @@ func (o *ListBuildsOptions) Run() error {
uitable.NewValueString(build.Name),
uitable.NewValueString(string(build.Status.Builder)),
o.succeededValue(build),
uitable.NewValueTime(build.CreationTimestamp.Time),
NewValueAge(build.CreationTimestamp.Time),
})
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/knctl/cmd/list_ingresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (o *ListIngressesOptions) Run() error {
uitable.NewHeader("Name"),
uitable.NewHeader("Addresses"),
uitable.NewHeader("Ports"),
uitable.NewHeader("Created At"),
uitable.NewHeader("Age"),
},

SortBy: []uitable.ColumnSort{
Expand Down Expand Up @@ -93,7 +93,7 @@ func (o *ListIngressesOptions) Run() error {
uitable.NewValueString(svc.Name),
uitable.NewValueStrings(addrs),
uitable.NewValueStrings(ports),
uitable.NewValueTime(svc.CreationTimestamp.Time),
NewValueAge(svc.CreationTimestamp.Time),
})
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/knctl/cmd/list_pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (o *ListPodsOptions) Run() error {
uitable.NewHeader("Name"),
uitable.NewHeader("Phase"),
uitable.NewHeader("Restarts"),
uitable.NewHeader("Created At"),
uitable.NewHeader("Age"),
},

SortBy: []uitable.ColumnSort{
Expand All @@ -82,7 +82,7 @@ func (o *ListPodsOptions) Run() error {
Error: !o.isOKStatus(pod),
},
uitable.NewValueInt(o.podRestarts(pod)),
uitable.NewValueTime(pod.CreationTimestamp.Time),
NewValueAge(pod.CreationTimestamp.Time),
})
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/knctl/cmd/list_revisions.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (o *ListRevisionsOptions) Run() error {
uitable.NewHeader("Allocated Traffic %"),
uitable.NewHeader("Serving State"),
uitable.NewHeader("Annotations"),
uitable.NewHeader("Created At"),
uitable.NewHeader("Age"),
},

SortBy: []uitable.ColumnSort{
Expand All @@ -103,7 +103,7 @@ func (o *ListRevisionsOptions) Run() error {
NewAllocatedTrafficPercentValue(service, rev),
uitable.NewValueString(string(rev.Spec.ServingState)),
NewAnnotationsValue(rev.Annotations),
uitable.NewValueTime(rev.CreationTimestamp.Time),
NewValueAge(rev.CreationTimestamp.Time),
})
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/knctl/cmd/list_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (o *ListRoutesOptions) Run() error {
uitable.NewHeader("Ready"),
uitable.NewHeader("Domain"),
uitable.NewHeader("Internal Domain"),
uitable.NewHeader("Created At"),
uitable.NewHeader("Age"),
},

SortBy: []uitable.ColumnSort{
Expand All @@ -92,7 +92,7 @@ func (o *ListRoutesOptions) Run() error {
o.readyValue(route),
uitable.NewValueString(route.Status.Domain),
uitable.NewValueString(route.Status.DomainInternal),
uitable.NewValueTime(route.CreationTimestamp.Time),
NewValueAge(route.CreationTimestamp.Time),
})
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/knctl/cmd/list_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (o *ListServicesOptions) Run() error {
uitable.NewHeader("Domain"),
uitable.NewHeader("Internal Domain"),
uitable.NewHeader("Annotations"),
uitable.NewHeader("Created At"),
uitable.NewHeader("Age"),
},

SortBy: []uitable.ColumnSort{
Expand All @@ -86,7 +86,7 @@ func (o *ListServicesOptions) Run() error {
uitable.NewValueString(svc.Status.Domain),
uitable.NewValueString(svc.Status.DomainInternal),
NewAnnotationsValue(svc.Annotations),
uitable.NewValueTime(svc.CreationTimestamp.Time),
NewValueAge(svc.CreationTimestamp.Time),
})
}

Expand Down

0 comments on commit e64c7db

Please sign in to comment.