github.com/niteshexa/cloudfoundry_cli@v7.1.0+incompatible/command/v7/download_droplet_command.go (about)

     1  package v7
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"code.cloudfoundry.org/cli/actor/actionerror"
    10  	"code.cloudfoundry.org/cli/actor/v7action"
    11  	"code.cloudfoundry.org/cli/command/flag"
    12  	"code.cloudfoundry.org/cli/command/translatableerror"
    13  )
    14  
    15  type DownloadDropletCommand struct {
    16  	BaseCommand
    17  
    18  	RequiredArgs    flag.AppName `positional-args:"yes"`
    19  	Droplet         string       `long:"droplet" description:"The guid of the droplet to download (default: app's current droplet)."`
    20  	Path            string       `long:"path" short:"p" description:"File path to download droplet to (default: current working directory)."`
    21  	usage           interface{}  `usage:"CF_NAME download-droplet APP_NAME [--droplet DROPLET_GUID] [--path /path/to/droplet.tgz]"`
    22  	relatedCommands interface{}  `related_commands:"apps, droplets, push, set-droplet"`
    23  }
    24  
    25  func (cmd DownloadDropletCommand) Execute(args []string) error {
    26  	err := cmd.SharedActor.CheckTarget(true, true)
    27  	if err != nil {
    28  		return err
    29  	}
    30  
    31  	user, err := cmd.Config.CurrentUser()
    32  	if err != nil {
    33  		return err
    34  	}
    35  
    36  	var (
    37  		rawDropletBytes []byte
    38  		dropletGUID     string
    39  		warnings        v7action.Warnings
    40  	)
    41  
    42  	if cmd.Droplet != "" {
    43  		dropletGUID = cmd.Droplet
    44  
    45  		cmd.UI.DisplayTextWithFlavor("Downloading droplet {{.DropletGUID}} for app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{
    46  			"DropletGUID": dropletGUID,
    47  			"AppName":     cmd.RequiredArgs.AppName,
    48  			"OrgName":     cmd.Config.TargetedOrganization().Name,
    49  			"SpaceName":   cmd.Config.TargetedSpace().Name,
    50  			"Username":    user.Name,
    51  		})
    52  
    53  		rawDropletBytes, warnings, err = cmd.Actor.DownloadDropletByGUIDAndAppName(dropletGUID, cmd.RequiredArgs.AppName, cmd.Config.TargetedSpace().GUID)
    54  	} else {
    55  		cmd.UI.DisplayTextWithFlavor("Downloading current droplet for app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{
    56  			"AppName":   cmd.RequiredArgs.AppName,
    57  			"OrgName":   cmd.Config.TargetedOrganization().Name,
    58  			"SpaceName": cmd.Config.TargetedSpace().Name,
    59  			"Username":  user.Name,
    60  		})
    61  
    62  		rawDropletBytes, dropletGUID, warnings, err = cmd.Actor.DownloadCurrentDropletByAppName(cmd.RequiredArgs.AppName, cmd.Config.TargetedSpace().GUID)
    63  	}
    64  
    65  	cmd.UI.DisplayWarnings(warnings)
    66  
    67  	if err != nil {
    68  		if _, ok := err.(actionerror.DropletNotFoundError); ok {
    69  			return translatableerror.NoDropletForAppError{AppName: cmd.RequiredArgs.AppName, DropletGUID: cmd.Droplet}
    70  		}
    71  		return err
    72  	}
    73  
    74  	var pathToDroplet string
    75  
    76  	if cmd.Path == "" {
    77  		currentDir, err := os.Getwd()
    78  		if err != nil {
    79  			return err
    80  		}
    81  
    82  		pathToDroplet = filepath.Join(currentDir, fmt.Sprintf("droplet_%s.tgz", dropletGUID))
    83  	} else {
    84  		stats, err := os.Stat(cmd.Path)
    85  
    86  		if err == nil && stats.IsDir() {
    87  			pathToDroplet = filepath.Join(cmd.Path, fmt.Sprintf("droplet_%s.tgz", dropletGUID))
    88  		} else {
    89  			pathToDroplet = cmd.Path
    90  		}
    91  	}
    92  
    93  	err = ioutil.WriteFile(pathToDroplet, rawDropletBytes, 0666)
    94  	if err != nil {
    95  		return translatableerror.DropletFileError{Err: err}
    96  	}
    97  
    98  	cmd.UI.DisplayText("Droplet downloaded successfully at {{.FilePath}}", map[string]interface{}{
    99  		"FilePath": pathToDroplet,
   100  	})
   101  	cmd.UI.DisplayOK()
   102  
   103  	return nil
   104  }