Skip to content

Commit d6bddec

Browse files
committed
Use time.Time not string for LastModified
Signed-off-by: Anders F Björklund <[email protected]>
1 parent cf20b24 commit d6bddec

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

pkg/downloader/downloader.go

+23-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"path"
1414
"path/filepath"
1515
"strings"
16+
"time"
1617

1718
"github.com/cheggaaa/pb/v3"
1819
"github.com/containerd/continuity/fs"
@@ -44,7 +45,7 @@ const (
4445
type Result struct {
4546
Status Status
4647
CachePath string // "/Users/foo/Library/Caches/lima/download/by-url-sha256/<SHA256_OF_URL>/data"
47-
LastModified string
48+
LastModified time.Time
4849
ContentType string
4950
ValidatedDigest bool
5051
}
@@ -134,6 +135,24 @@ func readFile(path string) string {
134135
return string(b)
135136
}
136137

138+
func readTime(path string) time.Time {
139+
if path == "" {
140+
return time.Time{}
141+
}
142+
if _, err := os.Stat(path); err != nil {
143+
return time.Time{}
144+
}
145+
b, err := os.ReadFile(path)
146+
if err != nil {
147+
return time.Time{}
148+
}
149+
t, err := time.Parse(http.TimeFormat, string(b))
150+
if err != nil {
151+
return time.Time{}
152+
}
153+
return t
154+
}
155+
137156
// Download downloads the remote resource into the local path.
138157
//
139158
// Download caches the remote resource if WithCache or WithCacheDir option is specified.
@@ -228,7 +247,7 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result,
228247
res := &Result{
229248
Status: StatusUsedCache,
230249
CachePath: shadData,
231-
LastModified: readFile(shadTime),
250+
LastModified: readTime(shadTime),
232251
ContentType: readFile(shadType),
233252
ValidatedDigest: o.expectedDigest != "",
234253
}
@@ -259,7 +278,7 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result,
259278
res := &Result{
260279
Status: StatusDownloaded,
261280
CachePath: shadData,
262-
LastModified: readFile(shadTime),
281+
LastModified: readTime(shadTime),
263282
ContentType: readFile(shadType),
264283
ValidatedDigest: o.expectedDigest != "",
265284
}
@@ -309,7 +328,7 @@ func Cached(remote string, opts ...Opt) (*Result, error) {
309328
res := &Result{
310329
Status: StatusUsedCache,
311330
CachePath: shadData,
312-
LastModified: readFile(shadTime),
331+
LastModified: readTime(shadTime),
313332
ContentType: readFile(shadType),
314333
ValidatedDigest: o.expectedDigest != "",
315334
}

pkg/downloader/downloader_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func TestDownloadRemote(t *testing.T) {
116116
r, err := Download(context.Background(), "", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir))
117117
assert.NilError(t, err)
118118
assert.Equal(t, StatusDownloaded, r.Status)
119-
assert.Equal(t, dummyRemoteFileStat.ModTime().UTC().Format(time.RFC1123), strings.Replace(r.LastModified, "GMT", "UTC", 1))
119+
assert.Equal(t, dummyRemoteFileStat.ModTime().Truncate(time.Second).UTC(), r.LastModified)
120120
assert.Equal(t, "text/plain; charset=utf-8", r.ContentType)
121121
})
122122
}

0 commit comments

Comments
 (0)