Beispiel #1
0
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
}
Beispiel #2
0
// Provider returns the appropriate provider interface based on the env
func Provider() provider.Provider {
	if os.Getenv("PROVIDER") == "test" {
		return TestProvider
	}

	return provider.FromEnv()
}
Beispiel #3
0
func init() {
	currentProvider = provider.FromEnv()

	var buf bytes.Buffer

	currentProvider.Initialize(structs.ProviderOptions{
		LogOutput: &buf,
	})
}
Beispiel #4
0
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
}