forked from a2call/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contract.go
106 lines (99 loc) · 5.02 KB
/
contract.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
package files
import (
"github.com/Sirupsen/logrus"
"github.com/daticahealth/cli/commands/services"
"github.com/daticahealth/cli/config"
"github.com/daticahealth/cli/lib/auth"
"github.com/daticahealth/cli/lib/prompts"
"github.com/daticahealth/cli/models"
"github.com/jault3/mow.cli"
)
// Cmd is the contract between the user and the CLI. This specifies the command
// name, arguments, and required/optional arguments and flags for the command.
var Cmd = models.Command{
Name: "files",
ShortHelp: "Tasks for managing service files",
LongHelp: "The <code>files</code> command gives access to service files on your environment's services. " +
"Service files can include Nginx configs, SSL certificates, and any other file that might be injected into your running service. " +
"The files command can not be run directly but has subcommands.",
CmdFunc: func(settings *models.Settings) func(cmd *cli.Cmd) {
return func(cmd *cli.Cmd) {
cmd.CommandLong(DownloadSubCmd.Name, DownloadSubCmd.ShortHelp, DownloadSubCmd.LongHelp, DownloadSubCmd.CmdFunc(settings))
cmd.CommandLong(ListSubCmd.Name, ListSubCmd.ShortHelp, ListSubCmd.LongHelp, ListSubCmd.CmdFunc(settings))
}
},
}
var DownloadSubCmd = models.Command{
Name: "download",
ShortHelp: "Download a file to your localhost with the same file permissions as on the remote host or print it to stdout",
LongHelp: "<code>files download</code> allows you to view the contents of a service file and save it to your local machine. " +
"Most service files are stored on your service_proxy and therefore you should not have to specify the <code>SERVICE_NAME</code> argument. " +
"Simply supply the <code>FILE_NAME</code> found from the files list command and the contents of the file, as well as the permissions string, will be printed to your console. " +
"You can always store the file locally, applying the same permissions as those on the remote server, by specifying an output file with the <code>-o</code> flag. Here is a sample command\n\n" +
"<pre>\ndatica -E \"<your_env_name>\" files download /etc/nginx/sites-enabled/mywebsite.com\n</pre>",
CmdFunc: func(settings *models.Settings) func(cmd *cli.Cmd) {
return func(subCmd *cli.Cmd) {
serviceName := subCmd.StringArg("SERVICE_NAME", "service_proxy", "The name of the service to download a file from")
fileName := subCmd.StringArg("FILE_NAME", "", "The name of the service file from running \"datica files list\"")
output := subCmd.StringOpt("o output", "", "The downloaded file will be saved to the given location with the same file permissions as it has on the remote host. If those file permissions cannot be applied, a warning will be printed and default 0644 permissions applied. If no output is specified, stdout is used.")
force := subCmd.BoolOpt("f force", false, "If the specified output file already exists, automatically overwrite it")
subCmd.Action = func() {
if _, err := auth.New(settings, prompts.New()).Signin(); err != nil {
logrus.Fatal(err.Error())
}
if err := config.CheckRequiredAssociation(settings); err != nil {
logrus.Fatal(err.Error())
}
err := CmdDownload(*serviceName, *fileName, *output, *force, New(settings), services.New(settings))
if err != nil {
logrus.Fatal(err.Error())
}
}
subCmd.Spec = "[SERVICE_NAME] FILE_NAME [-o] [-f]"
}
},
}
var ListSubCmd = models.Command{
Name: "list",
ShortHelp: "List all files available for a given service",
LongHelp: "<code>files list</code> prints out a listing of all service files available for download. " +
"Nearly all service files are stored on the service_proxy and therefore you should not have to specify the <code>SERVICE_NAME</code> argument. " +
"Here is a sample command\n\n" +
"<pre>\ndatica -E \"<your_env_name>\" files list\n</pre>",
CmdFunc: func(settings *models.Settings) func(cmd *cli.Cmd) {
return func(subCmd *cli.Cmd) {
svcName := subCmd.StringArg("SERVICE_NAME", "service_proxy", "The name of the service to list files for")
showTimestamps := subCmd.BoolOpt("t showTimestamps", false, "Show when each file was created and most recently updated")
subCmd.Action = func() {
if _, err := auth.New(settings, prompts.New()).Signin(); err != nil {
logrus.Fatal(err.Error())
}
if err := config.CheckRequiredAssociation(settings); err != nil {
logrus.Fatal(err.Error())
}
err := CmdList(*svcName, *showTimestamps, New(settings), services.New(settings))
if err != nil {
logrus.Fatal(err.Error())
}
}
subCmd.Spec = "[SERVICE_NAME] [-t]"
}
},
}
// IFiles
type IFiles interface {
Create(svcID, filePath, name, mode string) (*models.ServiceFile, error)
List(svcID string) (*[]models.ServiceFile, error)
Retrieve(fileName string, svcID string) (*models.ServiceFile, error)
Save(output string, force bool, file *models.ServiceFile) error
}
// SFiles is a concrete implementation of IFiles
type SFiles struct {
Settings *models.Settings
}
// New generates a new instance of IFiles
func New(settings *models.Settings) IFiles {
return &SFiles{
Settings: settings,
}
}