-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_files_command.go
68 lines (55 loc) · 1.4 KB
/
list_files_command.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
package main
import (
"encoding/json"
"fmt"
log "github.com/Sirupsen/logrus"
"strings"
)
type ListFilesCommand struct {
ServerURL string
}
func NewListFilesCommand(config *Config) ListFilesCommand {
return ListFilesCommand{
ServerURL: fmt.Sprintf("http://%s:%s", config.Host, config.Port),
}
}
type ListFilesResult struct {
Files []string `json:"files"`
}
func (listFilesCommand ListFilesCommand) Do() string {
subURL := "v1/files"
if !strings.HasSuffix(listFilesCommand.ServerURL, "/") {
subURL = "/" + subURL
}
response, err := doRequest("GET", listFilesCommand.ServerURL, subURL)
if err != nil {
log.WithFields(log.Fields{
"serverURL": listFilesCommand.ServerURL,
"subURL": subURL,
"error": err.Error(),
}).Error("List files do request error")
return "Error command execution"
}
result := new(ListFilesResult)
if response != nil && response.Body != nil {
defer response.Body.Close()
if err := json.NewDecoder(response.Body).Decode(result); err != nil {
log.WithField("error", err.Error()).Error("Encode body error")
return "Error command execution"
}
prettyList := ""
for index, fileName := range result.Files {
prettyList += fileName
if index != len(result.Files)-1 {
prettyList += "\n"
}
}
return prettyList
} else {
log.Error("Response is nil")
}
return ""
}
func (listFilesCommand ListFilesCommand) isExitCommand() bool {
return false
}