Skip to content
Merged
Prev Previous commit
Next Next commit
more tests, some cli tweaks
  • Loading branch information
johnstcn committed Jun 16, 2022
commit f817751f0cfc9fc6f00e08766f6fa1109f98e916
6 changes: 3 additions & 3 deletions cli/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func list() *cobra.Command {
}

tableWriter := cliui.Table()
header := table.Row{"workspace", "template", "status", "last built", "outdated", "autostart", "ttl"}
header := table.Row{"workspace", "template", "status", "last built", "outdated", "starts at", "stops after"}
Comment on lines -53 to +52
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clearer language

tableWriter.AppendHeader(header)
tableWriter.SortBy([]table.SortBy{{
Name: "workspace",
Expand Down Expand Up @@ -89,7 +89,7 @@ func list() *cobra.Command {
autostartDisplay := "-"
if !ptr.NilOrEmpty(workspace.AutostartSchedule) {
if sched, err := schedule.Weekly(*workspace.AutostartSchedule); err == nil {
autostartDisplay = sched.Cron()
autostartDisplay = fmt.Sprintf("%s %s (%s)", sched.Time(), sched.DaysOfWeek(), sched.Location())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consistent schedule format

}
}

Expand All @@ -98,7 +98,7 @@ func list() *cobra.Command {
dur := time.Duration(*workspace.TTLMillis) * time.Millisecond
autostopDisplay = durationDisplay(dur)
if has, ext := hasExtension(workspace); has {
autostopDisplay += fmt.Sprintf(" (+%s)", durationDisplay(ext))
autostopDisplay += fmt.Sprintf(" (%s%s)", sign(ext), durationDisplay(ext))
}
}

Expand Down
12 changes: 6 additions & 6 deletions cli/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,13 +281,13 @@ func displaySchedule(workspace codersdk.Workspace, out io.Writer) error {
}

if !workspace.LatestBuild.Deadline.IsZero() {
schedNextStop = workspace.LatestBuild.Deadline.In(loc).Format(timeFormat + " on " + dateFormat)
if found, dur := hasExtension(workspace); found {
sign := "+"
if dur < 0 {
sign = "" // negative durations get prefixed with - already
if workspace.LatestBuild.Transition != "start" {
schedNextStop = "-"
} else {
schedNextStop = workspace.LatestBuild.Deadline.In(loc).Format(timeFormat + " on " + dateFormat)
if found, dur := hasExtension(workspace); found {
schedNextStop += fmt.Sprintf(" (%s%s from schedule)", sign(dur), durationDisplay(dur))
}
schedNextStop += fmt.Sprintf(" (%s%s)", sign, durationDisplay(dur))
}
}

Expand Down
36 changes: 30 additions & 6 deletions cli/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"
"time"

"golang.org/x/exp/constraints"
"golang.org/x/xerrors"

"github.com/coder/coder/coderd/autobuild/schedule"
Expand All @@ -20,12 +21,27 @@ var errUnsupportedTimezone = xerrors.New("The location you provided looks like a

// durationDisplay formats a duration for easier display:
// * Durations of 24 hours or greater are displays as Xd
// * Durations less than 1 minute are displayed as <1m
// * Duration is truncated to the nearest minute
// * Empty minutes and seconds are truncated
// * The returned string is the absolute value. Use sign()
// if you need to indicate if the duration is positive or
// negative.
func durationDisplay(d time.Duration) string {
duration := d
sign := ""
if duration == 0 {
return "0s"
}
if duration < 0 {
duration *= -1
}
// duration > 0 now
if duration < time.Minute {
return "<1m"
return sign + "<1m"
}
if duration > 24*time.Hour {
duration = duration.Truncate(time.Hour)
}
if duration > time.Minute {
duration = duration.Truncate(time.Minute)
Expand All @@ -39,16 +55,24 @@ func durationDisplay(d time.Duration) string {
if days > 0 {
durationDisplay = fmt.Sprintf("%dd%s", days, durationDisplay)
}
if strings.HasSuffix(durationDisplay, "m0s") {
durationDisplay = durationDisplay[:len(durationDisplay)-2]
for _, suffix := range []string{"m0s", "h0m", "d0s"} {
if strings.HasSuffix(durationDisplay, suffix) {
durationDisplay = durationDisplay[:len(durationDisplay)-2]
}
}
if strings.HasSuffix(durationDisplay, "h0m") {
durationDisplay = durationDisplay[:len(durationDisplay)-2]
return sign + durationDisplay
}

// Sign returns the sign of n. 0 is considered positive.
func sign[T constraints.Integer | constraints.Float | time.Duration](n T) string {
if n < 0 {
return "-"
}
return durationDisplay
return "+"
}

// hasExtension returns the deadline extension of ws, if it is present.
// This is calculated from the time the provisioner job was completed.
// Note that the extension may be negative.
func hasExtension(ws codersdk.Workspace) (bool, time.Duration) {
if ws.LatestBuild.Transition != codersdk.WorkspaceTransitionStart {
Expand Down