Skip to content

Commit

Permalink
feat: add --format for commands to output json (#1323)
Browse files Browse the repository at this point in the history
* feat: add --format for commands to output json

- verson
- context ls
- image ls
- image describe
- env ls
- env describe

Signed-off-by: cutecutecat <starkind1997@gmail.com>

* move json format into package

Signed-off-by: cutecutecat <starkind1997@gmail.com>

* refactor default table formatter into pkg/app/formatter/table

Signed-off-by: cutecutecat <starkind1997@gmail.com>

* fix Json spelling into JSON

Signed-off-by: cutecutecat <starkind1997@gmail.com>

* fix lint

Signed-off-by: cutecutecat <starkind1997@gmail.com>

* fix more lint

Signed-off-by: cutecutecat <12032261@mail.sustech.edu.cn>

Signed-off-by: cutecutecat <starkind1997@gmail.com>
Signed-off-by: cutecutecat <12032261@mail.sustech.edu.cn>
  • Loading branch information
cutecutecat committed Dec 27, 2022
1 parent 296680c commit 571abfe
Show file tree
Hide file tree
Showing 16 changed files with 820 additions and 306 deletions.
56 changes: 15 additions & 41 deletions pkg/app/context_ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,23 @@
package app

import (
"fmt"
"io"
"os"

"github.com/cockroachdb/errors"
"github.com/olekukonko/tablewriter"
"github.com/urfave/cli/v2"

"github.com/tensorchord/envd/pkg/app/formatter"
"github.com/tensorchord/envd/pkg/app/formatter/json"
"github.com/tensorchord/envd/pkg/app/formatter/table"
"github.com/tensorchord/envd/pkg/home"
"github.com/tensorchord/envd/pkg/types"
)

var CommandContextList = &cli.Command{
Name: "ls",
Usage: "List envd contexts",
Name: "ls",
Usage: "List envd contexts",
Flags: []cli.Flag{
&formatter.FormatFlag,
},
Action: contextList,
}

Expand All @@ -38,40 +40,12 @@ func contextList(clicontext *cli.Context) error {
if err != nil {
return errors.Wrap(err, "failed to list context")
}
renderContext(contexts, os.Stdout)
return nil
}

func renderContext(contexts types.EnvdContext, w io.Writer) {
table := tablewriter.NewWriter(w)
table.SetHeader([]string{"context", "builder", "builder addr", "runner", "runner addr"})

table.SetAutoWrapText(false)
table.SetAutoFormatHeaders(true)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.SetHeaderLine(false)
table.SetBorder(false)
table.SetTablePadding("\t") // pad with tabs
table.SetNoWhiteSpace(true)

for _, p := range contexts.Contexts {
envRow := make([]string, 5)
if p.Name == contexts.Current {
envRow[0] = fmt.Sprintf("%s (current)", p.Name)
} else {
envRow[0] = p.Name
}
envRow[1] = string(p.Builder)
envRow[2] = fmt.Sprintf("%s://%s", p.Builder, p.BuilderAddress)
envRow[3] = string(p.Runner)
if p.RunnerAddress != nil {
envRow[4] = stringOrNone(*p.RunnerAddress)
}
table.Append(envRow)
format := clicontext.String("format")
switch format {
case "table":
table.RenderContext(os.Stdout, contexts)
case "json":
return json.PrintContext(contexts)
}
table.Render()
return nil
}
73 changes: 12 additions & 61 deletions pkg/app/env_describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
package app

import (
"io"
"os"
"path/filepath"

"github.com/cockroachdb/errors"
"github.com/olekukonko/tablewriter"
"github.com/urfave/cli/v2"

"github.com/tensorchord/envd/pkg/app/formatter"
"github.com/tensorchord/envd/pkg/app/formatter/json"
"github.com/tensorchord/envd/pkg/app/formatter/table"
"github.com/tensorchord/envd/pkg/envd"
"github.com/tensorchord/envd/pkg/home"
"github.com/tensorchord/envd/pkg/types"
)

func getCurrentDirOrPanic() string {
Expand All @@ -47,6 +47,7 @@ var CommandDescribeEnvironment = &cli.Command{
Aliases: []string{"e"},
Value: getCurrentDirOrPanic(),
},
&formatter.FormatFlag,
},
Action: getEnvironmentDescriptions,
}
Expand Down Expand Up @@ -74,64 +75,14 @@ func getEnvironmentDescriptions(clicontext *cli.Context) error {
if err != nil {
return errors.Wrap(err, "failed to list port bindings")
}

renderDependencies(os.Stdout, dep)
renderPortBindings(os.Stdout, ports)
return nil
}

func createTable(w io.Writer, headers []string) *tablewriter.Table {
table := tablewriter.NewWriter(w)
table.SetHeader(headers)

table.SetAutoWrapText(false)
table.SetAutoFormatHeaders(true)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.SetHeaderLine(false)
table.SetBorder(true)
table.SetTablePadding("\t") // pad with tabs
table.SetNoWhiteSpace(true)

return table
}

func renderPortBindings(w io.Writer, ports []types.PortBinding) {
if ports == nil {
return
}
table := createTable(w, []string{"Name", "Container Port", "Protocol", "Host IP", "Host Port"})
for _, port := range ports {
row := make([]string, 5)
row[0] = port.Name
row[1] = port.Port
row[2] = port.Protocol
row[3] = port.HostIP
row[4] = port.HostPort
table.Append(row)
format := clicontext.String("format")
switch format {
case "table":
table.RenderDependencies(os.Stdout, dep)
table.RenderPortBindings(os.Stdout, ports)
case "json":
return json.PrintEnvironmentDescriptions(dep, ports)
}
table.Render()
}

func renderDependencies(w io.Writer, dep *types.Dependency) {
if dep == nil {
return
}
table := createTable(w, []string{"Dependencies", "Type"})
for _, p := range dep.PyPIPackages {
envRow := make([]string, 2)
envRow[0] = p
envRow[1] = "Python"
table.Append(envRow)
}
for _, p := range dep.APTPackages {
envRow := make([]string, 2)
envRow[0] = p
envRow[1] = "APT"
table.Append(envRow)
}
table.Render()
return nil
}
67 changes: 14 additions & 53 deletions pkg/app/env_ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@
package app

import (
"fmt"
"io"
"os"
"strconv"
"strings"

"github.com/cockroachdb/errors"
"github.com/olekukonko/tablewriter"
"github.com/urfave/cli/v2"

"github.com/tensorchord/envd/pkg/app/formatter"
"github.com/tensorchord/envd/pkg/app/formatter/json"
"github.com/tensorchord/envd/pkg/app/formatter/table"
"github.com/tensorchord/envd/pkg/app/telemetry"
"github.com/tensorchord/envd/pkg/envd"
"github.com/tensorchord/envd/pkg/home"
"github.com/tensorchord/envd/pkg/types"
)

var CommandListEnv = &cli.Command{
Name: "list",
Aliases: []string{"ls", "l"},
Usage: "List envd environments",
Action: getEnvironment,
Flags: []cli.Flag{
&formatter.FormatFlag,
},
Action: getEnvironment,
}

func getEnvironment(clicontext *cli.Context) error {
Expand All @@ -56,51 +56,12 @@ func getEnvironment(clicontext *cli.Context) error {
if err != nil {
return err
}
renderEnvironments(envs, os.Stdout)
return nil
}

func renderEnvironments(envs []types.EnvdEnvironment, w io.Writer) {
table := tablewriter.NewWriter(w)
table.SetHeader([]string{
"Name", "Endpoint", "SSH Target", "Image",
"GPU", "CUDA", "CUDNN", "Status",
})

table.SetAutoWrapText(false)
table.SetAutoFormatHeaders(true)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.SetHeaderLine(false)
table.SetBorder(false)
table.SetTablePadding("\t") // pad with tabs
table.SetNoWhiteSpace(true)

for _, env := range envs {
envRow := make([]string, 9)
envRow[0] = env.Name
envRow[1] = endpointOrNone(env)
envRow[2] = fmt.Sprintf("%s.envd", env.Name)
envRow[3] = env.Spec.Image
envRow[4] = strconv.FormatBool(env.GPU)
envRow[5] = stringOrNone(env.CUDA)
envRow[6] = stringOrNone(env.CUDNN)
envRow[7] = env.Status.Phase
table.Append(envRow)
format := clicontext.String("format")
switch format {
case "table":
table.RenderEnvironments(os.Stdout, envs)
case "json":
return json.PrintEnvironments(envs)
}
table.Render()
}

func endpointOrNone(env types.EnvdEnvironment) string {
var res strings.Builder
if env.Status.JupyterAddr != nil {
res.WriteString(fmt.Sprintf("jupyter: %s", *env.Status.JupyterAddr))
}
if env.Status.RStudioServerAddr != nil {
res.WriteString(fmt.Sprintf("rstudio: %s", *env.Status.RStudioServerAddr))
}
return res.String()
return nil
}
Loading

0 comments on commit 571abfe

Please sign in to comment.