From 00ebedab58f5828ac1dc91da7551d0a17eed9e40 Mon Sep 17 00:00:00 2001 From: gunadhya <6939749+gunadhya@users.noreply.github.com> Date: Sun, 29 Nov 2020 21:24:11 +0530 Subject: [PATCH 01/11] Fix for humanize time in #5005 --- pkg/skaffold/runner/deploy.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/pkg/skaffold/runner/deploy.go b/pkg/skaffold/runner/deploy.go index e02cf9c9d9a..f0081cde8ee 100644 --- a/pkg/skaffold/runner/deploy.go +++ b/pkg/skaffold/runner/deploy.go @@ -19,6 +19,7 @@ package runner import ( "context" "fmt" + "github.com/dustin/go-humanize" "io" "time" @@ -156,7 +157,14 @@ func (r *SkaffoldRunner) performStatusCheck(ctx context.Context, out io.Writer) if err := s.Check(ctx, out); err != nil { return err } + //Create human readable time string + prettyTime := humanize.Time(start) - color.Default.Fprintln(out, "Deployments stabilized in", time.Since(start)) + //Case for when it takes less than a second + if time.Since(start).Seconds() < 1 { + prettyTime = time.Since(start).String() + " ago" + } + + color.Default.Fprintln(out, "Deployments stabilized ", prettyTime) return nil } From 0de553f3e4f0364adba4ca38f485d0145db5ff04 Mon Sep 17 00:00:00 2001 From: gunadhya <6939749+gunadhya@users.noreply.github.com> Date: Mon, 30 Nov 2020 16:04:12 +0530 Subject: [PATCH 02/11] Fixing lint errors --- pkg/skaffold/runner/deploy.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/skaffold/runner/deploy.go b/pkg/skaffold/runner/deploy.go index f0081cde8ee..7d6a42ec05e 100644 --- a/pkg/skaffold/runner/deploy.go +++ b/pkg/skaffold/runner/deploy.go @@ -19,10 +19,11 @@ package runner import ( "context" "fmt" - "github.com/dustin/go-humanize" "io" "time" + "github.com/dustin/go-humanize" + "github.com/sirupsen/logrus" "k8s.io/client-go/tools/clientcmd/api" @@ -157,6 +158,7 @@ func (r *SkaffoldRunner) performStatusCheck(ctx context.Context, out io.Writer) if err := s.Check(ctx, out); err != nil { return err } + //Create human readable time string prettyTime := humanize.Time(start) From a2703c5088366e8948d2551037e537d35a304981 Mon Sep 17 00:00:00 2001 From: gunadhya <6939749+gunadhya@users.noreply.github.com> Date: Tue, 1 Dec 2020 01:40:08 +0530 Subject: [PATCH 03/11] Fix lint errors using gci --- pkg/skaffold/runner/deploy.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/skaffold/runner/deploy.go b/pkg/skaffold/runner/deploy.go index 7d6a42ec05e..d0d1dc497aa 100644 --- a/pkg/skaffold/runner/deploy.go +++ b/pkg/skaffold/runner/deploy.go @@ -23,7 +23,6 @@ import ( "time" "github.com/dustin/go-humanize" - "github.com/sirupsen/logrus" "k8s.io/client-go/tools/clientcmd/api" From b9bd6c3b991fbc2f68ed06c6e9afc3ccaa917777 Mon Sep 17 00:00:00 2001 From: gunadhya <6939749+gunadhya@users.noreply.github.com> Date: Mon, 7 Dec 2020 12:47:44 +0530 Subject: [PATCH 04/11] Added gohumanize to replace time.Since --- integration/skaffold/helper.go | 34 +++++++++++++++++++--- pkg/skaffold/build/cache/retrieve.go | 9 +++++- pkg/skaffold/diagnose/diagnose.go | 42 +++++++++++++++++++++------- pkg/skaffold/runner/build_deploy.go | 9 +++++- pkg/skaffold/runner/deploy.go | 6 ++-- pkg/skaffold/runner/dev.go | 9 +++++- pkg/skaffold/runner/load_images.go | 9 +++++- pkg/skaffold/runner/timings.go | 41 +++++++++++++++++++++++---- pkg/skaffold/runner/timings_test.go | 10 +++---- 9 files changed, 138 insertions(+), 31 deletions(-) diff --git a/integration/skaffold/helper.go b/integration/skaffold/helper.go index e829a7f22cc..a849e2ad9a2 100644 --- a/integration/skaffold/helper.go +++ b/integration/skaffold/helper.go @@ -27,6 +27,7 @@ import ( "testing" "time" + "github.com/dustin/go-humanize" "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -186,7 +187,14 @@ func (b *RunBuilder) RunBackground(t *testing.T) io.ReadCloser { go func() { cmd.Wait() - logrus.Infoln("Ran in", time.Since(start)) + //Create human readable time string + showsTime := humanize.Time(start) + + //Case for when it takes less than a second + if time.Since(start).Seconds() < 1 { + showsTime = time.Since(start).String() + " ago" + } + logrus.Infoln("Ran", showsTime) }() t.Cleanup(func() { @@ -215,8 +223,14 @@ func (b *RunBuilder) Run(t *testing.T) error { if err := cmd.Run(); err != nil { return fmt.Errorf("skaffold %q: %w", b.command, err) } + //Create human readable time string + showsTime := humanize.Time(start) - logrus.Infoln("Ran in", time.Since(start)) + //Case for when it takes less than a second + if time.Since(start).Seconds() < 1 { + showsTime = time.Since(start).String() + " ago" + } + logrus.Infoln("Ran", showsTime) return nil } @@ -233,8 +247,14 @@ func (b *RunBuilder) RunWithCombinedOutput(t *testing.T) ([]byte, error) { if err != nil { return out, fmt.Errorf("skaffold %q: %w", b.command, err) } + //Create human readable time string + showsTime := humanize.Time(start) - logrus.Infoln("Ran in", time.Since(start)) + //Case for when it takes less than a second + if time.Since(start).Seconds() < 1 { + showsTime = time.Since(start).String() + " ago" + } + logrus.Infoln("Ran", showsTime) return out, nil } @@ -256,8 +276,14 @@ func (b *RunBuilder) RunOrFailOutput(t *testing.T) []byte { } t.Fatalf("skaffold %s: %v, %s", b.command, err, out) } + //Create human readable time string + showsTime := humanize.Time(start) - logrus.Infoln("Ran in", time.Since(start)) + //Case for when it takes less than a second + if time.Since(start).Seconds() < 1 { + showsTime = time.Since(start).String() + " ago" + } + logrus.Infoln("Ran", showsTime) return out } diff --git a/pkg/skaffold/build/cache/retrieve.go b/pkg/skaffold/build/cache/retrieve.go index 5db4975e47e..5cfdf4b9761 100644 --- a/pkg/skaffold/build/cache/retrieve.go +++ b/pkg/skaffold/build/cache/retrieve.go @@ -22,6 +22,7 @@ import ( "io" "time" + "github.com/dustin/go-humanize" "github.com/sirupsen/logrus" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/build" @@ -110,8 +111,14 @@ func (c *cache) Build(ctx context.Context, out io.Writer, tags tag.ImageTags, ar Tag: uniqueTag, }) } + //Create human readable time string + showsTime := humanize.Time(start) - logrus.Infoln("Cache check complete in", time.Since(start)) + //Case for when it takes less than a second + if time.Since(start).Seconds() < 1 { + showsTime = time.Since(start).String() + " ago" + } + logrus.Infoln("Cache check completed", showsTime) bRes, err := buildAndTest(ctx, out, tags, needToBuild) if err != nil { diff --git a/pkg/skaffold/diagnose/diagnose.go b/pkg/skaffold/diagnose/diagnose.go index 9a909ebe255..5920ed25117 100644 --- a/pkg/skaffold/diagnose/diagnose.go +++ b/pkg/skaffold/diagnose/diagnose.go @@ -23,6 +23,8 @@ import ( "io/ioutil" "time" + "github.com/dustin/go-humanize" + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/build" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/color" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker" @@ -60,7 +62,7 @@ func CheckArtifacts(ctx context.Context, cfg Config, out io.Writer) error { } fmt.Fprintln(out, " - Dependencies:", len(deps), "files") - fmt.Fprintf(out, " - Time to list dependencies: %v (2nd time: %v)\n", timeDeps1, timeDeps2) + fmt.Fprintf(out, " - Time to list dependencies: %s (2nd time: %s)\n", timeDeps1, timeDeps2) timeSyncMap1, err := timeToConstructSyncMap(artifact, cfg) if err != nil { @@ -74,7 +76,7 @@ func CheckArtifacts(ctx context.Context, cfg Config, out io.Writer) error { return fmt.Errorf("construct artifact dependencies: %w", err) } } else { - fmt.Fprintf(out, " - Time to construct sync-map: %v (2nd time: %v)\n", timeSyncMap1, timeSyncMap2) + fmt.Fprintf(out, " - Time to construct sync-map: %s (2nd time: %s)\n", timeSyncMap1, timeSyncMap2) } timeMTimes1, err := timeToComputeMTimes(deps) @@ -86,7 +88,7 @@ func CheckArtifacts(ctx context.Context, cfg Config, out io.Writer) error { return fmt.Errorf("computing modTimes: %w", err) } - fmt.Fprintf(out, " - Time to compute mTimes on dependencies: %v (2nd time: %v)\n", timeMTimes1, timeMTimes2) + fmt.Fprintf(out, " - Time to compute mTimes on dependencies: %s (2nd time: %s)\n", timeMTimes1, timeMTimes2) } return nil @@ -111,26 +113,46 @@ func typeOfArtifact(a *latest.Artifact) string { } } -func timeToListDependencies(ctx context.Context, a *latest.Artifact, cfg docker.Config) (time.Duration, []string, error) { +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 time.Since(start), paths, err + //Create human readable time string + showsTime := humanize.Time(start) + + //Case for when it takes less than a second + if time.Since(start).Seconds() < 1 { + showsTime = time.Since(start).String() + " ago" + } + return showsTime, paths, err } -func timeToConstructSyncMap(a *latest.Artifact, cfg docker.Config) (time.Duration, error) { +func timeToConstructSyncMap(a *latest.Artifact, cfg docker.Config) (string, error) { start := time.Now() _, err := sync.SyncMap(a, cfg) - return time.Since(start), err + //Create human readable time string + showsTime := humanize.Time(start) + + //Case for when it takes less than a second + if time.Since(start).Seconds() < 1 { + showsTime = time.Since(start).String() + " ago" + } + return showsTime, err } -func timeToComputeMTimes(deps []string) (time.Duration, error) { +func timeToComputeMTimes(deps []string) (string, error) { start := time.Now() if _, err := filemon.Stat(func() ([]string, error) { return deps, nil }); err != nil { - return 0, fmt.Errorf("computing modTimes: %w", err) + return "nil", fmt.Errorf("computing modTimes: %w", err) } + //Create human readable time string + showsTime := humanize.Time(start) - return time.Since(start), nil + //Case for when it takes less than a second + if time.Since(start).Seconds() < 1 { + showsTime = time.Since(start).String() + " ago" + } + return showsTime, nil } func sizeOfDockerContext(ctx context.Context, a *latest.Artifact, cfg docker.Config) (int64, error) { diff --git a/pkg/skaffold/runner/build_deploy.go b/pkg/skaffold/runner/build_deploy.go index d2a39fcb9e3..8a436007fce 100644 --- a/pkg/skaffold/runner/build_deploy.go +++ b/pkg/skaffold/runner/build_deploy.go @@ -23,6 +23,7 @@ import ( "os" "time" + "github.com/dustin/go-humanize" "github.com/sirupsen/logrus" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/build" @@ -202,8 +203,14 @@ func (r *SkaffoldRunner) imageTags(ctx context.Context, out io.Writer, artifacts if showWarning { color.Yellow.Fprintln(out, "Some taggers failed. Rerun with -vdebug for errors.") } + //Create human readable time string + showsTime := humanize.Time(start) - logrus.Infoln("Tags generated in", time.Since(start)) + //Case for when it takes less than a second + if time.Since(start).Seconds() < 1 { + showsTime = time.Since(start).String() + " ago" + } + logrus.Infoln("Tags generated", showsTime) return imageTags, nil } diff --git a/pkg/skaffold/runner/deploy.go b/pkg/skaffold/runner/deploy.go index d0d1dc497aa..5ee2a71d617 100644 --- a/pkg/skaffold/runner/deploy.go +++ b/pkg/skaffold/runner/deploy.go @@ -159,13 +159,13 @@ func (r *SkaffoldRunner) performStatusCheck(ctx context.Context, out io.Writer) } //Create human readable time string - prettyTime := humanize.Time(start) + showsTime := humanize.Time(start) //Case for when it takes less than a second if time.Since(start).Seconds() < 1 { - prettyTime = time.Since(start).String() + " ago" + showsTime = time.Since(start).String() + " ago" } - color.Default.Fprintln(out, "Deployments stabilized ", prettyTime) + color.Default.Fprintln(out, "Deployments stabilized ", showsTime) return nil } diff --git a/pkg/skaffold/runner/dev.go b/pkg/skaffold/runner/dev.go index 97fbe125e87..c6a97b8db9d 100644 --- a/pkg/skaffold/runner/dev.go +++ b/pkg/skaffold/runner/dev.go @@ -23,6 +23,7 @@ import ( "io" "time" + "github.com/dustin/go-humanize" "github.com/sirupsen/logrus" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/build" @@ -194,8 +195,14 @@ func (r *SkaffoldRunner) Dev(ctx context.Context, out io.Writer, artifacts []*la event.DevLoopFailedWithErrorCode(r.devIteration, proto.StatusCode_DEVINIT_REGISTER_CONFIG_DEP, err) return fmt.Errorf("watching skaffold configuration %q: %w", r.runCtx.ConfigurationFile(), err) } + //Create human readable time string + showsTime := humanize.Time(start) - logrus.Infoln("List generated in", time.Since(start)) + //Case for when it takes less than a second + if time.Since(start).Seconds() < 1 { + showsTime = time.Since(start).String() + " ago" + } + logrus.Infoln("List generated ", showsTime) // Init Sync State if err := sync.Init(ctx, artifacts); err != nil { diff --git a/pkg/skaffold/runner/load_images.go b/pkg/skaffold/runner/load_images.go index 6d23f6d56eb..e43e16c7781 100644 --- a/pkg/skaffold/runner/load_images.go +++ b/pkg/skaffold/runner/load_images.go @@ -25,6 +25,7 @@ import ( "time" "github.com/docker/distribution/reference" + "github.com/dustin/go-humanize" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/build" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/color" @@ -85,8 +86,14 @@ func (r *SkaffoldRunner) loadImages(ctx context.Context, out io.Writer, artifact color.Green.Fprintln(out, "Loaded") } + //Create human readable time string + showsTime := humanize.Time(start) - color.Default.Fprintln(out, "Images loaded in", time.Since(start)) + //Case for when it takes less than a second + if time.Since(start).Seconds() < 1 { + showsTime = time.Since(start).String() + " ago" + } + color.Default.Fprintln(out, "Images loaded", showsTime) return nil } diff --git a/pkg/skaffold/runner/timings.go b/pkg/skaffold/runner/timings.go index 8377699fd4c..91ae454aa8c 100644 --- a/pkg/skaffold/runner/timings.go +++ b/pkg/skaffold/runner/timings.go @@ -21,6 +21,7 @@ import ( "io" "time" + "github.com/dustin/go-humanize" "github.com/sirupsen/logrus" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/build" @@ -60,8 +61,14 @@ func (w withTimings) Build(ctx context.Context, out io.Writer, tags tag.ImageTag if err != nil { return nil, err } + //Create human readable time string + showsTime := humanize.Time(start) - logrus.Infoln("Build complete in", time.Since(start)) + //Case for when it takes less than a second + if time.Since(start).Seconds() < 1 { + showsTime = time.Since(start).String() + " ago" + } + logrus.Infoln("Build completed", showsTime) return bRes, nil } @@ -72,8 +79,14 @@ func (w withTimings) Test(ctx context.Context, out io.Writer, builds []build.Art if err != nil { return err } + //Create human readable time string + showsTime := humanize.Time(start) - logrus.Infoln("Test complete in", time.Since(start)) + //Case for when it takes less than a second + if time.Since(start).Seconds() < 1 { + showsTime = time.Since(start).String() + " ago" + } + logrus.Infoln("Test completed", showsTime) return nil } @@ -85,8 +98,14 @@ func (w withTimings) Deploy(ctx context.Context, out io.Writer, builds []build.A if err != nil { return nil, err } + //Create human readable time string + showsTime := humanize.Time(start) - logrus.Infoln("Deploy complete in", time.Since(start)) + //Case for when it takes less than a second + if time.Since(start).Seconds() < 1 { + showsTime = time.Since(start).String() + " ago" + } + logrus.Infoln("Deploy completed", showsTime) return ns, err } @@ -98,8 +117,14 @@ func (w withTimings) Cleanup(ctx context.Context, out io.Writer) error { if err != nil { return err } + //Create human readable time string + showsTime := humanize.Time(start) - logrus.Infoln("Cleanup complete in", time.Since(start)) + //Case for when it takes less than a second + if time.Since(start).Seconds() < 1 { + showsTime = time.Since(start).String() + " ago" + } + logrus.Infoln("Cleanup completed", showsTime) return nil } @@ -111,7 +136,13 @@ func (w withTimings) Prune(ctx context.Context, out io.Writer) error { if err != nil { return err } + //Create human readable time string + showsTime := humanize.Time(start) - logrus.Infoln("Image prune complete in", time.Since(start)) + //Case for when it takes less than a second + if time.Since(start).Seconds() < 1 { + showsTime = time.Since(start).String() + } + logrus.Infoln("Image prune completed", showsTime) return nil } diff --git a/pkg/skaffold/runner/timings_test.go b/pkg/skaffold/runner/timings_test.go index 1e109935fa5..b0936730071 100644 --- a/pkg/skaffold/runner/timings_test.go +++ b/pkg/skaffold/runner/timings_test.go @@ -94,7 +94,7 @@ func TestTimingsBuild(t *testing.T) { { description: "build success", shouldOutput: "", - shouldLog: "Build complete in .+$", + shouldLog: "Build completed .+$", shouldErr: false, }, { @@ -130,7 +130,7 @@ func TestTimingsPrune(t *testing.T) { { description: "test success", shouldOutput: "(?m)^Pruning images...\n", - shouldLog: "Image prune complete in .+$", + shouldLog: "Image prune completed .+$", shouldErr: false, }, { @@ -166,7 +166,7 @@ func TestTimingsTest(t *testing.T) { { description: "test success", shouldOutput: "", - shouldLog: "Test complete in .+$", + shouldLog: "Test completed .+$", shouldErr: false, }, { @@ -202,7 +202,7 @@ func TestTimingsDeploy(t *testing.T) { { description: "prune success", shouldOutput: "(?m)^Starting deploy...\n", - shouldLog: "Deploy complete in .+$", + shouldLog: "Deploy completed .+$", shouldErr: false, }, { @@ -238,7 +238,7 @@ func TestTimingsCleanup(t *testing.T) { { description: "cleanup success", shouldOutput: "(?m)^Cleaning up...\n", - shouldLog: "Cleanup complete in .+$", + shouldLog: "Cleanup completed .+$", shouldErr: false, }, { From 0fea467c95939420d6557259014262d415bdfdd7 Mon Sep 17 00:00:00 2001 From: gunadhya <6939749+gunadhya@users.noreply.github.com> Date: Wed, 9 Dec 2020 23:56:58 +0530 Subject: [PATCH 05/11] Added gohumanize in util and reworked usage --- integration/skaffold/helper.go | 37 +++------------------------- pkg/skaffold/build/cache/retrieve.go | 10 ++------ pkg/skaffold/diagnose/diagnose.go | 30 +++------------------- pkg/skaffold/runner/build_deploy.go | 10 ++------ pkg/skaffold/runner/deploy.go | 12 ++------- pkg/skaffold/runner/dev.go | 10 ++------ pkg/skaffold/runner/load_images.go | 9 +------ pkg/skaffold/util/util.go | 10 ++++++++ pkg/skaffold/util/util_test.go | 28 +++++++++++++++++++++ 9 files changed, 55 insertions(+), 101 deletions(-) diff --git a/integration/skaffold/helper.go b/integration/skaffold/helper.go index a849e2ad9a2..c279ab7b1ca 100644 --- a/integration/skaffold/helper.go +++ b/integration/skaffold/helper.go @@ -27,7 +27,6 @@ import ( "testing" "time" - "github.com/dustin/go-humanize" "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -187,14 +186,7 @@ func (b *RunBuilder) RunBackground(t *testing.T) io.ReadCloser { go func() { cmd.Wait() - //Create human readable time string - showsTime := humanize.Time(start) - - //Case for when it takes less than a second - if time.Since(start).Seconds() < 1 { - showsTime = time.Since(start).String() + " ago" - } - logrus.Infoln("Ran", showsTime) + logrus.Infoln("Ran", util.ShowHumanizeTime(start)) }() t.Cleanup(func() { @@ -223,14 +215,7 @@ func (b *RunBuilder) Run(t *testing.T) error { if err := cmd.Run(); err != nil { return fmt.Errorf("skaffold %q: %w", b.command, err) } - //Create human readable time string - showsTime := humanize.Time(start) - - //Case for when it takes less than a second - if time.Since(start).Seconds() < 1 { - showsTime = time.Since(start).String() + " ago" - } - logrus.Infoln("Ran", showsTime) + logrus.Infoln("Ran", util.ShowHumanizeTime(start)) return nil } @@ -247,14 +232,7 @@ func (b *RunBuilder) RunWithCombinedOutput(t *testing.T) ([]byte, error) { if err != nil { return out, fmt.Errorf("skaffold %q: %w", b.command, err) } - //Create human readable time string - showsTime := humanize.Time(start) - - //Case for when it takes less than a second - if time.Since(start).Seconds() < 1 { - showsTime = time.Since(start).String() + " ago" - } - logrus.Infoln("Ran", showsTime) + logrus.Infoln("Ran", util.ShowHumanizeTime(start)) return out, nil } @@ -276,14 +254,7 @@ func (b *RunBuilder) RunOrFailOutput(t *testing.T) []byte { } t.Fatalf("skaffold %s: %v, %s", b.command, err, out) } - //Create human readable time string - showsTime := humanize.Time(start) - - //Case for when it takes less than a second - if time.Since(start).Seconds() < 1 { - showsTime = time.Since(start).String() + " ago" - } - logrus.Infoln("Ran", showsTime) + logrus.Infoln("Ran", util.ShowHumanizeTime(start)) return out } diff --git a/pkg/skaffold/build/cache/retrieve.go b/pkg/skaffold/build/cache/retrieve.go index 5cfdf4b9761..e83a5708d5a 100644 --- a/pkg/skaffold/build/cache/retrieve.go +++ b/pkg/skaffold/build/cache/retrieve.go @@ -22,7 +22,6 @@ import ( "io" "time" - "github.com/dustin/go-humanize" "github.com/sirupsen/logrus" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/build" @@ -31,6 +30,7 @@ import ( "github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker" sErrors "github.com/GoogleContainerTools/skaffold/pkg/skaffold/errors" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest" + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/util" ) func (c *cache) Build(ctx context.Context, out io.Writer, tags tag.ImageTags, artifacts []*latest.Artifact, buildAndTest BuildAndTestFn) ([]build.Artifact, error) { @@ -111,14 +111,8 @@ func (c *cache) Build(ctx context.Context, out io.Writer, tags tag.ImageTags, ar Tag: uniqueTag, }) } - //Create human readable time string - showsTime := humanize.Time(start) - //Case for when it takes less than a second - if time.Since(start).Seconds() < 1 { - showsTime = time.Since(start).String() + " ago" - } - logrus.Infoln("Cache check completed", showsTime) + logrus.Infoln("Cache check completed", util.ShowHumanizeTime(start)) bRes, err := buildAndTest(ctx, out, tags, needToBuild) if err != nil { diff --git a/pkg/skaffold/diagnose/diagnose.go b/pkg/skaffold/diagnose/diagnose.go index 5920ed25117..b43ace7850f 100644 --- a/pkg/skaffold/diagnose/diagnose.go +++ b/pkg/skaffold/diagnose/diagnose.go @@ -23,14 +23,13 @@ import ( "io/ioutil" "time" - "github.com/dustin/go-humanize" - "github.com/GoogleContainerTools/skaffold/pkg/skaffold/build" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/color" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/filemon" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/sync" + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/util" ) type Config interface { @@ -116,27 +115,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) - //Create human readable time string - showsTime := humanize.Time(start) - - //Case for when it takes less than a second - if time.Since(start).Seconds() < 1 { - showsTime = time.Since(start).String() + " ago" - } - return showsTime, paths, err + return util.ShowHumanizeTime(start), paths, err } func timeToConstructSyncMap(a *latest.Artifact, cfg docker.Config) (string, error) { start := time.Now() _, err := sync.SyncMap(a, cfg) - //Create human readable time string - showsTime := humanize.Time(start) - - //Case for when it takes less than a second - if time.Since(start).Seconds() < 1 { - showsTime = time.Since(start).String() + " ago" - } - return showsTime, err + return util.ShowHumanizeTime(start), err } func timeToComputeMTimes(deps []string) (string, error) { @@ -145,14 +130,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) } - //Create human readable time string - showsTime := humanize.Time(start) - - //Case for when it takes less than a second - if time.Since(start).Seconds() < 1 { - showsTime = time.Since(start).String() + " ago" - } - return showsTime, nil + return util.ShowHumanizeTime(start), nil } func sizeOfDockerContext(ctx context.Context, a *latest.Artifact, cfg docker.Config) (int64, error) { diff --git a/pkg/skaffold/runner/build_deploy.go b/pkg/skaffold/runner/build_deploy.go index 8a436007fce..092e88c085a 100644 --- a/pkg/skaffold/runner/build_deploy.go +++ b/pkg/skaffold/runner/build_deploy.go @@ -23,7 +23,6 @@ import ( "os" "time" - "github.com/dustin/go-humanize" "github.com/sirupsen/logrus" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/build" @@ -31,6 +30,7 @@ import ( "github.com/GoogleContainerTools/skaffold/pkg/skaffold/color" deployutil "github.com/GoogleContainerTools/skaffold/pkg/skaffold/deploy/util" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest" + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/util" ) // BuildAndTest builds and tests a list of artifacts. @@ -203,14 +203,8 @@ func (r *SkaffoldRunner) imageTags(ctx context.Context, out io.Writer, artifacts if showWarning { color.Yellow.Fprintln(out, "Some taggers failed. Rerun with -vdebug for errors.") } - //Create human readable time string - showsTime := humanize.Time(start) - //Case for when it takes less than a second - if time.Since(start).Seconds() < 1 { - showsTime = time.Since(start).String() + " ago" - } - logrus.Infoln("Tags generated", showsTime) + logrus.Infoln("Tags generated", util.ShowHumanizeTime(start)) return imageTags, nil } diff --git a/pkg/skaffold/runner/deploy.go b/pkg/skaffold/runner/deploy.go index 5ee2a71d617..25c7a2c2ae9 100644 --- a/pkg/skaffold/runner/deploy.go +++ b/pkg/skaffold/runner/deploy.go @@ -22,7 +22,6 @@ import ( "io" "time" - "github.com/dustin/go-humanize" "github.com/sirupsen/logrus" "k8s.io/client-go/tools/clientcmd/api" @@ -33,6 +32,7 @@ import ( "github.com/GoogleContainerTools/skaffold/pkg/skaffold/event" kubernetesclient "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/client" kubectx "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/context" + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/util" ) func (r *SkaffoldRunner) Deploy(ctx context.Context, out io.Writer, artifacts []build.Artifact) error { @@ -158,14 +158,6 @@ func (r *SkaffoldRunner) performStatusCheck(ctx context.Context, out io.Writer) return err } - //Create human readable time string - showsTime := humanize.Time(start) - - //Case for when it takes less than a second - if time.Since(start).Seconds() < 1 { - showsTime = time.Since(start).String() + " ago" - } - - color.Default.Fprintln(out, "Deployments stabilized ", showsTime) + color.Default.Fprintln(out, "Deployments stabilized ", util.ShowHumanizeTime(start)) return nil } diff --git a/pkg/skaffold/runner/dev.go b/pkg/skaffold/runner/dev.go index c6a97b8db9d..fdb36b6fb41 100644 --- a/pkg/skaffold/runner/dev.go +++ b/pkg/skaffold/runner/dev.go @@ -23,7 +23,6 @@ import ( "io" "time" - "github.com/dustin/go-humanize" "github.com/sirupsen/logrus" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/build" @@ -35,6 +34,7 @@ import ( "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/portforward" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/sync" + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/util" "github.com/GoogleContainerTools/skaffold/proto" ) @@ -195,14 +195,8 @@ func (r *SkaffoldRunner) Dev(ctx context.Context, out io.Writer, artifacts []*la event.DevLoopFailedWithErrorCode(r.devIteration, proto.StatusCode_DEVINIT_REGISTER_CONFIG_DEP, err) return fmt.Errorf("watching skaffold configuration %q: %w", r.runCtx.ConfigurationFile(), err) } - //Create human readable time string - showsTime := humanize.Time(start) - //Case for when it takes less than a second - if time.Since(start).Seconds() < 1 { - showsTime = time.Since(start).String() + " ago" - } - logrus.Infoln("List generated ", showsTime) + logrus.Infoln("List generated ", util.ShowHumanizeTime(start)) // Init Sync State if err := sync.Init(ctx, artifacts); err != nil { diff --git a/pkg/skaffold/runner/load_images.go b/pkg/skaffold/runner/load_images.go index e43e16c7781..6f6fbca924c 100644 --- a/pkg/skaffold/runner/load_images.go +++ b/pkg/skaffold/runner/load_images.go @@ -25,7 +25,6 @@ import ( "time" "github.com/docker/distribution/reference" - "github.com/dustin/go-humanize" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/build" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/color" @@ -86,14 +85,8 @@ func (r *SkaffoldRunner) loadImages(ctx context.Context, out io.Writer, artifact color.Green.Fprintln(out, "Loaded") } - //Create human readable time string - showsTime := humanize.Time(start) - //Case for when it takes less than a second - if time.Since(start).Seconds() < 1 { - showsTime = time.Since(start).String() + " ago" - } - color.Default.Fprintln(out, "Images loaded", showsTime) + color.Default.Fprintln(out, "Images loaded", util.ShowHumanizeTime(start)) return nil } diff --git a/pkg/skaffold/util/util.go b/pkg/skaffold/util/util.go index a8e51f20368..4e32d4d46ea 100644 --- a/pkg/skaffold/util/util.go +++ b/pkg/skaffold/util/util.go @@ -27,7 +27,9 @@ import ( "regexp" "sort" "strings" + "time" + "github.com/dustin/go-humanize" "github.com/sirupsen/logrus" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/walk" @@ -341,3 +343,11 @@ func IsSubPath(basepath string, targetpath string) bool { func hasHiddenPrefix(s string) bool { return strings.HasPrefix(s, hiddenPrefix) } + +// ShowHumanizeTime returns time in human readable format +func ShowHumanizeTime(start time.Time) string { + if time.Since(start).Seconds() < 1 { + return time.Since(start).String() + " ago" + } + return humanize.Time(start) +} diff --git a/pkg/skaffold/util/util_test.go b/pkg/skaffold/util/util_test.go index 0423803698a..475791f5408 100644 --- a/pkg/skaffold/util/util_test.go +++ b/pkg/skaffold/util/util_test.go @@ -19,6 +19,7 @@ package util import ( "path/filepath" "testing" + "time" "github.com/mitchellh/go-homedir" @@ -468,3 +469,30 @@ func TestIsURL(t *testing.T) { func stringPointer(s string) *string { return &s } + +func TestShowHumanizeTime(t *testing.T) { + currTime := time.Now() + tests := []struct { + description string + value time.Time + expected string + }{ + { + description: "Case for 10 seconds", + value: currTime.Add(-time.Second * 10), + expected: "10 seconds ago", + }, + { + description: "Case for a Minute", + value: currTime.Add(-time.Minute * 1), + expected: "1 minute ago", + }, + } + for _, test := range tests { + testutil.Run(t, test.description, func(t *testutil.T) { + humanizedValue := ShowHumanizeTime(test.value) + + t.CheckDeepEqual(test.expected, humanizedValue) + }) + } +} From 912173d31f9ab6b710e412aed56266963542b61e Mon Sep 17 00:00:00 2001 From: gunadhya <6939749+gunadhya@users.noreply.github.com> Date: Sun, 27 Dec 2020 05:03:58 +0530 Subject: [PATCH 06/11] Updated ShowHumanizeTime --- integration/skaffold/helper.go | 8 ++++---- pkg/skaffold/build/cache/retrieve.go | 2 +- pkg/skaffold/runner/build_deploy.go | 2 +- pkg/skaffold/runner/deploy.go | 2 +- pkg/skaffold/runner/dev.go | 2 +- pkg/skaffold/runner/load_images.go | 2 +- pkg/skaffold/util/util.go | 22 +++++++++++++++++++--- pkg/skaffold/util/util_test.go | 17 +++++++++-------- 8 files changed, 37 insertions(+), 20 deletions(-) diff --git a/integration/skaffold/helper.go b/integration/skaffold/helper.go index c279ab7b1ca..b4802e7af00 100644 --- a/integration/skaffold/helper.go +++ b/integration/skaffold/helper.go @@ -186,7 +186,7 @@ func (b *RunBuilder) RunBackground(t *testing.T) io.ReadCloser { go func() { cmd.Wait() - logrus.Infoln("Ran", util.ShowHumanizeTime(start)) + logrus.Infoln("Ran in", util.ShowHumanizeTime(start)) }() t.Cleanup(func() { @@ -215,7 +215,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", util.ShowHumanizeTime(start)) + logrus.Infoln("Ran in", util.ShowHumanizeTime(start)) return nil } @@ -232,7 +232,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", util.ShowHumanizeTime(start)) + logrus.Infoln("Ran in", util.ShowHumanizeTime(start)) return out, nil } @@ -254,7 +254,7 @@ func (b *RunBuilder) RunOrFailOutput(t *testing.T) []byte { } t.Fatalf("skaffold %s: %v, %s", b.command, err, out) } - logrus.Infoln("Ran", util.ShowHumanizeTime(start)) + logrus.Infoln("Ran in", util.ShowHumanizeTime(start)) return out } diff --git a/pkg/skaffold/build/cache/retrieve.go b/pkg/skaffold/build/cache/retrieve.go index e83a5708d5a..38023281921 100644 --- a/pkg/skaffold/build/cache/retrieve.go +++ b/pkg/skaffold/build/cache/retrieve.go @@ -112,7 +112,7 @@ func (c *cache) Build(ctx context.Context, out io.Writer, tags tag.ImageTags, ar }) } - logrus.Infoln("Cache check completed", util.ShowHumanizeTime(start)) + logrus.Infoln("Cache check completed in", util.ShowHumanizeTime(start)) bRes, err := buildAndTest(ctx, out, tags, needToBuild) if err != nil { diff --git a/pkg/skaffold/runner/build_deploy.go b/pkg/skaffold/runner/build_deploy.go index 092e88c085a..dcc144c681c 100644 --- a/pkg/skaffold/runner/build_deploy.go +++ b/pkg/skaffold/runner/build_deploy.go @@ -204,7 +204,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", util.ShowHumanizeTime(start)) + logrus.Infoln("Tags generated in", util.ShowHumanizeTime(start)) return imageTags, nil } diff --git a/pkg/skaffold/runner/deploy.go b/pkg/skaffold/runner/deploy.go index 25c7a2c2ae9..e913b165c3f 100644 --- a/pkg/skaffold/runner/deploy.go +++ b/pkg/skaffold/runner/deploy.go @@ -158,6 +158,6 @@ func (r *SkaffoldRunner) performStatusCheck(ctx context.Context, out io.Writer) return err } - color.Default.Fprintln(out, "Deployments stabilized ", util.ShowHumanizeTime(start)) + color.Default.Fprintln(out, "Deployments stabilized in", util.ShowHumanizeTime(start)) return nil } diff --git a/pkg/skaffold/runner/dev.go b/pkg/skaffold/runner/dev.go index fdb36b6fb41..904d81c9dc5 100644 --- a/pkg/skaffold/runner/dev.go +++ b/pkg/skaffold/runner/dev.go @@ -196,7 +196,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 ", util.ShowHumanizeTime(start)) + logrus.Infoln("List generated in", util.ShowHumanizeTime(start)) // Init Sync State if err := sync.Init(ctx, artifacts); err != nil { diff --git a/pkg/skaffold/runner/load_images.go b/pkg/skaffold/runner/load_images.go index 6f6fbca924c..89dbb403678 100644 --- a/pkg/skaffold/runner/load_images.go +++ b/pkg/skaffold/runner/load_images.go @@ -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", util.ShowHumanizeTime(start)) + color.Default.Fprintln(out, "Images loaded in", util.ShowHumanizeTime(start)) return nil } diff --git a/pkg/skaffold/util/util.go b/pkg/skaffold/util/util.go index 4e32d4d46ea..ae284f568b5 100644 --- a/pkg/skaffold/util/util.go +++ b/pkg/skaffold/util/util.go @@ -29,7 +29,6 @@ import ( "strings" "time" - "github.com/dustin/go-humanize" "github.com/sirupsen/logrus" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/walk" @@ -346,8 +345,25 @@ 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) + longTine := shortTime.String() + out := time.Time{}.Add(shortTime) + if time.Since(start).Seconds() < 1 { - return time.Since(start).String() + " ago" + return time.Since(start).String() + } + + longTine = strings.ReplaceAll(longTine, "h", " hour ") + longTine = strings.ReplaceAll(longTine, "m", " minute ") + longTine = strings.ReplaceAll(longTine, "s", " second") + if out.Hour() > 1 { + longTine = strings.ReplaceAll(longTine, "hour", "hours") + } + if out.Minute() > 1 { + longTine = strings.ReplaceAll(longTine, "minute", "minutes") + } + if out.Second() > 1 { + longTine = strings.ReplaceAll(longTine, "second", "seconds") } - return humanize.Time(start) + return longTine } diff --git a/pkg/skaffold/util/util_test.go b/pkg/skaffold/util/util_test.go index 475791f5408..e7e0047eeaa 100644 --- a/pkg/skaffold/util/util_test.go +++ b/pkg/skaffold/util/util_test.go @@ -472,27 +472,28 @@ func stringPointer(s string) *string { func TestShowHumanizeTime(t *testing.T) { currTime := time.Now() + duration1, _ := time.ParseDuration("1h58m30.918273645s") + duration2, _ := time.ParseDuration("5.23494327s") tests := []struct { description string value time.Time expected string }{ { - description: "Case for 10 seconds", - value: currTime.Add(-time.Second * 10), - expected: "10 seconds ago", + description: "Case for 1h58m30.918273645s", + value: currTime.Add(-duration1), + expected: "1 hour 58 minutes 30.918 seconds", }, { - description: "Case for a Minute", - value: currTime.Add(-time.Minute * 1), - expected: "1 minute ago", + description: "Case for 5.23494327s", + value: currTime.Add(-duration2), + expected: "5.235 seconds", }, } for _, test := range tests { testutil.Run(t, test.description, func(t *testutil.T) { humanizedValue := ShowHumanizeTime(test.value) - - t.CheckDeepEqual(test.expected, humanizedValue) + t.CheckMatches(test.expected, humanizedValue) }) } } From ec75910494f36e1ec3ea5e7a4869a2a3daccc074 Mon Sep 17 00:00:00 2001 From: gunadhya <6939749+gunadhya@users.noreply.github.com> Date: Sun, 27 Dec 2020 06:05:53 +0530 Subject: [PATCH 07/11] fixed spelling error --- pkg/skaffold/util/util.go | 16 ++++++++-------- pkg/skaffold/util/util_test.go | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/pkg/skaffold/util/util.go b/pkg/skaffold/util/util.go index ae284f568b5..84455463dfb 100644 --- a/pkg/skaffold/util/util.go +++ b/pkg/skaffold/util/util.go @@ -346,24 +346,24 @@ 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) - longTine := shortTime.String() + longTime := shortTime.String() out := time.Time{}.Add(shortTime) if time.Since(start).Seconds() < 1 { return time.Since(start).String() } - longTine = strings.ReplaceAll(longTine, "h", " hour ") - longTine = strings.ReplaceAll(longTine, "m", " minute ") - longTine = strings.ReplaceAll(longTine, "s", " second") + longTime = strings.ReplaceAll(longTime, "h", " hour ") + longTime = strings.ReplaceAll(longTime, "m", " minute ") + longTime = strings.ReplaceAll(longTime, "s", " second") if out.Hour() > 1 { - longTine = strings.ReplaceAll(longTine, "hour", "hours") + longTime = strings.ReplaceAll(longTime, "hour", "hours") } if out.Minute() > 1 { - longTine = strings.ReplaceAll(longTine, "minute", "minutes") + longTime = strings.ReplaceAll(longTime, "minute", "minutes") } if out.Second() > 1 { - longTine = strings.ReplaceAll(longTine, "second", "seconds") + longTime = strings.ReplaceAll(longTime, "second", "seconds") } - return longTine + return longTime } diff --git a/pkg/skaffold/util/util_test.go b/pkg/skaffold/util/util_test.go index e7e0047eeaa..3eca1105f8b 100644 --- a/pkg/skaffold/util/util_test.go +++ b/pkg/skaffold/util/util_test.go @@ -482,12 +482,12 @@ func TestShowHumanizeTime(t *testing.T) { { description: "Case for 1h58m30.918273645s", value: currTime.Add(-duration1), - expected: "1 hour 58 minutes 30.918 seconds", + expected: "1 hour 58 minutes 30.9", }, { description: "Case for 5.23494327s", value: currTime.Add(-duration2), - expected: "5.235 seconds", + expected: "5.2", }, } for _, test := range tests { From 14e94696e8d2470e8e3fb90af3f439664a0c14d6 Mon Sep 17 00:00:00 2001 From: gunadhya <6939749+gunadhya@users.noreply.github.com> Date: Sun, 27 Dec 2020 19:09:53 +0530 Subject: [PATCH 08/11] Replaced humanize with util.ShowHumanizeTime() in timings.go --- pkg/skaffold/runner/timings.go | 47 ++++------------------------- pkg/skaffold/runner/timings_test.go | 10 +++--- 2 files changed, 11 insertions(+), 46 deletions(-) diff --git a/pkg/skaffold/runner/timings.go b/pkg/skaffold/runner/timings.go index 91ae454aa8c..ac93a3803b6 100644 --- a/pkg/skaffold/runner/timings.go +++ b/pkg/skaffold/runner/timings.go @@ -21,7 +21,6 @@ import ( "io" "time" - "github.com/dustin/go-humanize" "github.com/sirupsen/logrus" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/build" @@ -30,6 +29,7 @@ import ( "github.com/GoogleContainerTools/skaffold/pkg/skaffold/deploy" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/test" + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/util" ) // WithTimings creates a deployer that logs the duration of each phase. @@ -61,14 +61,7 @@ func (w withTimings) Build(ctx context.Context, out io.Writer, tags tag.ImageTag if err != nil { return nil, err } - //Create human readable time string - showsTime := humanize.Time(start) - - //Case for when it takes less than a second - if time.Since(start).Seconds() < 1 { - showsTime = time.Since(start).String() + " ago" - } - logrus.Infoln("Build completed", showsTime) + logrus.Infoln("Build completed in", util.ShowHumanizeTime(start)) return bRes, nil } @@ -79,14 +72,7 @@ func (w withTimings) Test(ctx context.Context, out io.Writer, builds []build.Art if err != nil { return err } - //Create human readable time string - showsTime := humanize.Time(start) - - //Case for when it takes less than a second - if time.Since(start).Seconds() < 1 { - showsTime = time.Since(start).String() + " ago" - } - logrus.Infoln("Test completed", showsTime) + logrus.Infoln("Test completed in", util.ShowHumanizeTime(start)) return nil } @@ -98,14 +84,7 @@ func (w withTimings) Deploy(ctx context.Context, out io.Writer, builds []build.A if err != nil { return nil, err } - //Create human readable time string - showsTime := humanize.Time(start) - - //Case for when it takes less than a second - if time.Since(start).Seconds() < 1 { - showsTime = time.Since(start).String() + " ago" - } - logrus.Infoln("Deploy completed", showsTime) + logrus.Infoln("Deploy completed in", util.ShowHumanizeTime(start)) return ns, err } @@ -117,14 +96,7 @@ func (w withTimings) Cleanup(ctx context.Context, out io.Writer) error { if err != nil { return err } - //Create human readable time string - showsTime := humanize.Time(start) - - //Case for when it takes less than a second - if time.Since(start).Seconds() < 1 { - showsTime = time.Since(start).String() + " ago" - } - logrus.Infoln("Cleanup completed", showsTime) + logrus.Infoln("Cleanup completed in", util.ShowHumanizeTime(start)) return nil } @@ -136,13 +108,6 @@ func (w withTimings) Prune(ctx context.Context, out io.Writer) error { if err != nil { return err } - //Create human readable time string - showsTime := humanize.Time(start) - - //Case for when it takes less than a second - if time.Since(start).Seconds() < 1 { - showsTime = time.Since(start).String() - } - logrus.Infoln("Image prune completed", showsTime) + logrus.Infoln("Image prune completed in", util.ShowHumanizeTime(start)) return nil } diff --git a/pkg/skaffold/runner/timings_test.go b/pkg/skaffold/runner/timings_test.go index b0936730071..d8afc56664e 100644 --- a/pkg/skaffold/runner/timings_test.go +++ b/pkg/skaffold/runner/timings_test.go @@ -94,7 +94,7 @@ func TestTimingsBuild(t *testing.T) { { description: "build success", shouldOutput: "", - shouldLog: "Build completed .+$", + shouldLog: "Build completed in .+$", shouldErr: false, }, { @@ -130,7 +130,7 @@ func TestTimingsPrune(t *testing.T) { { description: "test success", shouldOutput: "(?m)^Pruning images...\n", - shouldLog: "Image prune completed .+$", + shouldLog: "Image prune completed in .+$", shouldErr: false, }, { @@ -166,7 +166,7 @@ func TestTimingsTest(t *testing.T) { { description: "test success", shouldOutput: "", - shouldLog: "Test completed .+$", + shouldLog: "Test completed in .+$", shouldErr: false, }, { @@ -202,7 +202,7 @@ func TestTimingsDeploy(t *testing.T) { { description: "prune success", shouldOutput: "(?m)^Starting deploy...\n", - shouldLog: "Deploy completed .+$", + shouldLog: "Deploy completed in .+$", shouldErr: false, }, { @@ -238,7 +238,7 @@ func TestTimingsCleanup(t *testing.T) { { description: "cleanup success", shouldOutput: "(?m)^Cleaning up...\n", - shouldLog: "Cleanup completed .+$", + shouldLog: "Cleanup completed in .+$", shouldErr: false, }, { From f83503d563d8a703610f4d3a897e1cc380f53bbb Mon Sep 17 00:00:00 2001 From: gunadhya <6939749+gunadhya@users.noreply.github.com> Date: Wed, 30 Dec 2020 01:28:29 +0530 Subject: [PATCH 09/11] Updated test from CheckMatches to CheckDeepEqual --- pkg/skaffold/util/util_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/skaffold/util/util_test.go b/pkg/skaffold/util/util_test.go index 3eca1105f8b..ebcdc5352ac 100644 --- a/pkg/skaffold/util/util_test.go +++ b/pkg/skaffold/util/util_test.go @@ -482,18 +482,18 @@ func TestShowHumanizeTime(t *testing.T) { { description: "Case for 1h58m30.918273645s", value: currTime.Add(-duration1), - expected: "1 hour 58 minutes 30.9", + expected: "1 hour 58 minutes 30.918 seconds", }, { description: "Case for 5.23494327s", value: currTime.Add(-duration2), - expected: "5.2", + expected: "5.235 seconds", }, } for _, test := range tests { testutil.Run(t, test.description, func(t *testutil.T) { humanizedValue := ShowHumanizeTime(test.value) - t.CheckMatches(test.expected, humanizedValue) + t.CheckDeepEqual(test.expected, humanizedValue) }) } } From 91a6f115bccf64962327a7da13501c67a2a0f032 Mon Sep 17 00:00:00 2001 From: gunadhya <6939749+gunadhya@users.noreply.github.com> Date: Wed, 30 Dec 2020 16:51:24 +0530 Subject: [PATCH 10/11] Updated TestShowHumanizeTime for upper and lower bound cases --- pkg/skaffold/util/util_test.go | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/pkg/skaffold/util/util_test.go b/pkg/skaffold/util/util_test.go index ebcdc5352ac..9af4795b3d4 100644 --- a/pkg/skaffold/util/util_test.go +++ b/pkg/skaffold/util/util_test.go @@ -475,25 +475,38 @@ func TestShowHumanizeTime(t *testing.T) { duration1, _ := time.ParseDuration("1h58m30.918273645s") duration2, _ := time.ParseDuration("5.23494327s") tests := []struct { - description string - value time.Time - expected string + description string + value time.Time + expected string + expectedUp string + expectedDown string }{ { - description: "Case for 1h58m30.918273645s", - value: currTime.Add(-duration1), - expected: "1 hour 58 minutes 30.918 seconds", + 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 5.23494327s", - value: currTime.Add(-duration2), - expected: "5.235 seconds", + description: "Case for 5.23494327s", + value: currTime.Add(-duration2), + expected: "5.235 seconds", + expectedUp: "5.236 seconds", + expectedDown: "5.234 seconds", }, } for _, test := range tests { testutil.Run(t, test.description, func(t *testutil.T) { humanizedValue := ShowHumanizeTime(test.value) - t.CheckDeepEqual(test.expected, humanizedValue) + switch humanizedValue { + case test.expectedUp: + t.CheckDeepEqual(test.expectedUp, humanizedValue) + case test.expectedDown: + t.CheckDeepEqual(test.expectedDown, humanizedValue) + default: + t.CheckDeepEqual(test.expected, humanizedValue) + } }) } } From f61028e48e21954a49925a5491c606092590939e Mon Sep 17 00:00:00 2001 From: gunadhya <6939749+gunadhya@users.noreply.github.com> Date: Thu, 31 Dec 2020 18:45:35 +0530 Subject: [PATCH 11/11] Converted time.Time to time.Duration in ShowHumanizeTime() --- integration/skaffold/helper.go | 8 ++--- pkg/skaffold/build/cache/retrieve.go | 2 +- pkg/skaffold/diagnose/diagnose.go | 6 ++-- pkg/skaffold/runner/build_deploy.go | 2 +- pkg/skaffold/runner/deploy.go | 2 +- pkg/skaffold/runner/dev.go | 2 +- pkg/skaffold/runner/load_images.go | 2 +- pkg/skaffold/runner/timings.go | 10 +++---- pkg/skaffold/util/util.go | 8 ++--- pkg/skaffold/util/util_test.go | 44 ++++++++++++---------------- 10 files changed, 39 insertions(+), 47 deletions(-) diff --git a/integration/skaffold/helper.go b/integration/skaffold/helper.go index 23fd6191c11..6f717f39677 100644 --- a/integration/skaffold/helper.go +++ b/integration/skaffold/helper.go @@ -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() { @@ -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 } @@ -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 } @@ -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 } diff --git a/pkg/skaffold/build/cache/retrieve.go b/pkg/skaffold/build/cache/retrieve.go index b7c70a65c23..c1388cc71c1 100644 --- a/pkg/skaffold/build/cache/retrieve.go +++ b/pkg/skaffold/build/cache/retrieve.go @@ -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 { diff --git a/pkg/skaffold/diagnose/diagnose.go b/pkg/skaffold/diagnose/diagnose.go index 31b107360d3..d6a7d7957ee 100644 --- a/pkg/skaffold/diagnose/diagnose.go +++ b/pkg/skaffold/diagnose/diagnose.go @@ -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) { @@ -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) { diff --git a/pkg/skaffold/runner/build_deploy.go b/pkg/skaffold/runner/build_deploy.go index 339c0e93618..53203c65f21 100644 --- a/pkg/skaffold/runner/build_deploy.go +++ b/pkg/skaffold/runner/build_deploy.go @@ -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 } diff --git a/pkg/skaffold/runner/deploy.go b/pkg/skaffold/runner/deploy.go index 0851bac8434..dec55bacd70 100644 --- a/pkg/skaffold/runner/deploy.go +++ b/pkg/skaffold/runner/deploy.go @@ -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 } diff --git a/pkg/skaffold/runner/dev.go b/pkg/skaffold/runner/dev.go index f2e4bf509df..7144fba098d 100644 --- a/pkg/skaffold/runner/dev.go +++ b/pkg/skaffold/runner/dev.go @@ -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 { diff --git a/pkg/skaffold/runner/load_images.go b/pkg/skaffold/runner/load_images.go index 89dbb403678..51dee61af75 100644 --- a/pkg/skaffold/runner/load_images.go +++ b/pkg/skaffold/runner/load_images.go @@ -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 } diff --git a/pkg/skaffold/runner/timings.go b/pkg/skaffold/runner/timings.go index ac93a3803b6..8f01cab28a6 100644 --- a/pkg/skaffold/runner/timings.go +++ b/pkg/skaffold/runner/timings.go @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } diff --git a/pkg/skaffold/util/util.go b/pkg/skaffold/util/util.go index 84455463dfb..91e7a579b01 100644 --- a/pkg/skaffold/util/util.go +++ b/pkg/skaffold/util/util.go @@ -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 ") diff --git a/pkg/skaffold/util/util_test.go b/pkg/skaffold/util/util_test.go index 9af4795b3d4..91df2f7761e 100644 --- a/pkg/skaffold/util/util_test.go +++ b/pkg/skaffold/util/util_test.go @@ -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) }) } }