From 871ba61aa1cc918c2467d0d880e0bb1c11cf4415 Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Sat, 4 Feb 2017 11:06:57 -0500 Subject: [PATCH] *: prepare for time comparison with remote Signed-off-by: Vincent Batts --- cmd/sl-feeds/main.go | 41 ++++++++++++++++++++++++++++++++++++++--- fetch/fetch.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/cmd/sl-feeds/main.go b/cmd/sl-feeds/main.go index 10d52f4..f4ebb4b 100644 --- a/cmd/sl-feeds/main.go +++ b/cmd/sl-feeds/main.go @@ -35,13 +35,42 @@ func main() { app.Action = func(c *cli.Context) error { if c.Bool("sample-config") { c := Config{ - Dest: "./public_html/feeds/", + Dest: "$HOME/public_html/feeds/", + Mirrors: []Mirror{ + Mirror{ + URL: "http://slackware.osuosl.org/", + Releases: []string{ + "slackware-14.0", + "slackware-14.1", + "slackware-14.2", + "slackware-current", + "slackware64-14.0", + "slackware64-14.1", + "slackware64-14.2", + "slackware64-current", + }, + }, + Mirror{ + URL: "http://ftp.arm.slackware.com/slackwarearm/", + Releases: []string{ + "slackwarearm-14.1", + "slackwarearm-14.2", + "slackwarearm-current", + }, + }, + }, } toml.NewEncoder(os.Stdout).Encode(c) return nil } - fmt.Println(config.Dest) + fmt.Println(os.ExpandEnv(config.Dest)) + /* + for each mirror in Mirrors + if there is not a $release.RSS file, then fetch the whole ChangeLog + if there is a $release.RSS file, then stat the file and only fetch remote if it is newer than the local RSS file + if the remote returns any error (404, 503, etc) then print a warning but continue + */ return nil } @@ -67,5 +96,11 @@ func main() { } type Config struct { - Dest string + Dest string + Mirrors []Mirror +} + +type Mirror struct { + URL string + Releases []string } diff --git a/fetch/fetch.go b/fetch/fetch.go index 11bd238..a908dee 100644 --- a/fetch/fetch.go +++ b/fetch/fetch.go @@ -13,10 +13,38 @@ type Repo struct { URL string } +func (r Repo) head(file string) (*http.Response, error) { + return http.Head(r.URL + "/" + file) +} func (r Repo) get(file string) (*http.Response, error) { return http.Get(r.URL + "/" + file) } +func (r Repo) NewerChangeLog(than time.Time) (e []changelog.Entry, mtime time.Time, err error) { + resp, err := r.head("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 + } + + if mtime.After(than) { + return r.ChangeLog() + } + return nil, time.Unix(0, 0), NotNewer +} + +// NotNewer is a status error usage to indicate that the remote file is not newer +var NotNewer = fmt.Errorf("Remote file is not newer than provided time") + // ChangeLog fetches the ChangeLog.txt for this remote Repo, along with the // last-modified (for comparisons). func (r Repo) ChangeLog() (e []changelog.Entry, mtime time.Time, err error) {