-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
status.go
62 lines (55 loc) · 1.59 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
62
// Copyright 2020-2021 Changkun Ou. All rights reserved.
// Use of this source code is governed by a GPL-3.0
// license that can be found in the LICENSE file.
package cmd
import (
"context"
"encoding/json"
"fmt"
"net/http"
"changkun.de/x/midgard/internal/config"
"changkun.de/x/midgard/internal/term"
"changkun.de/x/midgard/internal/types"
"changkun.de/x/midgard/internal/types/proto"
"changkun.de/x/midgard/internal/utils"
"github.com/spf13/cobra"
"google.golang.org/grpc/status"
)
var statusCmd = &cobra.Command{
Use: "status",
Short: "check midgard setup status",
Long: `check midgard setup status`,
Args: cobra.ExactArgs(0),
Run: func(_ *cobra.Command, args []string) {
var s string
// check server status
res, err := utils.Request(http.MethodGet,
config.Get().Domain+"/midgard/ping", nil)
if err != nil {
s += fmt.Sprintf("server status: %s, %v\n",
term.Red("request error"), err)
} else {
var out types.PingOutput
err = json.Unmarshal(res, &out)
if err != nil {
s += fmt.Sprintf("server status: %s, details:\n%v\n",
term.Red("failed to parse ping response from server"),
err)
} else {
s += fmt.Sprintf("server status: %s\n", term.Green("OK"))
}
}
// check daemon status
utils.Connect(func(ctx context.Context, c proto.MidgardClient) {
_, err := c.Ping(ctx, &proto.PingInput{})
if err != nil {
s += fmt.Sprintf("daemon status: %s, details:\n%v\n",
term.Red("failed to ping daemon"),
status.Convert(err).Message())
} else {
s += fmt.Sprintf("daemon status: %s\n", term.Green("OK"))
}
})
fmt.Println(s)
},
}