mirror of
https://github.com/vbatts/sl-feeds.git
synced 2025-02-02 13:53:30 +00:00
fetch: Fetch a ChangeLog from a Repo
and get its remote last-modified time Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
parent
8e97e3d16f
commit
ce788fcd88
2 changed files with 75 additions and 0 deletions
39
fetch/fetch.go
Normal file
39
fetch/fetch.go
Normal file
|
@ -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
|
||||
}
|
36
fetch/fetch_test.go
Normal file
36
fetch/fetch_test.go
Normal file
|
@ -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())
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue