/
status.go
61 lines (53 loc) · 1.72 KB
/
status.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
package clipkg
import (
"encoding/json"
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/valyala/fasthttp"
"log"
"os"
)
// RespStatusData : Used to serialize server returned json data
type RespStatusData struct {
RunHealth int `json:"status"`
CaptchaEnabled bool `json:"captcha_enabled"`
MaxExpireTime int `json:"max_expire"`
FileCheckEnabled bool `json:"abuse_detection"`
}
var (
statusCmd = &cobra.Command{
Use: "status",
Short: "Fetching server running status and limitation.",
Args: cobra.NoArgs,
RunE: statusOnline,
}
)
// StatusCommand : sub-command status.
func StatusCommand() *cobra.Command {
return statusCmd
}
func statusOnline(command *cobra.Command, args []string) (err error) {
url := viper.Get("host").(string) + "/status"
code, body, err := fasthttp.Get(make([]byte, 0), url)
if err != nil {
log.Fatal(err.Error())
}
_, _ = fmt.Fprintf(os.Stderr, "Server Response:\n")
_, _ = fmt.Fprintf(os.Stderr, "Http Status Code: %d\n", code)
if code >= 400 {
log.Fatalln("Your request is rejected by server. Please contact server administrator.")
}
_, _ = fmt.Fprintf(os.Stderr, "Http Response Body:\n")
var serverStatusData RespStatusData
err = json.Unmarshal(body, &serverStatusData)
if err != nil {
log.Fatalln("Please contact server administrator.")
}
_, _ = fmt.Printf("\t Server Status: Running \n")
_, _ = fmt.Printf("\t Server Requires CAPTCHA Verification: %v \n", serverStatusData.CaptchaEnabled)
_, _ = fmt.Printf("\t Server-allowed maximum expire time: %d (hrs) \n", serverStatusData.MaxExpireTime)
_, _ = fmt.Printf("\t Server Enabled Abuse Detection Feature: %v \n", serverStatusData.FileCheckEnabled)
_, _ = fmt.Fprintf(os.Stderr, "\n")
return
}