func (s *SourceIndex) Fetch(out io.Writer) (string, error) { tmp, err := ioutil.TempDir("", "") if err != nil { return "", err } r, err := urlReader(s.URL) if err != nil { return "", err } defer r.Close() data, err := ioutil.ReadAll(r) if err != nil { return "", err } var index structs.Index if err := json.Unmarshal(data, &index); err != nil { return "", err } if err := provider.FromEnv().IndexDownload(&index, tmp); err != nil { return "", err } return tmp, nil }
// Provider returns the appropriate provider interface based on the env func Provider() provider.Provider { if os.Getenv("PROVIDER") == "test" { return TestProvider } return provider.FromEnv() }
func init() { currentProvider = provider.FromEnv() var buf bytes.Buffer currentProvider.Initialize(structs.ProviderOptions{ LogOutput: &buf, }) }
func urlReader(url_ string) (io.ReadCloser, error) { u, err := url.Parse(url_) if err != nil { return nil, err } switch u.Scheme { case "file": fd, err := os.Open(u.Path) if err != nil { return nil, err } return fd, nil case "object": return provider.FromEnv().ObjectFetch(u.Path) } req, err := http.Get(url_) if err != nil { return nil, err } return req.Body, nil }