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

     1  package v2action
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/liamawhite/cli-with-i18n/api/cloudcontroller/ccv2"
     7  	log "github.com/Sirupsen/logrus"
     8  )
     9  
    10  const (
    11  	DefaultFolderPermissions      = 0755
    12  	DefaultArchiveFilePermissions = 0744
    13  	MaxResourceMatchChunkSize     = 1000
    14  )
    15  
    16  var DefaultIgnoreLines = []string{
    17  	".cfignore",
    18  	".DS_Store",
    19  	".git",
    20  	".gitignore",
    21  	".hg",
    22  	".svn",
    23  	"_darcs",
    24  	"manifest.yaml",
    25  	"manifest.yml",
    26  }
    27  
    28  type FileChangedError struct {
    29  	Filename string
    30  }
    31  
    32  func (e FileChangedError) Error() string {
    33  	return fmt.Sprint("SHA1 mismatch for:", e.Filename)
    34  }
    35  
    36  type Resource ccv2.Resource
    37  
    38  // ResourceMatch returns a set of matched resources and unmatched resources in
    39  // the order they were given in allResources.
    40  func (actor Actor) ResourceMatch(allResources []Resource) ([]Resource, []Resource, Warnings, error) {
    41  	resourcesToSend := [][]ccv2.Resource{{}}
    42  	var currentList, sendCount int
    43  	for _, resource := range allResources {
    44  		if resource.Size == 0 {
    45  			continue
    46  		}
    47  
    48  		resourcesToSend[currentList] = append(
    49  			resourcesToSend[currentList],
    50  			ccv2.Resource(resource),
    51  		)
    52  		sendCount += 1
    53  
    54  		if len(resourcesToSend[currentList]) == MaxResourceMatchChunkSize {
    55  			currentList += 1
    56  			resourcesToSend = append(resourcesToSend, []ccv2.Resource{})
    57  		}
    58  	}
    59  
    60  	log.WithFields(log.Fields{
    61  		"total_resources":    len(allResources),
    62  		"resources_to_match": sendCount,
    63  		"chunks":             len(resourcesToSend),
    64  	}).Debug("sending resource match stats")
    65  
    66  	matchedCCResources := map[string]ccv2.Resource{}
    67  	var allWarnings Warnings
    68  	for _, chunk := range resourcesToSend {
    69  		if len(chunk) == 0 {
    70  			log.Debug("chunk size 0, stopping resource match requests")
    71  			break
    72  		}
    73  
    74  		returnedResources, warnings, err := actor.CloudControllerClient.ResourceMatch(chunk)
    75  		allWarnings = append(allWarnings, warnings...)
    76  
    77  		if err != nil {
    78  			log.Errorln("during resource matching", err)
    79  			return nil, nil, allWarnings, err
    80  		}
    81  
    82  		for _, resource := range returnedResources {
    83  			matchedCCResources[resource.SHA1] = resource
    84  		}
    85  	}
    86  	log.WithField("matched_resource_count", len(matchedCCResources)).Debug("total number of matched resources")
    87  
    88  	var matchedResources, unmatchedResources []Resource
    89  	for _, resource := range allResources {
    90  		if _, ok := matchedCCResources[resource.SHA1]; ok {
    91  			matchedResources = append(matchedResources, resource)
    92  		} else {
    93  			unmatchedResources = append(unmatchedResources, resource)
    94  		}
    95  	}
    96  
    97  	return matchedResources, unmatchedResources, allWarnings, nil
    98  }
    99  
   100  func (Actor) actorToCCResources(resources []Resource) []ccv2.Resource {
   101  	apiResources := make([]ccv2.Resource, 0, len(resources)) // Explicitly done to prevent nils
   102  
   103  	for _, resource := range resources {
   104  		apiResources = append(apiResources, ccv2.Resource(resource))
   105  	}
   106  
   107  	return apiResources
   108  }