github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/command/v7/apply_manifest_command.go (about) 1 package v7 2 3 import ( 4 "os" 5 6 "github.com/LukasHeimann/cloudfoundrycli/v8/api/cloudcontroller/ccerror" 7 "github.com/LukasHeimann/cloudfoundrycli/v8/cf/errors" 8 "github.com/LukasHeimann/cloudfoundrycli/v8/command" 9 "github.com/LukasHeimann/cloudfoundrycli/v8/command/flag" 10 "github.com/LukasHeimann/cloudfoundrycli/v8/command/translatableerror" 11 "github.com/LukasHeimann/cloudfoundrycli/v8/command/v7/shared" 12 "github.com/LukasHeimann/cloudfoundrycli/v8/util/manifestparser" 13 "github.com/cloudfoundry/bosh-cli/director/template" 14 "gopkg.in/yaml.v2" 15 ) 16 17 type ApplyManifestCommand struct { 18 BaseCommand 19 20 PathToManifest flag.ManifestPathWithExistenceCheck `short:"f" description:"Path to app manifest"` 21 Vars []template.VarKV `long:"var" description:"Variable key value pair for variable substitution, (e.g., name=app1); can specify multiple times"` 22 PathsToVarsFiles []flag.PathWithExistenceCheck `long:"vars-file" description:"Path to a variable substitution file for manifest; can specify multiple times"` 23 usage interface{} `usage:"CF_NAME apply-manifest -f APP_MANIFEST_PATH"` 24 relatedCommands interface{} `related_commands:"create-app, create-app-manifest, push"` 25 26 ManifestLocator ManifestLocator 27 ManifestParser ManifestParser 28 29 DiffDisplayer DiffDisplayer 30 CWD string 31 } 32 33 func (cmd *ApplyManifestCommand) Setup(config command.Config, ui command.UI) error { 34 cmd.ManifestLocator = manifestparser.NewLocator() 35 cmd.ManifestParser = manifestparser.ManifestParser{} 36 cmd.DiffDisplayer = shared.NewManifestDiffDisplayer(ui) 37 38 currentDir, err := os.Getwd() 39 if err != nil { 40 return err 41 } 42 cmd.CWD = currentDir 43 44 return cmd.BaseCommand.Setup(config, ui) 45 } 46 47 func (cmd ApplyManifestCommand) Execute(args []string) error { 48 err := cmd.SharedActor.CheckTarget(true, true) 49 if err != nil { 50 return err 51 } 52 53 user, err := cmd.Actor.GetCurrentUser() 54 if err != nil { 55 return err 56 } 57 58 readPath := cmd.CWD 59 if cmd.PathToManifest != "" { 60 readPath = string(cmd.PathToManifest) 61 } 62 63 pathToManifest, exists, err := cmd.ManifestLocator.Path(readPath) 64 if err != nil { 65 return err 66 } 67 68 if !exists { 69 return translatableerror.ManifestFileNotFoundInDirectoryError{PathToManifest: readPath} 70 } 71 72 cmd.UI.DisplayTextWithFlavor("Applying manifest {{.ManifestPath}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{ 73 "ManifestPath": pathToManifest, 74 "OrgName": cmd.Config.TargetedOrganization().Name, 75 "SpaceName": cmd.Config.TargetedSpace().Name, 76 "Username": user.Name, 77 }) 78 79 spaceGUID := cmd.Config.TargetedSpace().GUID 80 81 var pathsToVarsFiles []string 82 for _, varFilePath := range cmd.PathsToVarsFiles { 83 pathsToVarsFiles = append(pathsToVarsFiles, string(varFilePath)) 84 } 85 86 interpolatedManifestBytes, err := cmd.ManifestParser.InterpolateManifest(pathToManifest, pathsToVarsFiles, cmd.Vars) 87 if err != nil { 88 return err 89 } 90 91 manifest, err := cmd.ManifestParser.ParseManifest(pathToManifest, interpolatedManifestBytes) 92 if err != nil { 93 if _, ok := err.(*yaml.TypeError); ok { 94 return errors.New("Unable to apply manifest because its format is invalid.") 95 } 96 return err 97 } 98 99 manifestBytes, err := cmd.ManifestParser.MarshalManifest(manifest) 100 if err != nil { 101 return err 102 } 103 104 diff, warnings, err := cmd.Actor.DiffSpaceManifest(spaceGUID, manifestBytes) 105 cmd.UI.DisplayWarnings(warnings) 106 if err != nil { 107 if _, isUnexpectedError := err.(ccerror.V3UnexpectedResponseError); isUnexpectedError { 108 cmd.UI.DisplayWarning("Unable to generate diff. Continuing to apply manifest...") 109 } else { 110 return err 111 } 112 } else { 113 cmd.UI.DisplayNewline() 114 cmd.UI.DisplayText("Updating with these attributes...") 115 116 err = cmd.DiffDisplayer.DisplayDiff(manifestBytes, diff) 117 if err != nil { 118 return err 119 } 120 } 121 122 warnings, err = cmd.Actor.SetSpaceManifest(spaceGUID, manifestBytes) 123 cmd.UI.DisplayWarnings(warnings) 124 if err != nil { 125 return err 126 } 127 128 cmd.UI.DisplayNewline() 129 cmd.UI.DisplayOK() 130 131 return nil 132 }