github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/buildpack/update_buildpack.go (about)

     1  package buildpack
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"os"
     7  
     8  	"code.cloudfoundry.org/cli/cf/api"
     9  	"code.cloudfoundry.org/cli/cf/commandregistry"
    10  	"code.cloudfoundry.org/cli/cf/flags"
    11  	. "code.cloudfoundry.org/cli/cf/i18n"
    12  	"code.cloudfoundry.org/cli/cf/requirements"
    13  	"code.cloudfoundry.org/cli/cf/terminal"
    14  )
    15  
    16  type UpdateBuildpack struct {
    17  	ui                terminal.UI
    18  	buildpackRepo     api.BuildpackRepository
    19  	buildpackBitsRepo api.BuildpackBitsRepository
    20  	buildpackReq      requirements.BuildpackRequirement
    21  }
    22  
    23  func init() {
    24  	commandregistry.Register(&UpdateBuildpack{})
    25  }
    26  
    27  func (cmd *UpdateBuildpack) MetaData() commandregistry.CommandMetadata {
    28  	fs := make(map[string]flags.FlagSet)
    29  	fs["i"] = &flags.IntFlag{ShortName: "i", Usage: T("The order in which the buildpacks are checked during buildpack auto-detection")}
    30  	fs["p"] = &flags.StringFlag{ShortName: "p", Usage: T("Path to directory or zip file")}
    31  	fs["enable"] = &flags.BoolFlag{Name: "enable", Usage: T("Enable the buildpack to be used for staging")}
    32  	fs["disable"] = &flags.BoolFlag{Name: "disable", Usage: T("Disable the buildpack from being used for staging")}
    33  	fs["lock"] = &flags.BoolFlag{Name: "lock", Usage: T("Lock the buildpack to prevent updates")}
    34  	fs["unlock"] = &flags.BoolFlag{Name: "unlock", Usage: T("Unlock the buildpack to enable updates")}
    35  
    36  	return commandregistry.CommandMetadata{
    37  		Name:        "update-buildpack",
    38  		Description: T("Update a buildpack"),
    39  		Usage: []string{
    40  			T("CF_NAME update-buildpack BUILDPACK [-p PATH] [-i POSITION] [--enable|--disable] [--lock|--unlock]"),
    41  			T("\n\nTIP:\n"),
    42  			T("   Path should be a zip file, a url to a zip file, or a local directory. Position is a positive integer, sets priority, and is sorted from lowest to highest."),
    43  		},
    44  		Flags: fs,
    45  	}
    46  }
    47  
    48  func (cmd *UpdateBuildpack) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    49  	if len(fc.Args()) != 1 {
    50  		cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("update-buildpack"))
    51  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1)
    52  	}
    53  
    54  	loginReq := requirementsFactory.NewLoginRequirement()
    55  	cmd.buildpackReq = requirementsFactory.NewBuildpackRequirement(fc.Args()[0])
    56  
    57  	reqs := []requirements.Requirement{
    58  		loginReq,
    59  		cmd.buildpackReq,
    60  	}
    61  
    62  	return reqs, nil
    63  }
    64  
    65  func (cmd *UpdateBuildpack) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    66  	cmd.ui = deps.UI
    67  	cmd.buildpackRepo = deps.RepoLocator.GetBuildpackRepository()
    68  	cmd.buildpackBitsRepo = deps.RepoLocator.GetBuildpackBitsRepository()
    69  	return cmd
    70  }
    71  
    72  func (cmd *UpdateBuildpack) Execute(c flags.FlagContext) error {
    73  	buildpack := cmd.buildpackReq.GetBuildpack()
    74  
    75  	cmd.ui.Say(T("Updating buildpack {{.BuildpackName}}...", map[string]interface{}{"BuildpackName": terminal.EntityNameColor(buildpack.Name)}))
    76  
    77  	updateBuildpack := false
    78  
    79  	if c.IsSet("i") {
    80  		position := c.Int("i")
    81  
    82  		buildpack.Position = &position
    83  		updateBuildpack = true
    84  	}
    85  
    86  	enabled := c.Bool("enable")
    87  	disabled := c.Bool("disable")
    88  	if enabled && disabled {
    89  		return errors.New(T("Cannot specify both {{.Enabled}} and {{.Disabled}}.", map[string]interface{}{
    90  			"Enabled":  "enabled",
    91  			"Disabled": "disabled",
    92  		}))
    93  	}
    94  
    95  	if enabled {
    96  		buildpack.Enabled = &enabled
    97  		updateBuildpack = true
    98  	}
    99  	if disabled {
   100  		disabled = false
   101  		buildpack.Enabled = &disabled
   102  		updateBuildpack = true
   103  	}
   104  
   105  	lock := c.Bool("lock")
   106  	unlock := c.Bool("unlock")
   107  	if lock && unlock {
   108  		return errors.New(T("Cannot specify both lock and unlock options."))
   109  	}
   110  
   111  	path := c.String("p")
   112  
   113  	if path != "" && (lock || unlock) {
   114  		return errors.New(T("Cannot specify buildpack bits and lock/unlock."))
   115  	}
   116  
   117  	if lock {
   118  		buildpack.Locked = &lock
   119  		updateBuildpack = true
   120  	}
   121  	if unlock {
   122  		unlock = false
   123  		buildpack.Locked = &unlock
   124  		updateBuildpack = true
   125  	}
   126  	var (
   127  		buildpackFile     *os.File
   128  		buildpackFileName string
   129  		err               error
   130  	)
   131  	if path != "" {
   132  		buildpackFile, buildpackFileName, err = cmd.buildpackBitsRepo.CreateBuildpackZipFile(path)
   133  		if err != nil {
   134  			cmd.ui.Warn(T("Failed to create a local temporary zip file for the buildpack"))
   135  			return err
   136  		}
   137  	}
   138  
   139  	if updateBuildpack {
   140  		newBuildpack, err := cmd.buildpackRepo.Update(buildpack)
   141  		if err != nil {
   142  			return errors.New(T("Error updating buildpack {{.Name}}\n{{.Error}}", map[string]interface{}{
   143  				"Name":  terminal.EntityNameColor(buildpack.Name),
   144  				"Error": err.Error(),
   145  			}))
   146  		}
   147  		buildpack = newBuildpack
   148  	}
   149  
   150  	if path != "" {
   151  		err := cmd.buildpackBitsRepo.UploadBuildpack(buildpack, buildpackFile, buildpackFileName)
   152  		if err != nil {
   153  			return errors.New(T("Error uploading buildpack {{.Name}}\n{{.Error}}", map[string]interface{}{
   154  				"Name":  terminal.EntityNameColor(buildpack.Name),
   155  				"Error": err.Error(),
   156  			}))
   157  		}
   158  	}
   159  	cmd.ui.Ok()
   160  	return nil
   161  }