Skip to content

Commit f61028e

Browse files
committed
Converted time.Time to time.Duration in ShowHumanizeTime()
1 parent 91a6f11 commit f61028e

10 files changed

Lines changed: 39 additions & 47 deletions

File tree

integration/skaffold/helper.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func (b *RunBuilder) runForked(t *testing.T, out io.Writer) {
214214

215215
go func() {
216216
cmd.Wait()
217-
logrus.Infoln("Ran in", util.ShowHumanizeTime(start))
217+
logrus.Infoln("Ran in", util.ShowHumanizeTime(time.Since(start)))
218218
}()
219219

220220
t.Cleanup(func() {
@@ -240,7 +240,7 @@ func (b *RunBuilder) Run(t *testing.T) error {
240240
if err := cmd.Run(); err != nil {
241241
return fmt.Errorf("skaffold %q: %w", b.command, err)
242242
}
243-
logrus.Infoln("Ran in", util.ShowHumanizeTime(start))
243+
logrus.Infoln("Ran in", util.ShowHumanizeTime(time.Since(start)))
244244
return nil
245245
}
246246

@@ -257,7 +257,7 @@ func (b *RunBuilder) RunWithCombinedOutput(t *testing.T) ([]byte, error) {
257257
if err != nil {
258258
return out, fmt.Errorf("skaffold %q: %w", b.command, err)
259259
}
260-
logrus.Infoln("Ran in", util.ShowHumanizeTime(start))
260+
logrus.Infoln("Ran in", util.ShowHumanizeTime(time.Since(start)))
261261
return out, nil
262262
}
263263

@@ -279,7 +279,7 @@ func (b *RunBuilder) RunOrFailOutput(t *testing.T) []byte {
279279
}
280280
t.Fatalf("skaffold %s: %v, %s", b.command, err, out)
281281
}
282-
logrus.Infoln("Ran in", util.ShowHumanizeTime(start))
282+
logrus.Infoln("Ran in", util.ShowHumanizeTime(time.Since(start)))
283283
return out
284284
}
285285

pkg/skaffold/build/cache/retrieve.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func (c *cache) Build(ctx context.Context, out io.Writer, tags tag.ImageTags, ar
120120
})
121121
}
122122

123-
logrus.Infoln("Cache check completed in", util.ShowHumanizeTime(start))
123+
logrus.Infoln("Cache check completed in", util.ShowHumanizeTime(time.Since(start)))
124124

125125
bRes, err := buildAndTest(ctx, out, tags, needToBuild)
126126
if err != nil {

pkg/skaffold/diagnose/diagnose.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,13 @@ func typeOfArtifact(a *latest.Artifact) string {
116116
func timeToListDependencies(ctx context.Context, a *latest.Artifact, cfg docker.Config) (string, []string, error) {
117117
start := time.Now()
118118
paths, err := build.DependenciesForArtifact(ctx, a, cfg, nil)
119-
return util.ShowHumanizeTime(start), paths, err
119+
return util.ShowHumanizeTime(time.Since(start)), paths, err
120120
}
121121

122122
func timeToConstructSyncMap(a *latest.Artifact, cfg docker.Config) (string, error) {
123123
start := time.Now()
124124
_, err := sync.SyncMap(a, cfg)
125-
return util.ShowHumanizeTime(start), err
125+
return util.ShowHumanizeTime(time.Since(start)), err
126126
}
127127

128128
func timeToComputeMTimes(deps []string) (string, error) {
@@ -131,7 +131,7 @@ func timeToComputeMTimes(deps []string) (string, error) {
131131
if _, err := filemon.Stat(func() ([]string, error) { return deps, nil }); err != nil {
132132
return "nil", fmt.Errorf("computing modTimes: %w", err)
133133
}
134-
return util.ShowHumanizeTime(start), nil
134+
return util.ShowHumanizeTime(time.Since(start)), nil
135135
}
136136

137137
func sizeOfDockerContext(ctx context.Context, a *latest.Artifact, cfg docker.Config) (int64, error) {

pkg/skaffold/runner/build_deploy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ func (r *SkaffoldRunner) imageTags(ctx context.Context, out io.Writer, artifacts
213213
color.Yellow.Fprintln(out, "Some taggers failed. Rerun with -vdebug for errors.")
214214
}
215215

216-
logrus.Infoln("Tags generated in", util.ShowHumanizeTime(start))
216+
logrus.Infoln("Tags generated in", util.ShowHumanizeTime(time.Since(start)))
217217
return imageTags, nil
218218
}
219219

pkg/skaffold/runner/deploy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,6 @@ func (r *SkaffoldRunner) performStatusCheck(ctx context.Context, out io.Writer)
168168
return err
169169
}
170170

171-
color.Default.Fprintln(out, "Deployments stabilized in", util.ShowHumanizeTime(start))
171+
color.Default.Fprintln(out, "Deployments stabilized in", util.ShowHumanizeTime(time.Since(start)))
172172
return nil
173173
}

pkg/skaffold/runner/dev.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func (r *SkaffoldRunner) Dev(ctx context.Context, out io.Writer, artifacts []*la
206206
return fmt.Errorf("watching skaffold configuration %q: %w", r.runCtx.ConfigurationFile(), err)
207207
}
208208

209-
logrus.Infoln("List generated in", util.ShowHumanizeTime(start))
209+
logrus.Infoln("List generated in", util.ShowHumanizeTime(time.Since(start)))
210210

211211
// Init Sync State
212212
if err := sync.Init(ctx, artifacts); err != nil {

pkg/skaffold/runner/load_images.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func (r *SkaffoldRunner) loadImages(ctx context.Context, out io.Writer, artifact
8686
color.Green.Fprintln(out, "Loaded")
8787
}
8888

89-
color.Default.Fprintln(out, "Images loaded in", util.ShowHumanizeTime(start))
89+
color.Default.Fprintln(out, "Images loaded in", util.ShowHumanizeTime(time.Since(start)))
9090
return nil
9191
}
9292

pkg/skaffold/runner/timings.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (w withTimings) Build(ctx context.Context, out io.Writer, tags tag.ImageTag
6161
if err != nil {
6262
return nil, err
6363
}
64-
logrus.Infoln("Build completed in", util.ShowHumanizeTime(start))
64+
logrus.Infoln("Build completed in", util.ShowHumanizeTime(time.Since(start)))
6565
return bRes, nil
6666
}
6767

@@ -72,7 +72,7 @@ func (w withTimings) Test(ctx context.Context, out io.Writer, builds []build.Art
7272
if err != nil {
7373
return err
7474
}
75-
logrus.Infoln("Test completed in", util.ShowHumanizeTime(start))
75+
logrus.Infoln("Test completed in", util.ShowHumanizeTime(time.Since(start)))
7676
return nil
7777
}
7878

@@ -84,7 +84,7 @@ func (w withTimings) Deploy(ctx context.Context, out io.Writer, builds []build.A
8484
if err != nil {
8585
return nil, err
8686
}
87-
logrus.Infoln("Deploy completed in", util.ShowHumanizeTime(start))
87+
logrus.Infoln("Deploy completed in", util.ShowHumanizeTime(time.Since(start)))
8888
return ns, err
8989
}
9090

@@ -96,7 +96,7 @@ func (w withTimings) Cleanup(ctx context.Context, out io.Writer) error {
9696
if err != nil {
9797
return err
9898
}
99-
logrus.Infoln("Cleanup completed in", util.ShowHumanizeTime(start))
99+
logrus.Infoln("Cleanup completed in", util.ShowHumanizeTime(time.Since(start)))
100100
return nil
101101
}
102102

@@ -108,6 +108,6 @@ func (w withTimings) Prune(ctx context.Context, out io.Writer) error {
108108
if err != nil {
109109
return err
110110
}
111-
logrus.Infoln("Image prune completed in", util.ShowHumanizeTime(start))
111+
logrus.Infoln("Image prune completed in", util.ShowHumanizeTime(time.Since(start)))
112112
return nil
113113
}

pkg/skaffold/util/util.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,13 +344,13 @@ func hasHiddenPrefix(s string) bool {
344344
}
345345

346346
// ShowHumanizeTime returns time in human readable format
347-
func ShowHumanizeTime(start time.Time) string {
348-
shortTime := time.Since(start).Truncate(time.Millisecond)
347+
func ShowHumanizeTime(start time.Duration) string {
348+
shortTime := start.Truncate(time.Millisecond)
349349
longTime := shortTime.String()
350350
out := time.Time{}.Add(shortTime)
351351

352-
if time.Since(start).Seconds() < 1 {
353-
return time.Since(start).String()
352+
if start.Seconds() < 1 {
353+
return start.String()
354354
}
355355

356356
longTime = strings.ReplaceAll(longTime, "h", " hour ")

pkg/skaffold/util/util_test.go

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -471,42 +471,34 @@ func stringPointer(s string) *string {
471471
}
472472

473473
func TestShowHumanizeTime(t *testing.T) {
474-
currTime := time.Now()
475-
duration1, _ := time.ParseDuration("1h58m30.918273645s")
476-
duration2, _ := time.ParseDuration("5.23494327s")
474+
duration1, err := time.ParseDuration("1h58m30.918273645s")
475+
if err != nil {
476+
t.Errorf("%s", err)
477+
}
478+
duration2, err := time.ParseDuration("5.23494327s")
479+
if err != nil {
480+
t.Errorf("%s", err)
481+
}
477482
tests := []struct {
478-
description string
479-
value time.Time
480-
expected string
481-
expectedUp string
482-
expectedDown string
483+
description string
484+
value time.Duration
485+
expected string
483486
}{
484487
{
485-
description: "Case for 1h58m30.918273645s",
486-
value: currTime.Add(-duration1),
487-
expected: "1 hour 58 minutes 30.918 seconds",
488-
expectedUp: "1 hour 58 minutes 30.919 seconds",
489-
expectedDown: "1 hour 58 minutes 30.917 seconds",
488+
description: "Case for 1h58m30.918273645s",
489+
value: duration1,
490+
expected: "1 hour 58 minutes 30.918 seconds",
490491
},
491492
{
492-
description: "Case for 5.23494327s",
493-
value: currTime.Add(-duration2),
494-
expected: "5.235 seconds",
495-
expectedUp: "5.236 seconds",
496-
expectedDown: "5.234 seconds",
493+
description: "Case for 5.23494327s",
494+
value: duration2,
495+
expected: "5.234 seconds",
497496
},
498497
}
499498
for _, test := range tests {
500499
testutil.Run(t, test.description, func(t *testutil.T) {
501500
humanizedValue := ShowHumanizeTime(test.value)
502-
switch humanizedValue {
503-
case test.expectedUp:
504-
t.CheckDeepEqual(test.expectedUp, humanizedValue)
505-
case test.expectedDown:
506-
t.CheckDeepEqual(test.expectedDown, humanizedValue)
507-
default:
508-
t.CheckDeepEqual(test.expected, humanizedValue)
509-
}
501+
t.CheckDeepEqual(test.expected, humanizedValue)
510502
})
511503
}
512504
}

0 commit comments

Comments
 (0)