From ce788fcd88289f37f99bb434de9c67172f763a3e Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Thu, 26 Jan 2017 18:47:54 -0500 Subject: [PATCH] fetch: Fetch a ChangeLog from a Repo and get its remote last-modified time Signed-off-by: Vincent Batts --- fetch/fetch.go | 39 +++++++++++++++++++++++++++++++++++++++ fetch/fetch_test.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 fetch/fetch.go create mode 100644 fetch/fetch_test.go diff --git a/fetch/fetch.go b/fetch/fetch.go new file mode 100644 index 0000000..6d7f54c --- /dev/null +++ b/fetch/fetch.go @@ -0,0 +1,39 @@ +package fetch + +import ( + "fmt" + "net/http" + "time" + + "../changelog" +) + +type Repo struct { + URL string +} + +func (r Repo) get(file string) (*http.Response, error) { + return http.Get(r.URL + "/" + file) +} + +func (r Repo) ChangeLog() (e []changelog.Entry, mtime time.Time, err error) { + resp, err := r.get("ChangeLog.txt") + if err != nil { + return nil, time.Unix(0, 0), err + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + return nil, time.Unix(0, 0), fmt.Errorf("%d status from %s", resp.StatusCode, resp.Request.URL) + } + + mtime, err = http.ParseTime(resp.Header.Get("last-modified")) + if err != nil { + return nil, time.Unix(0, 0), err + } + + e, err = changelog.Parse(resp.Body) + if err != nil { + return nil, mtime, err + } + return e, mtime, nil +} diff --git a/fetch/fetch_test.go b/fetch/fetch_test.go new file mode 100644 index 0000000..04c6e35 --- /dev/null +++ b/fetch/fetch_test.go @@ -0,0 +1,36 @@ +package fetch + +import ( + "net/http" + "net/http/httptest" + "os" + "testing" +) + +func TestFetchChangeLog(t *testing.T) { + server := httptest.NewServer(http.FileServer(http.Dir("../changelog/testdata/"))) + defer server.Close() + + r := Repo{ + URL: server.URL, + } + + e, mtime, err := r.ChangeLog() + if err != nil { + t.Fatal(err) + } + + expectedLen := 52 + if len(e) != expectedLen { + t.Errorf("expected %d entries; got %d", expectedLen, len(e)) + } + + stat, err := os.Stat("../changelog/testdata/ChangeLog.txt") + if err != nil { + t.Fatal(err) + } + + if mtime.Unix() != stat.ModTime().Unix() { + t.Errorf("time stamps not the same: expected %d; got %d", stat.ModTime().Unix(), mtime.Unix()) + } +}