github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/actor/pushaction/merge_and_validate_settings_and_manifest.go (about)

     1  package pushaction
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"code.cloudfoundry.org/cli/util/manifest"
     8  	log "github.com/sirupsen/logrus"
     9  )
    10  
    11  type MissingNameError struct{}
    12  
    13  func (MissingNameError) Error() string {
    14  	return "name not specified for app"
    15  }
    16  
    17  type NonexistentAppPathError struct {
    18  	Path string
    19  }
    20  
    21  func (e NonexistentAppPathError) Error() string {
    22  	return fmt.Sprint("app path not found:", e.Path)
    23  }
    24  
    25  type CommandLineOptionsWithMultipleAppsError struct{}
    26  
    27  func (CommandLineOptionsWithMultipleAppsError) Error() string {
    28  	return "cannot use command line flag with multiple apps"
    29  }
    30  
    31  type AppNotFoundInManifestError struct {
    32  	Name string
    33  }
    34  
    35  func (e AppNotFoundInManifestError) Error() string {
    36  	return fmt.Sprintf("specfied app: %s not found in manifest", e.Name)
    37  }
    38  
    39  func (actor Actor) MergeAndValidateSettingsAndManifests(settings CommandLineSettings, apps []manifest.Application) ([]manifest.Application, error) {
    40  	var mergedApps []manifest.Application
    41  
    42  	if len(apps) == 0 {
    43  		log.Info("no manifest, generating one from command line settings")
    44  		mergedApps = append(mergedApps, settings.OverrideManifestSettings(manifest.Application{}))
    45  	} else {
    46  		if settings.Name != "" && len(apps) > 1 {
    47  			var err error
    48  			apps, err = actor.selectApp(settings.Name, apps)
    49  			if err != nil {
    50  				return nil, err
    51  			}
    52  		}
    53  
    54  		err := actor.validatePremergedSettings(settings, apps)
    55  		if err != nil {
    56  			return nil, err
    57  		}
    58  
    59  		for _, app := range apps {
    60  			mergedApps = append(mergedApps, settings.OverrideManifestSettings(app))
    61  		}
    62  	}
    63  
    64  	mergedApps = actor.setSaneDefaults(mergedApps)
    65  
    66  	log.Debugf("merged app settings: %#v", mergedApps)
    67  	return mergedApps, actor.validateMergedSettings(mergedApps)
    68  }
    69  
    70  func (Actor) selectApp(appName string, apps []manifest.Application) ([]manifest.Application, error) {
    71  	var returnedApps []manifest.Application
    72  	for _, app := range apps {
    73  		if app.Name == appName {
    74  			returnedApps = append(returnedApps, app)
    75  		}
    76  	}
    77  	if len(returnedApps) == 0 {
    78  		return nil, AppNotFoundInManifestError{Name: appName}
    79  	}
    80  
    81  	return returnedApps, nil
    82  }
    83  
    84  func (Actor) setSaneDefaults(apps []manifest.Application) []manifest.Application {
    85  	for i, app := range apps {
    86  		if app.HealthCheckType == "http" && app.HealthCheckHTTPEndpoint == "" {
    87  			apps[i].HealthCheckHTTPEndpoint = "/"
    88  		}
    89  	}
    90  
    91  	return apps
    92  }
    93  
    94  func (Actor) validatePremergedSettings(settings CommandLineSettings, apps []manifest.Application) error {
    95  	if len(apps) > 1 {
    96  		switch {
    97  		case
    98  			settings.Buildpack.IsSet,
    99  			settings.Command.IsSet,
   100  			settings.DiskQuota != 0,
   101  			settings.DockerImage != "",
   102  			settings.HealthCheckTimeout != 0,
   103  			settings.HealthCheckType != "",
   104  			settings.Instances.IsSet,
   105  			settings.Memory != 0,
   106  			settings.ProvidedAppPath != "",
   107  			settings.StackName != "":
   108  			log.Error("cannot use some parameters with multiple apps")
   109  			return CommandLineOptionsWithMultipleAppsError{}
   110  		}
   111  	}
   112  	return nil
   113  }
   114  
   115  func (Actor) validateMergedSettings(apps []manifest.Application) error {
   116  	for i, app := range apps {
   117  		if app.Name == "" {
   118  			log.WithField("index", i).Error("does not contain an app name")
   119  			return MissingNameError{}
   120  		}
   121  		_, err := os.Stat(app.Path)
   122  		if os.IsNotExist(err) {
   123  			log.WithField("path", app.Path).Error("app path does not exist")
   124  			return NonexistentAppPathError{Path: app.Path}
   125  		}
   126  	}
   127  	return nil
   128  }