forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.go
98 lines (85 loc) · 2.74 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
package application
import (
"github.com/cloudfoundry/cli/cf/api"
"github.com/cloudfoundry/cli/cf/command_metadata"
"github.com/cloudfoundry/cli/cf/configuration"
"github.com/cloudfoundry/cli/cf/flag_helpers"
. "github.com/cloudfoundry/cli/cf/i18n"
"github.com/cloudfoundry/cli/cf/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/codegangsta/cli"
)
type Files struct {
ui terminal.UI
config configuration.Reader
appFilesRepo api.AppFilesRepository
appReq requirements.ApplicationRequirement
}
func NewFiles(ui terminal.UI, config configuration.Reader, appFilesRepo api.AppFilesRepository) (cmd *Files) {
cmd = new(Files)
cmd.ui = ui
cmd.config = config
cmd.appFilesRepo = appFilesRepo
return
}
func (cmd *Files) Metadata() command_metadata.CommandMetadata {
return command_metadata.CommandMetadata{
Name: "files",
ShortName: "f",
Description: T("Print out a list of files in a directory or the contents of a specific file"),
Usage: T("CF_NAME files APP [-i INSTANCE] [PATH]"),
Flags: []cli.Flag{
flag_helpers.NewIntFlag("i", T("Instance")),
},
}
}
func (cmd *Files) GetRequirements(requirementsFactory requirements.Factory, c *cli.Context) (reqs []requirements.Requirement, err error) {
if len(c.Args()) < 1 {
cmd.ui.FailWithUsage(c)
}
cmd.appReq = requirementsFactory.NewApplicationRequirement(c.Args()[0])
reqs = []requirements.Requirement{
requirementsFactory.NewLoginRequirement(),
requirementsFactory.NewTargetedSpaceRequirement(),
cmd.appReq,
}
return
}
func (cmd *Files) Run(c *cli.Context) {
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("")
cmd.ui.Say("%s", list)
}