Skip to content
This repository has been archived by the owner on Dec 15, 2021. It is now read-only.

Commit

Permalink
Merge pull request #267 from ngtuna/uitable
Browse files Browse the repository at this point in the history
tabular display
  • Loading branch information
ngtuna authored Aug 22, 2017
2 parents 58b7924 + 8a4c492 commit bd91734
Show file tree
Hide file tree
Showing 19 changed files with 868 additions and 46 deletions.
44 changes: 18 additions & 26 deletions cmd/kubeless/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ package main
import (
"encoding/json"
"fmt"
"github.com/ghodss/yaml"
"os"

"github.com/Sirupsen/logrus"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"

"github.com/ghodss/yaml"
"github.com/gosuri/uitable"
"github.com/kubeless/kubeless/pkg/spec"
"github.com/kubeless/kubeless/pkg/utils"
"github.com/spf13/cobra"
"k8s.io/client-go/pkg/api"
)

Expand Down Expand Up @@ -69,33 +67,27 @@ func init() {
func print(f spec.Function, name, output string) {
switch output {
case "":
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Properties", "Value"})
table := uitable.New()
table.MaxColWidth = 80
table.Wrap = true
label, _ := json.Marshal(f.Metadata.Labels)
env, _ := json.Marshal(f.Spec.Template.Spec.Containers[0].Env)
data := [][]string{
{"Name", name},
{"Namespace", fmt.Sprintf(f.Metadata.Namespace)},
{"Handler", fmt.Sprintf(f.Spec.Handler)},
{"Runtime", fmt.Sprintf(f.Spec.Runtime)},
{"Type", fmt.Sprintf(f.Spec.Type)},
{"Topic", fmt.Sprintf(f.Spec.Topic)},
{"Dependencies", fmt.Sprintf(f.Spec.Deps)},
{"Labels", fmt.Sprintf(string(label))},
{"Environment variables", fmt.Sprintf(string(env))},
{"Memory", fmt.Sprintf(f.Spec.Template.Spec.Containers[0].Resources.Requests.Memory().String())},
}

for _, v := range data {
table.Append(v)
}
table.Render() // Send output
table.AddRow("Name:", name)
table.AddRow("Namespace:", fmt.Sprintf(f.Metadata.Namespace))
table.AddRow("Handler:", fmt.Sprintf(f.Spec.Handler))
table.AddRow("Runtime:", fmt.Sprintf(f.Spec.Runtime))
table.AddRow("Type:", fmt.Sprintf(f.Spec.Type))
table.AddRow("Topic:", fmt.Sprintf(f.Spec.Topic))
table.AddRow("Label:", fmt.Sprintf(string(label)))
table.AddRow("Envvar:", fmt.Sprintf(string(env)))
table.AddRow("Memory:", fmt.Sprintf(f.Spec.Template.Spec.Containers[0].Resources.Requests.Memory().String()))
table.AddRow("Dependencies:", fmt.Sprintf(f.Spec.Deps))
fmt.Println(table)
case "json":
b, _ := json.MarshalIndent(f, "", " ")
fmt.Println(string(b))
case "yaml":
//will fix later: there is a panic here with yaml.Marshal(f)
b, _ := yaml.Marshal(f.Spec)
b, _ := yaml.Marshal(f)
fmt.Println(string(b))
default:
fmt.Println("Wrong output format. Please use only json|yaml")
Expand Down
41 changes: 26 additions & 15 deletions cmd/kubeless/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ limitations under the License.
package main

import (
"bytes"
"encoding/json"
"fmt"
"io"

"github.com/Sirupsen/logrus"
"github.com/ghodss/yaml"
"github.com/olekukonko/tablewriter"
"github.com/gosuri/uitable"
"github.com/spf13/cobra"
"k8s.io/client-go/pkg/api"
"k8s.io/client-go/rest"
Expand Down Expand Up @@ -99,45 +100,55 @@ func doList(w io.Writer, tprClient rest.Interface, ns, output string, args []str
// printFunctions formats the output of function list
func printFunctions(w io.Writer, functions []*spec.Function, output string) error {
if output == "" {
table := tablewriter.NewWriter(w)
table.SetHeader([]string{"Name", "namespace", "handler", "runtime", "type", "topic", "dependencies"})
table := uitable.New()
table.MaxColWidth = 50
table.Wrap = true
table.AddRow("NAME", "NAMESPACE", "HANDLER", "RUNTIME", "TYPE", "TOPIC")
for _, f := range functions {
n := f.Metadata.Name
h := f.Spec.Handler
r := f.Spec.Runtime
t := f.Spec.Type
tp := f.Spec.Topic
ns := f.Metadata.Namespace
dep := f.Spec.Deps
table.Append([]string{n, ns, h, r, t, tp, dep})
table.AddRow(n, ns, h, r, t, tp)
}
table.Render()
fmt.Fprintln(w, table)
} else if output == "wide" {
table := tablewriter.NewWriter(w)
table.SetHeader([]string{"Name", "namespace", "handler", "runtime", "type", "topic", "dependencies", "memory", "env", "label"})
table := uitable.New()
table.MaxColWidth = 50
table.Wrap = true
table.AddRow("NAME", "NAMESPACE", "HANDLER", "RUNTIME", "TYPE", "TOPIC", "MEMORY", "ENV", "LABEL")
for _, f := range functions {
n := f.Metadata.Name
h := f.Spec.Handler
r := f.Spec.Runtime
t := f.Spec.Type
tp := f.Spec.Topic
ns := f.Metadata.Namespace
dep := f.Spec.Deps
mem := ""
env := []byte{}
env := ""
if len(f.Spec.Template.Spec.Containers[0].Resources.Requests) != 0 {
mem = f.Spec.Template.Spec.Containers[0].Resources.Requests.Memory().String()
}
if len(f.Spec.Template.Spec.Containers[0].Env) != 0 {
env, _ = json.Marshal(f.Spec.Template.Spec.Containers[0].Env)
var buffer bytes.Buffer
for _, e := range f.Spec.Template.Spec.Containers[0].Env {
buffer.WriteString(e.Name + " = " + e.Value + "\n")
}
env = buffer.String()
}
label := []byte{}
label := ""
if len(f.Metadata.Labels) > 0 {
label, _ = json.Marshal(f.Metadata.Labels)
var buffer bytes.Buffer
for k, v := range f.Metadata.Labels {
buffer.WriteString(k + " : " + v + "\n")
}
label = buffer.String()
}
table.Append([]string{n, ns, h, r, t, tp, dep, mem, string(env), string(label)})
table.AddRow(n, ns, h, r, t, tp, mem, env, label)
}
table.Render()
fmt.Fprintln(w, table)
} else {
for _, f := range functions {
switch output {
Expand Down
6 changes: 5 additions & 1 deletion cmd/kubeless/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ func TestList(t *testing.T) {
Name: "foo",
Value: "bar",
},
{
Name: "foo2",
Value: "bar2",
},
},
Resources: v1.ResourceRequirements{
Limits: map[v1.ResourceName]resource.Quantity{
Expand Down Expand Up @@ -172,7 +176,7 @@ func TestList(t *testing.T) {
// wide output
output = listOutput(t, client, "myns", "wide", []string{})
t.Log("output is", output)
if !strings.Contains(output, "[{\"name\":\"foo\",\"value\":\"bar\"}]") {
if !strings.Contains(output, "foo = bar") {
t.Errorf("table output didn't mention proper env of function")
}
}
9 changes: 7 additions & 2 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ import:
- pkg/kubectl/cmd/util
- package: k8s.io/client-go
version: v3.0.0-beta.0
- package: github.com/olekukonko/tablewriter
- package: github.com/docker/spdystream
- package: github.com/renstrom/dedent
- package: github.com/renstrom/dedent
- package: github.com/gosuri/uitable
8 changes: 8 additions & 0 deletions vendor/github.com/gosuri/uitable/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions vendor/github.com/gosuri/uitable/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions vendor/github.com/gosuri/uitable/Makefile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 67 additions & 0 deletions vendor/github.com/gosuri/uitable/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions vendor/github.com/gosuri/uitable/example/main.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions vendor/github.com/gosuri/uitable/example_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit bd91734

Please sign in to comment.