feat: add extra json format
This commit is contained in:
parent
ef75f275b0
commit
b0692a2b0f
@ -31,6 +31,7 @@ var (
|
|||||||
listHost string
|
listHost string
|
||||||
listWide bool
|
listWide bool
|
||||||
listIds []string
|
listIds []string
|
||||||
|
listJson bool
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -38,6 +39,7 @@ func init() {
|
|||||||
add1Flags := func(f *pflag.FlagSet) {
|
add1Flags := func(f *pflag.FlagSet) {
|
||||||
f.StringVar(&listHost, "host", "127.0.0.1:7777", "specify the host of the goc server")
|
f.StringVar(&listHost, "host", "127.0.0.1:7777", "specify the host of the goc server")
|
||||||
f.BoolVar(&listWide, "wide", false, "list all services with more information (such as pid)")
|
f.BoolVar(&listWide, "wide", false, "list all services with more information (such as pid)")
|
||||||
|
f.BoolVar(&listJson, "json", false, "list all services info as json format")
|
||||||
f.StringSliceVar(&listIds, "id", nil, "specify the ids of the services")
|
f.StringSliceVar(&listIds, "id", nil, "specify the ids of the services")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,7 +52,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func list(cmd *cobra.Command, args []string) {
|
func list(cmd *cobra.Command, args []string) {
|
||||||
client.ListAgents(listHost, listIds, listWide)
|
client.ListAgents(listHost, listIds, listWide, listJson)
|
||||||
}
|
}
|
||||||
|
|
||||||
var getServiceCmd = &cobra.Command{
|
var getServiceCmd = &cobra.Command{
|
||||||
@ -59,7 +61,7 @@ var getServiceCmd = &cobra.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getAgents(cmd *cobra.Command, args []string) {
|
func getAgents(cmd *cobra.Command, args []string) {
|
||||||
client.ListAgents(listHost, listIds, listWide)
|
client.ListAgents(listHost, listIds, listWide, listJson)
|
||||||
}
|
}
|
||||||
|
|
||||||
var deleteServiceCmd = &cobra.Command{
|
var deleteServiceCmd = &cobra.Command{
|
||||||
|
@ -14,6 +14,8 @@
|
|||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/RickLeee/goc-v2/pkg/client/rest"
|
"github.com/RickLeee/goc-v2/pkg/client/rest"
|
||||||
@ -27,7 +29,7 @@ const (
|
|||||||
WATCHCONNECT = 1 << iota
|
WATCHCONNECT = 1 << iota
|
||||||
)
|
)
|
||||||
|
|
||||||
func ListAgents(host string, ids []string, wide bool) {
|
func ListAgents(host string, ids []string, wide, isJson bool) {
|
||||||
gocClient := rest.NewV2Client(host)
|
gocClient := rest.NewV2Client(host)
|
||||||
|
|
||||||
agents, err := gocClient.Agent().Get(ids)
|
agents, err := gocClient.Agent().Get(ids)
|
||||||
@ -35,8 +37,11 @@ func ListAgents(host string, ids []string, wide bool) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("cannot get agent list from goc server: %v", err)
|
log.Fatalf("cannot get agent list from goc server: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
table := tablewriter.NewWriter(os.Stdout)
|
table := tablewriter.NewWriter(os.Stdout)
|
||||||
|
if isJson {
|
||||||
|
goto asJson
|
||||||
|
}
|
||||||
|
|
||||||
table.SetCenterSeparator("")
|
table.SetCenterSeparator("")
|
||||||
table.SetColumnSeparator("")
|
table.SetColumnSeparator("")
|
||||||
table.SetRowSeparator("")
|
table.SetRowSeparator("")
|
||||||
@ -54,6 +59,7 @@ func ListAgents(host string, ids []string, wide bool) {
|
|||||||
table.SetHeader([]string{"ID", "STATUS", "REMOTEIP", "CMD"})
|
table.SetHeader([]string{"ID", "STATUS", "REMOTEIP", "CMD"})
|
||||||
table.SetColumnAlignment([]int{tablewriter.ALIGN_LEFT, tablewriter.ALIGN_LEFT, tablewriter.ALIGN_LEFT, tablewriter.ALIGN_LEFT})
|
table.SetColumnAlignment([]int{tablewriter.ALIGN_LEFT, tablewriter.ALIGN_LEFT, tablewriter.ALIGN_LEFT, tablewriter.ALIGN_LEFT})
|
||||||
}
|
}
|
||||||
|
asJson:
|
||||||
for _, agent := range agents {
|
for _, agent := range agents {
|
||||||
var status string
|
var status string
|
||||||
if agent.Status == DISCONNECT {
|
if agent.Status == DISCONNECT {
|
||||||
@ -61,14 +67,21 @@ func ListAgents(host string, ids []string, wide bool) {
|
|||||||
} else if agent.Status&(RPCCONNECT|WATCHCONNECT) > 0 {
|
} else if agent.Status&(RPCCONNECT|WATCHCONNECT) > 0 {
|
||||||
status = "CONNECT"
|
status = "CONNECT"
|
||||||
}
|
}
|
||||||
if wide {
|
agent.StatusStr = status
|
||||||
table.Append([]string{agent.Id, status, agent.RemoteIP, agent.Hostname, agent.Pid, agent.CmdLine, agent.Extra})
|
if !isJson {
|
||||||
} else {
|
if wide {
|
||||||
preLen := len(agent.Id) + len(agent.RemoteIP) + 9
|
table.Append([]string{agent.Id, status, agent.RemoteIP, agent.Hostname, agent.Pid, agent.CmdLine, agent.Extra})
|
||||||
table.Append([]string{agent.Id, status, agent.RemoteIP, getSimpleCmdLine(preLen, agent.CmdLine)})
|
} else {
|
||||||
|
preLen := len(agent.Id) + len(agent.RemoteIP) + 9
|
||||||
|
table.Append([]string{agent.Id, status, agent.RemoteIP, getSimpleCmdLine(preLen, agent.CmdLine)})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
table.Render()
|
if !isJson {
|
||||||
|
table.Render()
|
||||||
|
}
|
||||||
|
b, _ := json.Marshal(agents)
|
||||||
|
fmt.Fprint(os.Stdout, string(b))
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteAgents(host string, ids []string) {
|
func DeleteAgents(host string, ids []string) {
|
||||||
|
@ -79,6 +79,7 @@ func NewWorker(host string) Action {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ListAgents Deprecated
|
||||||
func (c *client) ListAgents(wide bool) {
|
func (c *client) ListAgents(wide bool) {
|
||||||
u := fmt.Sprintf("%s%s", c.Host, CoverAgentsListAPI)
|
u := fmt.Sprintf("%s%s", c.Host, CoverAgentsListAPI)
|
||||||
_, body, err := c.do("GET", u, "", nil)
|
_, body, err := c.do("GET", u, "", nil)
|
||||||
|
@ -21,13 +21,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Agent struct {
|
type Agent struct {
|
||||||
Id string `json:"id"`
|
Id string `json:"id"`
|
||||||
RemoteIP string `json:"rpc_remoteip"`
|
RemoteIP string `json:"rpc_remoteip"`
|
||||||
Hostname string `json:"hostname"`
|
Hostname string `json:"hostname"`
|
||||||
CmdLine string `json:"cmdline"`
|
CmdLine string `json:"cmdline"`
|
||||||
Pid string `json:"pid"`
|
Pid string `json:"pid"`
|
||||||
Status int `json:"status"`
|
Status int `json:"status"`
|
||||||
Extra string `json:"extra"`
|
StatusStr string `json:"status_str"`
|
||||||
|
Extra string `json:"extra"`
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
Loading…
Reference in New Issue
Block a user