feat: add extra json format

This commit is contained in:
liruichen 2023-09-08 17:27:32 +08:00
parent ef75f275b0
commit b0692a2b0f
4 changed files with 34 additions and 17 deletions

View File

@ -31,6 +31,7 @@ var (
listHost string
listWide bool
listIds []string
listJson bool
)
func init() {
@ -38,6 +39,7 @@ func init() {
add1Flags := func(f *pflag.FlagSet) {
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(&listJson, "json", false, "list all services info as json format")
f.StringSliceVar(&listIds, "id", nil, "specify the ids of the services")
}
@ -50,7 +52,7 @@ func init() {
}
func list(cmd *cobra.Command, args []string) {
client.ListAgents(listHost, listIds, listWide)
client.ListAgents(listHost, listIds, listWide, listJson)
}
var getServiceCmd = &cobra.Command{
@ -59,7 +61,7 @@ var getServiceCmd = &cobra.Command{
}
func getAgents(cmd *cobra.Command, args []string) {
client.ListAgents(listHost, listIds, listWide)
client.ListAgents(listHost, listIds, listWide, listJson)
}
var deleteServiceCmd = &cobra.Command{

View File

@ -14,6 +14,8 @@
package client
import (
"encoding/json"
"fmt"
"os"
"github.com/RickLeee/goc-v2/pkg/client/rest"
@ -27,7 +29,7 @@ const (
WATCHCONNECT = 1 << iota
)
func ListAgents(host string, ids []string, wide bool) {
func ListAgents(host string, ids []string, wide, isJson bool) {
gocClient := rest.NewV2Client(host)
agents, err := gocClient.Agent().Get(ids)
@ -35,8 +37,11 @@ func ListAgents(host string, ids []string, wide bool) {
if err != nil {
log.Fatalf("cannot get agent list from goc server: %v", err)
}
table := tablewriter.NewWriter(os.Stdout)
if isJson {
goto asJson
}
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
@ -54,6 +59,7 @@ func ListAgents(host string, ids []string, wide bool) {
table.SetHeader([]string{"ID", "STATUS", "REMOTEIP", "CMD"})
table.SetColumnAlignment([]int{tablewriter.ALIGN_LEFT, tablewriter.ALIGN_LEFT, tablewriter.ALIGN_LEFT, tablewriter.ALIGN_LEFT})
}
asJson:
for _, agent := range agents {
var status string
if agent.Status == DISCONNECT {
@ -61,6 +67,8 @@ func ListAgents(host string, ids []string, wide bool) {
} else if agent.Status&(RPCCONNECT|WATCHCONNECT) > 0 {
status = "CONNECT"
}
agent.StatusStr = status
if !isJson {
if wide {
table.Append([]string{agent.Id, status, agent.RemoteIP, agent.Hostname, agent.Pid, agent.CmdLine, agent.Extra})
} else {
@ -68,7 +76,12 @@ func ListAgents(host string, ids []string, wide bool) {
table.Append([]string{agent.Id, status, agent.RemoteIP, getSimpleCmdLine(preLen, agent.CmdLine)})
}
}
}
if !isJson {
table.Render()
}
b, _ := json.Marshal(agents)
fmt.Fprint(os.Stdout, string(b))
}
func DeleteAgents(host string, ids []string) {

View File

@ -79,6 +79,7 @@ func NewWorker(host string) Action {
}
}
// ListAgents Deprecated
func (c *client) ListAgents(wide bool) {
u := fmt.Sprintf("%s%s", c.Host, CoverAgentsListAPI)
_, body, err := c.do("GET", u, "", nil)

View File

@ -27,6 +27,7 @@ type Agent struct {
CmdLine string `json:"cmdline"`
Pid string `json:"pid"`
Status int `json:"status"`
StatusStr string `json:"status_str"`
Extra string `json:"extra"`
}