summaryrefslogtreecommitdiff
path: root/src/helsinki.at/rhimport
diff options
context:
space:
mode:
authorChristian Pointner <equinox@helsinki.at>2015-12-08 11:25:52 (GMT)
committerChristian Pointner <equinox@helsinki.at>2015-12-08 11:29:50 (GMT)
commit3012f89fff0ee185c11dca56d8172c04c4a1987f (patch)
tree1469f51ed2087c9756ffb5163e82ee862664162a /src/helsinki.at/rhimport
parenta6b29b342a73582a7f02877dda648d8d7a44ec57 (diff)
fetcher parses SourceUri and selects right fetcher function
Diffstat (limited to 'src/helsinki.at/rhimport')
-rw-r--r--src/helsinki.at/rhimport/fetcher.go38
1 files changed, 35 insertions, 3 deletions
diff --git a/src/helsinki.at/rhimport/fetcher.go b/src/helsinki.at/rhimport/fetcher.go
index 97fe7ff..26077b8 100644
--- a/src/helsinki.at/rhimport/fetcher.go
+++ b/src/helsinki.at/rhimport/fetcher.go
@@ -25,14 +25,46 @@
package rhimport
import (
+ "fmt"
+ "net/url"
"path"
)
+func FetchFileHttp(ctx *ImportContext, uri *url.URL) (err error) {
+ rhl.Printf("HTTP/HTTPS fetcher called for '%s'", ctx.SourceUri)
+ ctx.SourceFile = ctx.Config.TempDir + "/" + path.Base(uri.Path)
+ ctx.DeleteSourceFile = true
+ return
+}
+
+func FetchFileLocal(ctx *ImportContext, uri *url.URL) (err error) {
+ rhl.Printf("Local fetcher called for '%s'", ctx.SourceUri)
+ ctx.SourceFile = uri.Path
+ ctx.DeleteSourceFile = false
+ return
+}
+
+type FetchFunc func(*ImportContext, *url.URL) (err error)
+
+var (
+ fetchers = map[string]FetchFunc{
+ "http": FetchFileHttp,
+ "https": FetchFileHttp,
+ "local": FetchFileLocal,
+ }
+)
+
func FetchFile(ctx *ImportContext) (err error) {
- // TODO: fetch file from ctx.SourceUri and put it into ctx.Config.TempDir
+ var uri *url.URL
+ if uri, err = url.Parse(ctx.SourceUri); err != nil {
+ return
+ }
- ctx.SourceFile = ctx.Config.TempDir + "/" + path.Base(ctx.SourceUri)
- ctx.DeleteSourceFile = true
+ if fetcher, ok := fetchers[uri.Scheme]; ok {
+ err = fetcher(ctx, uri)
+ } else {
+ err = fmt.Errorf("No fetcher for uri scheme '%s' found.", uri.Scheme)
+ }
return
}