github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/cf/commands/application/files.go (about)

     1  package application
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"code.cloudfoundry.org/cli/cf/api/appfiles"
     8  	"code.cloudfoundry.org/cli/cf/commandregistry"
     9  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    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 Files struct {
    17  	ui           terminal.UI
    18  	config       coreconfig.Reader
    19  	appFilesRepo appfiles.Repository
    20  	appReq       requirements.DEAApplicationRequirement
    21  }
    22  
    23  func init() {
    24  	commandregistry.Register(&Files{})
    25  }
    26  
    27  func (cmd *Files) MetaData() commandregistry.CommandMetadata {
    28  	fs := make(map[string]flags.FlagSet)
    29  	fs["i"] = &flags.IntFlag{ShortName: "i", Usage: T("Instance")}
    30  
    31  	return commandregistry.CommandMetadata{
    32  		Name:        "files",
    33  		ShortName:   "f",
    34  		Description: T("Print out a list of files in a directory or the contents of a specific file of an app running on the DEA backend"),
    35  		Usage: []string{
    36  			T(`CF_NAME files APP_NAME [PATH] [-i INSTANCE]
    37  			
    38  TIP:
    39    To list and inspect files of an app running on the Diego backend, use 'CF_NAME ssh'`),
    40  		},
    41  		Flags: fs,
    42  	}
    43  }
    44  
    45  func (cmd *Files) Requirements(requirementsFactory requirements.Factory, c flags.FlagContext) ([]requirements.Requirement, error) {
    46  	if len(c.Args()) != 1 && len(c.Args()) != 2 {
    47  		cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("files"))
    48  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(c.Args()), 1)
    49  	}
    50  
    51  	cmd.appReq = requirementsFactory.NewDEAApplicationRequirement(c.Args()[0])
    52  
    53  	reqs := []requirements.Requirement{
    54  		requirementsFactory.NewLoginRequirement(),
    55  		requirementsFactory.NewTargetedSpaceRequirement(),
    56  		cmd.appReq,
    57  	}
    58  
    59  	return reqs, nil
    60  }
    61  
    62  func (cmd *Files) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    63  	cmd.ui = deps.UI
    64  	cmd.config = deps.Config
    65  	cmd.appFilesRepo = deps.RepoLocator.GetAppFilesRepository()
    66  	return cmd
    67  }
    68  
    69  func (cmd *Files) Execute(c flags.FlagContext) error {
    70  	app := cmd.appReq.GetApplication()
    71  
    72  	var instance int
    73  	if c.IsSet("i") {
    74  		instance = c.Int("i")
    75  		if instance < 0 {
    76  			return errors.New(T("Invalid instance: {{.Instance}}\nInstance must be a positive integer",
    77  				map[string]interface{}{
    78  					"Instance": instance,
    79  				}))
    80  		}
    81  		if instance >= app.InstanceCount {
    82  			return errors.New(T("Invalid instance: {{.Instance}}\nInstance must be less than {{.InstanceCount}}",
    83  				map[string]interface{}{
    84  					"Instance":      instance,
    85  					"InstanceCount": app.InstanceCount,
    86  				}))
    87  		}
    88  	}
    89  
    90  	cmd.ui.Say(T("Getting files for app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...",
    91  		map[string]interface{}{
    92  			"AppName":   terminal.EntityNameColor(app.Name),
    93  			"OrgName":   terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
    94  			"SpaceName": terminal.EntityNameColor(cmd.config.SpaceFields().Name),
    95  			"Username":  terminal.EntityNameColor(cmd.config.Username())}))
    96  
    97  	path := "/"
    98  	if len(c.Args()) > 1 {
    99  		path = c.Args()[1]
   100  	}
   101  
   102  	list, err := cmd.appFilesRepo.ListFiles(app.GUID, instance, path)
   103  	if err != nil {
   104  		return err
   105  	}
   106  
   107  	cmd.ui.Ok()
   108  	cmd.ui.Say("")
   109  
   110  	if list == "" {
   111  		cmd.ui.Say("Empty file or folder")
   112  	} else {
   113  		cmd.ui.Say("%s", list)
   114  	}
   115  	return nil
   116  }