diff --git a/cmd/service.go b/cmd/service.go index 3d5d036..77dda53 100644 --- a/cmd/service.go +++ b/cmd/service.go @@ -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{ diff --git a/pkg/client/agent.go b/pkg/client/agent.go index a1f20c9..fdc8415 100644 --- a/pkg/client/agent.go +++ b/pkg/client/agent.go @@ -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,14 +67,21 @@ func ListAgents(host string, ids []string, wide bool) { } else if agent.Status&(RPCCONNECT|WATCHCONNECT) > 0 { status = "CONNECT" } - if wide { - table.Append([]string{agent.Id, status, agent.RemoteIP, agent.Hostname, agent.Pid, agent.CmdLine, agent.Extra}) - } else { - preLen := len(agent.Id) + len(agent.RemoteIP) + 9 - table.Append([]string{agent.Id, status, agent.RemoteIP, getSimpleCmdLine(preLen, agent.CmdLine)}) + agent.StatusStr = status + if !isJson { + if wide { + table.Append([]string{agent.Id, status, agent.RemoteIP, agent.Hostname, agent.Pid, agent.CmdLine, agent.Extra}) + } 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) { diff --git a/pkg/client/client.go b/pkg/client/client.go index 79f9352..9a61cad 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -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) diff --git a/pkg/client/rest/agent/agent.go b/pkg/client/rest/agent/agent.go index 77a861f..678f0cb 100644 --- a/pkg/client/rest/agent/agent.go +++ b/pkg/client/rest/agent/agent.go @@ -21,13 +21,14 @@ import ( ) type Agent struct { - Id string `json:"id"` - RemoteIP string `json:"rpc_remoteip"` - Hostname string `json:"hostname"` - CmdLine string `json:"cmdline"` - Pid string `json:"pid"` - Status int `json:"status"` - Extra string `json:"extra"` + Id string `json:"id"` + RemoteIP string `json:"rpc_remoteip"` + Hostname string `json:"hostname"` + CmdLine string `json:"cmdline"` + Pid string `json:"pid"` + Status int `json:"status"` + StatusStr string `json:"status_str"` + Extra string `json:"extra"` } const (