github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/actor/pushaction/resource.go (about)

     1  package pushaction
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/liamawhite/cli-with-i18n/actor/sharedaction"
     7  	"github.com/liamawhite/cli-with-i18n/actor/v2action"
     8  
     9  	log "github.com/Sirupsen/logrus"
    10  )
    11  
    12  func (actor Actor) CreateArchive(config ApplicationConfig) (string, error) {
    13  	log.Info("creating archive")
    14  
    15  	var archivePath string
    16  	var err error
    17  
    18  	//change to look at unmatched
    19  	if config.Archive {
    20  		archivePath, err = actor.SharedActor.ZipArchiveResources(config.Path, actor.ConvertV2ResourcesToSharedResources(config.UnmatchedResources))
    21  	} else {
    22  		archivePath, err = actor.SharedActor.ZipDirectoryResources(config.Path, actor.ConvertV2ResourcesToSharedResources(config.UnmatchedResources))
    23  	}
    24  	if err != nil {
    25  		log.WithField("path", config.Path).Errorln("archiving resources:", err)
    26  		return "", err
    27  	}
    28  	log.WithField("archivePath", archivePath).Debug("archive created")
    29  	return archivePath, nil
    30  }
    31  
    32  func (actor Actor) ConvertSharedResourcesToV2Resources(resources []sharedaction.Resource) []v2action.Resource {
    33  	newResources := make([]v2action.Resource, 0, len(resources)) // Explicitly done to prevent nils
    34  
    35  	for _, resource := range resources {
    36  		newResources = append(newResources, v2action.Resource(resource))
    37  	}
    38  	return newResources
    39  }
    40  
    41  func (actor Actor) ConvertV2ResourcesToSharedResources(resources []v2action.Resource) []sharedaction.Resource {
    42  	newResources := make([]sharedaction.Resource, 0, len(resources)) // Explicitly done to prevent nils
    43  
    44  	for _, resource := range resources {
    45  		newResources = append(newResources, sharedaction.Resource(resource))
    46  	}
    47  	return newResources
    48  }
    49  
    50  func (actor Actor) SetMatchedResources(config ApplicationConfig) (ApplicationConfig, Warnings) {
    51  	matched, unmatched, warnings, err := actor.V2Actor.ResourceMatch(config.AllResources)
    52  
    53  	if err != nil {
    54  		log.Error("uploading all resources instead of resource matching")
    55  		config.UnmatchedResources = config.AllResources
    56  		return config, Warnings(warnings)
    57  	}
    58  
    59  	config.MatchedResources = matched
    60  	config.UnmatchedResources = unmatched
    61  
    62  	return config, Warnings(warnings)
    63  }
    64  
    65  func (actor Actor) UploadPackage(config ApplicationConfig, archivePath string, progressbar ProgressBar, eventStream chan<- Event) (Warnings, error) {
    66  	log.Info("uploading archive")
    67  	archive, err := os.Open(archivePath)
    68  	if err != nil {
    69  		log.WithField("archivePath", archivePath).Errorln("opening temp archive:", err)
    70  		return nil, err
    71  	}
    72  	defer archive.Close()
    73  
    74  	archiveInfo, err := archive.Stat()
    75  	if err != nil {
    76  		log.WithField("archivePath", archivePath).Errorln("stat temp archive:", err)
    77  		return nil, err
    78  	}
    79  
    80  	log.WithFields(log.Fields{
    81  		"appGUID":     config.DesiredApplication.GUID,
    82  		"archiveSize": archiveInfo.Size(),
    83  	}).Debug("uploading app bits")
    84  
    85  	eventStream <- UploadingApplication
    86  	reader := progressbar.NewProgressBarWrapper(archive, archiveInfo.Size())
    87  
    88  	var allWarnings Warnings
    89  	// change to look at matched resoruces
    90  	job, warnings, err := actor.V2Actor.UploadApplicationPackage(config.DesiredApplication.GUID, config.MatchedResources, reader, archiveInfo.Size())
    91  	allWarnings = append(allWarnings, Warnings(warnings)...)
    92  
    93  	if err != nil {
    94  		log.WithField("archivePath", archivePath).Errorln("streaming archive:", err)
    95  		return allWarnings, err
    96  	}
    97  	eventStream <- UploadComplete
    98  	warnings, err = actor.V2Actor.PollJob(job)
    99  	allWarnings = append(allWarnings, Warnings(warnings)...)
   100  
   101  	return allWarnings, err
   102  }