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