support 'format' option in 'fx list' (#462)

This commit is contained in:
Minghe
2020-02-21 17:26:39 +08:00
committed by GitHub
parent 71174ead45
commit 0068fb92eb
9 changed files with 91 additions and 58 deletions

7
fx.go
View File

@@ -179,6 +179,13 @@ func main() {
Name: "list",
Aliases: []string{"ls"},
Usage: "list deployed services",
Flags: []cli.Flag{
cli.StringFlag{
Name: "format, f",
Value: "table",
Usage: "output format, 'table' and 'JSON' supported",
},
},
Action: handle(
middlewares.Parse("list"),
middlewares.LoadConfig,

View File

@@ -3,19 +3,19 @@ package handlers
import (
"github.com/metrue/fx/context"
"github.com/metrue/fx/infra"
"github.com/metrue/fx/pkg/render"
"github.com/metrue/fx/pkg/renderrer"
)
// List command handle
func List(ctx context.Contexter) (err error) {
cli := ctx.GetCliContext()
deployer := ctx.Get("deployer").(infra.Deployer)
format := ctx.Get("format").(string)
services, err := deployer.List(ctx.GetContext(), cli.Args().First())
if err != nil {
return err
}
render.Table(services)
return nil
return renderrer.Render(services, format)
}

View File

@@ -4,7 +4,7 @@ import (
"github.com/apex/log"
"github.com/metrue/fx/context"
"github.com/metrue/fx/infra"
"github.com/metrue/fx/pkg/render"
"github.com/metrue/fx/pkg/renderrer"
"github.com/metrue/fx/types"
)
@@ -42,6 +42,5 @@ func Up(ctx context.Contexter) (err error) {
if err != nil {
return err
}
render.Table([]types.Service{service})
return nil
return renderrer.Render([]types.Service{service}, "table")
}

View File

@@ -86,12 +86,6 @@ func (d *Deployer) Ping(ctx context.Context) error {
// List services
func (d *Deployer) List(ctx context.Context, name string) (svcs []types.Service, err error) {
const task = "listing"
spinner.Start(task)
defer func() {
spinner.Stop(task, err)
}()
// FIXME support remote host
return d.cli.ListContainer(ctx, name)
}

View File

@@ -42,6 +42,8 @@ func Parse(action string) func(ctx context.Contexter) (err error) {
case "list":
name := cli.Args().First()
ctx.Set("filter", name)
format := cli.String("format")
ctx.Set("format", format)
case "image_build":
sources := []string{}
for _, s := range cli.Args() {

View File

@@ -1,27 +0,0 @@
package render
import (
"fmt"
"os"
"github.com/metrue/fx/types"
"github.com/olekukonko/tablewriter"
)
// Table output services as table format
func Table(services []types.Service) {
data := [][]string{}
for _, s := range services {
col := []string{
s.ID,
s.Name,
fmt.Sprintf("%s:%d", s.Host, +s.Port),
}
data = append(data, col)
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"ID", "Name", "Endpoint"})
table.AppendBulk(data)
table.Render()
}

View File

@@ -1,19 +0,0 @@
package render
import (
"testing"
"github.com/metrue/fx/types"
)
func TestTable(t *testing.T) {
services := []types.Service{
types.Service{
ID: "id-1",
Name: "name-1",
Host: "127.0.0.1",
Port: 1000,
},
}
Table(services)
}

View File

@@ -0,0 +1,53 @@
package renderrer
import (
"encoding/json"
"fmt"
"os"
"strings"
"github.com/metrue/fx/types"
"github.com/olekukonko/tablewriter"
)
const formatJSON = "json"
//nolint:unused,varcheck,deadcode
const formatTable = "table"
// Render render output with given format
func Render(services []types.Service, format string) error {
if strings.ToLower(format) == formatJSON {
return toJSON(services)
}
return toTable(services)
}
func toTable(services []types.Service) error {
data := [][]string{}
for _, s := range services {
col := []string{
s.ID,
s.Name,
fmt.Sprintf("%s:%d", s.Host, +s.Port),
}
data = append(data, col)
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"ID", "Name", "Endpoint"})
table.AppendBulk(data)
table.Render()
return nil
}
func toJSON(services []types.Service) error {
output, err := json.Marshal(services)
if err != nil {
return err
}
if _, err := fmt.Print(string(output)); err != nil {
return err
}
return nil
}

View File

@@ -0,0 +1,24 @@
package renderrer
import (
"testing"
"github.com/metrue/fx/types"
)
func TestRenderrer(t *testing.T) {
services := []types.Service{
types.Service{
ID: "id-1",
Name: "name-1",
Host: "127.0.0.1",
Port: 1000,
},
}
t.Run("toTable", func(t *testing.T) {
Render(services, "table")
})
t.Run("toJSON", func(t *testing.T) {
Render(services, "json")
})
}