-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_ps.go
53 lines (44 loc) · 1.22 KB
/
client_ps.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
package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/devcows/share/api"
"github.com/devcows/share/lib"
"github.com/ryanuber/columnize"
"github.com/spf13/cobra"
)
func runPsCmd(settings lib.SettingsShare) {
serverEndPoint := fmt.Sprintf("http://%s:%v/ps", settings.ShareDaemon.Host, settings.ShareDaemon.Port)
resp, _ := http.Get(serverEndPoint)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
res := api.PsResponse{}
json.Unmarshal([]byte(body), &res)
if res.Status {
lines := []string{
"UUID | Source | Flags | CreatedAt",
}
for i := 0; i < len(res.Servers); i++ {
server := res.Servers[i]
line := fmt.Sprintf("%v|%s|%v|%v", server.UUID, server.Source, server.Flags, server.CreatedAt)
lines = append(lines, line)
}
result := columnize.SimpleFormat(lines)
fmt.Println(result)
} else {
fmt.Printf("%s\n", res.ErrorMessage)
}
}
var PsCmd = &cobra.Command{
Use: "ps",
Short: "List files or folders from server",
Long: `List files or folders from server`,
Run: func(cmd *cobra.Command, args []string) {
if err := lib.InitSettings(lib.ConfigFile(), &appSettings); err != nil {
fmt.Printf("Error: %s\n", err.Error())
}
runPsCmd(appSettings)
},
}