Introduce a JSON output table mechanic

- Create JSONTable as a proxy/extension to tablewriter which can also output JSON.
- Implement machine readable output for `server list` and `recipe list`
This commit is contained in:
2023-01-10 18:51:38 -08:00
committed by Gitea
parent 89fcb5b216
commit cae0d9ef79
5 changed files with 223 additions and 10 deletions

View File

@ -7,12 +7,12 @@ import (
"coopcloud.tech/abra/pkg/app"
"coopcloud.tech/abra/pkg/config"
"coopcloud.tech/abra/pkg/formatter"
"coopcloud.tech/abra/pkg/jsontable"
"coopcloud.tech/abra/pkg/recipe"
recipePkg "coopcloud.tech/abra/pkg/recipe"
"coopcloud.tech/abra/pkg/secret"
"coopcloud.tech/abra/pkg/ssh"
"github.com/AlecAivazis/survey/v2"
"github.com/olekukonko/tablewriter"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
@ -144,7 +144,7 @@ func NewAction(c *cli.Context) error {
}
var secrets AppSecrets
var secretTable *tablewriter.Table
var secretTable *jsontable.JSONTable
if Secrets {
if err := ssh.EnsureHostKey(NewAppServer); err != nil {
logrus.Fatal(err)

View File

@ -27,6 +27,7 @@ var recipeListCommand = cli.Command{
Aliases: []string{"ls"},
Flags: []cli.Flag{
internal.DebugFlag,
internal.MachineReadableFlag,
patternFlag,
},
Before: internal.SubCommandBefore,
@ -66,10 +67,14 @@ var recipeListCommand = cli.Command{
}
}
table.SetCaption(true, fmt.Sprintf("total recipes: %v", len))
if table.NumLines() > 0 {
table.Render()
if internal.MachineReadable {
table.SetCaption(false, "")
table.JSONRender()
} else {
table.SetCaption(true, fmt.Sprintf("total recipes: %v", len))
table.Render()
}
}
return nil

View File

@ -18,6 +18,7 @@ var serverListCommand = cli.Command{
Usage: "List managed servers",
Flags: []cli.Flag{
internal.DebugFlag,
internal.MachineReadableFlag,
},
Before: internal.SubCommandBefore,
Action: func(c *cli.Context) error {
@ -29,8 +30,11 @@ var serverListCommand = cli.Command{
tableColumns := []string{"name", "host", "user", "port"}
table := formatter.CreateTable(tableColumns)
defer table.Render()
if internal.MachineReadable {
defer table.JSONRender()
} else {
defer table.Render()
}
serverNames, err := config.ReadServerNames()
if err != nil {
logrus.Fatal(err)