github.com/loafoe/cli@v7.1.0+incompatible/command/v7/apply_manifest_command.go (about) 1 package v7 2 3 import ( 4 "os" 5 6 "code.cloudfoundry.org/cli/cf/errors" 7 "gopkg.in/yaml.v2" 8 9 "code.cloudfoundry.org/cli/command" 10 "code.cloudfoundry.org/cli/command/flag" 11 "code.cloudfoundry.org/cli/command/translatableerror" 12 "code.cloudfoundry.org/cli/util/manifestparser" 13 "github.com/cloudfoundry/bosh-cli/director/template" 14 ) 15 16 type ApplyManifestCommand struct { 17 BaseCommand 18 19 PathToManifest flag.ManifestPathWithExistenceCheck `short:"f" description:"Path to app manifest"` 20 Vars []template.VarKV `long:"var" description:"Variable key value pair for variable substitution, (e.g., name=app1); can specify multiple times"` 21 PathsToVarsFiles []flag.PathWithExistenceCheck `long:"vars-file" description:"Path to a variable substitution file for manifest; can specify multiple times"` 22 usage interface{} `usage:"CF_NAME apply-manifest -f APP_MANIFEST_PATH"` 23 relatedCommands interface{} `related_commands:"create-app, create-app-manifest, push"` 24 25 ManifestLocator ManifestLocator 26 ManifestParser ManifestParser 27 CWD string 28 } 29 30 func (cmd *ApplyManifestCommand) Setup(config command.Config, ui command.UI) error { 31 cmd.ManifestLocator = manifestparser.NewLocator() 32 cmd.ManifestParser = manifestparser.ManifestParser{} 33 34 currentDir, err := os.Getwd() 35 if err != nil { 36 return err 37 } 38 cmd.CWD = currentDir 39 40 return cmd.BaseCommand.Setup(config, ui) 41 } 42 43 func (cmd ApplyManifestCommand) Execute(args []string) error { 44 err := cmd.SharedActor.CheckTarget(true, true) 45 if err != nil { 46 return err 47 } 48 49 user, err := cmd.Config.CurrentUser() 50 if err != nil { 51 return err 52 } 53 54 readPath := cmd.CWD 55 if cmd.PathToManifest != "" { 56 readPath = string(cmd.PathToManifest) 57 } 58 59 pathToManifest, exists, err := cmd.ManifestLocator.Path(readPath) 60 if err != nil { 61 return err 62 } 63 64 if !exists { 65 return translatableerror.ManifestFileNotFoundInDirectoryError{PathToManifest: readPath} 66 } 67 68 cmd.UI.DisplayTextWithFlavor("Applying manifest {{.ManifestPath}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{ 69 "ManifestPath": pathToManifest, 70 "OrgName": cmd.Config.TargetedOrganization().Name, 71 "SpaceName": cmd.Config.TargetedSpace().Name, 72 "Username": user.Name, 73 }) 74 75 var pathsToVarsFiles []string 76 for _, varFilePath := range cmd.PathsToVarsFiles { 77 pathsToVarsFiles = append(pathsToVarsFiles, string(varFilePath)) 78 } 79 80 manifest, err := cmd.ManifestParser.InterpolateAndParse(pathToManifest, pathsToVarsFiles, cmd.Vars) 81 if err != nil { 82 if _, ok := err.(*yaml.TypeError); ok { 83 return errors.New("Unable to apply manifest because its format is invalid.") 84 } 85 return err 86 } 87 88 rawManifest, err := cmd.ManifestParser.MarshalManifest(manifest) 89 if err != nil { 90 return err 91 } 92 93 warnings, err := cmd.Actor.SetSpaceManifest(cmd.Config.TargetedSpace().GUID, rawManifest) 94 cmd.UI.DisplayWarnings(warnings) 95 if err != nil { 96 return err 97 } 98 99 cmd.UI.DisplayOK() 100 101 return nil 102 }