github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/resource/context/internal/download.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package internal
     5  
     6  // TODO(ericsnow) Move this file elsewhere?
     7  //  (e.g. top-level resource pkg, charm/resource)
     8  
     9  import (
    10  	"github.com/juju/errors"
    11  )
    12  
    13  // Download downloads the resource from the provied source to the target.
    14  func Download(target DownloadTarget, remote ContentSource) error {
    15  	resDir, err := target.Initialize()
    16  	if err != nil {
    17  		return errors.Trace(err)
    18  	}
    19  
    20  	if err := resDir.Write(remote); err != nil {
    21  		return errors.Trace(err)
    22  	}
    23  
    24  	return nil
    25  }
    26  
    27  // DownloadTarget exposes the functionality of a directory spec
    28  // needed by Download().
    29  type DownloadTarget interface {
    30  	// Initialize prepares the target directory and returns it.
    31  	Initialize() (DownloadDirectory, error)
    32  }
    33  
    34  // DownloadDirectory exposes the functionality of a resource directory
    35  // needed by Download().
    36  type DownloadDirectory interface {
    37  	Resolver
    38  
    39  	// Write writes all the relevant files for the provided source
    40  	// to the directory.
    41  	Write(ContentSource) error
    42  }
    43  
    44  // Resolver exposes the functionality of DirectorySpec needed
    45  // by DownloadIndirect.
    46  type Resolver interface {
    47  	// Resolve returns the fully resolved path for the provided path items.
    48  	Resolve(...string) string
    49  }