Skip to content

Commit

Permalink
Converted time.Time to time.Duration in ShowHumanizeTime()
Browse files Browse the repository at this point in the history
  • Loading branch information
gunadhya committed Dec 31, 2020
1 parent 91a6f11 commit f61028e
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 47 deletions.
8 changes: 4 additions & 4 deletions integration/skaffold/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func (b *RunBuilder) runForked(t *testing.T, out io.Writer) {

go func() {
cmd.Wait()
logrus.Infoln("Ran in", util.ShowHumanizeTime(start))
logrus.Infoln("Ran in", util.ShowHumanizeTime(time.Since(start)))
}()

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/build/cache/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (c *cache) Build(ctx context.Context, out io.Writer, tags tag.ImageTags, ar
})
}

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

bRes, err := buildAndTest(ctx, out, tags, needToBuild)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/skaffold/diagnose/diagnose.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ func typeOfArtifact(a *latest.Artifact) string {
func timeToListDependencies(ctx context.Context, a *latest.Artifact, cfg docker.Config) (string, []string, error) {
start := time.Now()
paths, err := build.DependenciesForArtifact(ctx, a, cfg, nil)
return util.ShowHumanizeTime(start), paths, err
return util.ShowHumanizeTime(time.Since(start)), paths, err
}

func timeToConstructSyncMap(a *latest.Artifact, cfg docker.Config) (string, error) {
start := time.Now()
_, err := sync.SyncMap(a, cfg)
return util.ShowHumanizeTime(start), err
return util.ShowHumanizeTime(time.Since(start)), err
}

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

func sizeOfDockerContext(ctx context.Context, a *latest.Artifact, cfg docker.Config) (int64, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/runner/build_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (r *SkaffoldRunner) imageTags(ctx context.Context, out io.Writer, artifacts
color.Yellow.Fprintln(out, "Some taggers failed. Rerun with -vdebug for errors.")
}

logrus.Infoln("Tags generated in", util.ShowHumanizeTime(start))
logrus.Infoln("Tags generated in", util.ShowHumanizeTime(time.Since(start)))
return imageTags, nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/runner/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,6 @@ func (r *SkaffoldRunner) performStatusCheck(ctx context.Context, out io.Writer)
return err
}

color.Default.Fprintln(out, "Deployments stabilized in", util.ShowHumanizeTime(start))
color.Default.Fprintln(out, "Deployments stabilized in", util.ShowHumanizeTime(time.Since(start)))
return nil
}
2 changes: 1 addition & 1 deletion pkg/skaffold/runner/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func (r *SkaffoldRunner) Dev(ctx context.Context, out io.Writer, artifacts []*la
return fmt.Errorf("watching skaffold configuration %q: %w", r.runCtx.ConfigurationFile(), err)
}

logrus.Infoln("List generated in", util.ShowHumanizeTime(start))
logrus.Infoln("List generated in", util.ShowHumanizeTime(time.Since(start)))

// Init Sync State
if err := sync.Init(ctx, artifacts); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/runner/load_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (r *SkaffoldRunner) loadImages(ctx context.Context, out io.Writer, artifact
color.Green.Fprintln(out, "Loaded")
}

color.Default.Fprintln(out, "Images loaded in", util.ShowHumanizeTime(start))
color.Default.Fprintln(out, "Images loaded in", util.ShowHumanizeTime(time.Since(start)))
return nil
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/skaffold/runner/timings.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (w withTimings) Build(ctx context.Context, out io.Writer, tags tag.ImageTag
if err != nil {
return nil, err
}
logrus.Infoln("Build completed in", util.ShowHumanizeTime(start))
logrus.Infoln("Build completed in", util.ShowHumanizeTime(time.Since(start)))
return bRes, nil
}

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

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

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

Expand All @@ -108,6 +108,6 @@ func (w withTimings) Prune(ctx context.Context, out io.Writer) error {
if err != nil {
return err
}
logrus.Infoln("Image prune completed in", util.ShowHumanizeTime(start))
logrus.Infoln("Image prune completed in", util.ShowHumanizeTime(time.Since(start)))
return nil
}
8 changes: 4 additions & 4 deletions pkg/skaffold/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,13 +344,13 @@ func hasHiddenPrefix(s string) bool {
}

// ShowHumanizeTime returns time in human readable format
func ShowHumanizeTime(start time.Time) string {
shortTime := time.Since(start).Truncate(time.Millisecond)
func ShowHumanizeTime(start time.Duration) string {
shortTime := start.Truncate(time.Millisecond)
longTime := shortTime.String()
out := time.Time{}.Add(shortTime)

if time.Since(start).Seconds() < 1 {
return time.Since(start).String()
if start.Seconds() < 1 {
return start.String()
}

longTime = strings.ReplaceAll(longTime, "h", " hour ")
Expand Down
44 changes: 18 additions & 26 deletions pkg/skaffold/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,42 +471,34 @@ func stringPointer(s string) *string {
}

func TestShowHumanizeTime(t *testing.T) {
currTime := time.Now()
duration1, _ := time.ParseDuration("1h58m30.918273645s")
duration2, _ := time.ParseDuration("5.23494327s")
duration1, err := time.ParseDuration("1h58m30.918273645s")
if err != nil {
t.Errorf("%s", err)
}
duration2, err := time.ParseDuration("5.23494327s")
if err != nil {
t.Errorf("%s", err)
}
tests := []struct {
description string
value time.Time
expected string
expectedUp string
expectedDown string
description string
value time.Duration
expected string
}{
{
description: "Case for 1h58m30.918273645s",
value: currTime.Add(-duration1),
expected: "1 hour 58 minutes 30.918 seconds",
expectedUp: "1 hour 58 minutes 30.919 seconds",
expectedDown: "1 hour 58 minutes 30.917 seconds",
description: "Case for 1h58m30.918273645s",
value: duration1,
expected: "1 hour 58 minutes 30.918 seconds",
},
{
description: "Case for 5.23494327s",
value: currTime.Add(-duration2),
expected: "5.235 seconds",
expectedUp: "5.236 seconds",
expectedDown: "5.234 seconds",
description: "Case for 5.23494327s",
value: duration2,
expected: "5.234 seconds",
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
humanizedValue := ShowHumanizeTime(test.value)
switch humanizedValue {
case test.expectedUp:
t.CheckDeepEqual(test.expectedUp, humanizedValue)
case test.expectedDown:
t.CheckDeepEqual(test.expectedDown, humanizedValue)
default:
t.CheckDeepEqual(test.expected, humanizedValue)
}
t.CheckDeepEqual(test.expected, humanizedValue)
})
}
}

0 comments on commit f61028e

Please sign in to comment.