Skip to content

Commit

Permalink
Fixes in Flytectl (#109)
Browse files Browse the repository at this point in the history
- Show the latest entity based on created at
 - Optional truncation of large strings in table printing

Signed-off-by: Ketan Umare <[email protected]>
  • Loading branch information
kumare3 authored Jun 18, 2021
1 parent 179da66 commit e569acb
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 32 deletions.
10 changes: 6 additions & 4 deletions flytectl/cmd/get/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,17 @@ Usage
`
)

var hundredChars = 100

var executionColumns = []printer.Column{
{Header: "Name", JSONPath: "$.id.name"},
{Header: "Launch Plan Name", JSONPath: "$.spec.launchPlan.name"},
{Header: "Type", JSONPath: "$.spec.launchPlan.resourceType"},
{Header: "Phase", JSONPath: "$.closure.phase"},
{Header: "Started", JSONPath: "$.closure.startedAt"},
{Header: "Elapsed Time", JSONPath: "$.closure.duration"},
{Header: "Abort data", JSONPath: "$.closure.abortMetadata[\"cause\"]"},
{Header: "Error data", JSONPath: "$.closure.error[\"message\"]"},
{Header: "Abort data (Trunc)", JSONPath: "$.closure.abortMetadata[\"cause\"]", TruncateTo: &hundredChars},
{Header: "Error data (Trunc)", JSONPath: "$.closure.error[\"message\"]", TruncateTo: &hundredChars},
}

func ExecutionToProtoMessages(l []*admin.Execution) []proto.Message {
Expand All @@ -78,11 +80,11 @@ func getExecutionFunc(ctx context.Context, args []string, cmdCtx cmdCore.Command
var executions []*admin.Execution
if len(args) > 0 {
name := args[0]
execution, err := cmdCtx.AdminFetcherExt().FetchExecution(ctx, name, config.GetConfig().Project, config.GetConfig().Domain)
exec, err := cmdCtx.AdminFetcherExt().FetchExecution(ctx, name, config.GetConfig().Project, config.GetConfig().Domain)
if err != nil {
return err
}
executions = append(executions, execution)
executions = append(executions, exec)
logger.Infof(ctx, "Retrieved %v executions", len(executions))
return adminPrinter.Print(config.GetConfig().MustOutputFormat(), executionColumns,
ExecutionToProtoMessages(executions)...)
Expand Down
17 changes: 15 additions & 2 deletions flytectl/cmd/get/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"io"
"testing"

"github.com/stretchr/testify/mock"

"github.com/flyteorg/flytectl/cmd/config"
cmdCore "github.com/flyteorg/flytectl/cmd/core"
"github.com/flyteorg/flyteidl/clients/go/admin/mocks"
Expand All @@ -25,6 +27,10 @@ func TestListExecutionFunc(t *testing.T) {
cmdCtx := cmdCore.NewCommandContext(mockClient, *mockOutStream)
execListRequest := &admin.ResourceListRequest{
Limit: 100,
SortBy: &admin.Sort{
Key: "created_at",
Direction: admin.Sort_DESCENDING,
},
Id: &admin.NamedEntityIdentifier{
Project: projectValue,
Domain: domainValue,
Expand Down Expand Up @@ -58,7 +64,9 @@ func TestListExecutionFunc(t *testing.T) {
executionList := &admin.ExecutionList{
Executions: executions,
}
mockClient.OnListExecutionsMatch(ctx, execListRequest).Return(executionList, nil)
mockClient.OnListExecutionsMatch(mock.Anything, mock.MatchedBy(func(o *admin.ResourceListRequest) bool {
return execListRequest.SortBy.Key == o.SortBy.Key && execListRequest.SortBy.Direction == o.SortBy.Direction && execListRequest.Filters == o.Filters && execListRequest.Limit == o.Limit
})).Return(executionList, nil)
err := getExecutionFunc(ctx, args, cmdCtx)
assert.Nil(t, err)
mockClient.AssertCalled(t, "ListExecutions", ctx, execListRequest)
Expand All @@ -75,6 +83,9 @@ func TestListExecutionFuncWithError(t *testing.T) {
cmdCtx := cmdCore.NewCommandContext(mockClient, *mockOutStream)
execListRequest := &admin.ResourceListRequest{
Limit: 100,
SortBy: &admin.Sort{
Key: "created_at",
},
Id: &admin.NamedEntityIdentifier{
Project: projectValue,
Domain: domainValue,
Expand Down Expand Up @@ -104,7 +115,9 @@ func TestListExecutionFuncWithError(t *testing.T) {
Phase: core.WorkflowExecution_SUCCEEDED,
},
}
mockClient.OnListExecutionsMatch(ctx, execListRequest).Return(nil, errors.New("executions NotFound"))
mockClient.OnListExecutionsMatch(mock.Anything, mock.MatchedBy(func(o *admin.ResourceListRequest) bool {
return execListRequest.SortBy.Key == o.SortBy.Key && execListRequest.SortBy.Direction == o.SortBy.Direction && execListRequest.Filters == o.Filters && execListRequest.Limit == o.Limit
})).Return(nil, errors.New("executions NotFound"))
err := getExecutionFunc(ctx, args, cmdCtx)
assert.NotNil(t, err)
assert.Equal(t, err, errors.New("executions NotFound"))
Expand Down
8 changes: 4 additions & 4 deletions flytectl/cmd/sandbox/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ type ExecResult struct {
}

func startSandboxCluster(ctx context.Context, args []string, cmdCtx cmdCore.CommandContext) error {
fmt.Printf("%v It will take some time, We will start a fresh flyte cluster for you %v %v\n", emoji.ManTechnologist, emoji.Rocket, emoji.Rocket)
fmt.Printf("%v Bootstrapping a brand new flyte cluster... %v %v\n", emoji.FactoryWorker, emoji.Hammer, emoji.Wrench)
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
fmt.Printf("Please Check your docker client %v \n", emoji.ManTechnologist)
fmt.Printf("%v Please Check your docker client %v \n", emoji.GrimacingFace, emoji.Whale)
return err
}

Expand All @@ -72,13 +72,13 @@ func startSandboxCluster(ctx context.Context, args []string, cmdCtx cmdCore.Comm

defer func() {
if r := recover(); r != nil {
fmt.Println("Something goes wrong with container status", r)
fmt.Printf("%v Something went horribly wrong! %s\n", emoji.GrimacingFace, r)
}
}()

ID, err := startContainer(cli, volumes)
if err != nil {
fmt.Println("Something goes wrong. We are not able to start sandbox container, Please check your docker client and try again ")
fmt.Printf("%v Something went horribly wrong: Failed to start Sandbox container %v, Please check your docker client and try again. \n", emoji.GrimacingFace, emoji.Whale)
return fmt.Errorf("error: %v", err)
}

Expand Down
2 changes: 1 addition & 1 deletion flytectl/cmd/sandbox/teardown.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ func teardownSandboxCluster(ctx context.Context, args []string, cmdCtx cmdCore.C
if err := configCleanup(); err != nil {
fmt.Printf("Config cleanup failed. Which Failed due to %v \n ", err)
}
fmt.Printf("Sandbox cluster is removed successfully %v \n", emoji.Rocket)
fmt.Printf("%v %v Sandbox cluster is removed successfully. \n", emoji.Broom, emoji.Broom)
return nil
}
1 change: 0 additions & 1 deletion flytectl/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ require (
github.com/flyteorg/flyteidl v0.19.3
github.com/flyteorg/flytestdlib v0.3.24
github.com/ghodss/yaml v1.0.0
github.com/goccy/go-graphviz v0.0.9
github.com/golang/protobuf v1.4.3
github.com/google/go-github v17.0.0+incompatible
github.com/google/go-querystring v1.1.0 // indirect
Expand Down
14 changes: 0 additions & 14 deletions flytectl/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,6 @@ github.com/coreos/go-systemd/v22 v22.0.0/go.mod h1:xO0FLkIi5MaZafQlIrOotqXZ90ih+
github.com/coreos/go-systemd/v22 v22.1.0/go.mod h1:xO0FLkIi5MaZafQlIrOotqXZ90ih+1atmu1JpKERPPk=
github.com/coreos/pkg v0.0.0-20160727233714-3ac0863d7acf/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/corona10/goimagehash v1.0.2 h1:pUfB0LnsJASMPGEZLj7tGY251vF+qLGqOgEP4rUs6kA=
github.com/corona10/goimagehash v1.0.2/go.mod h1:/l9umBhvcHQXVtQO1V6Gp1yD20STawkhRnnX0D1bvVI=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.0 h1:EoUDS0afbrsXAZ9YQ9jdu/mZ2sXgT1/2yyNng4PGlyM=
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
Expand Down Expand Up @@ -344,8 +342,6 @@ github.com/flyteorg/flyteidl v0.19.3/go.mod h1:576W2ViEyjTpT+kEVHAGbrTP3HARNUZ/e
github.com/flyteorg/flytestdlib v0.3.13/go.mod h1:Tz8JCECAbX6VWGwFT6cmEQ+RJpZ/6L9pswu3fzWs220=
github.com/flyteorg/flytestdlib v0.3.24 h1:Eu5TMKch9ihOavPKufgTBI677eVYjJpOAPPg9hfZIzU=
github.com/flyteorg/flytestdlib v0.3.24/go.mod h1:1XG0DwYTUm34Yrffm1Qy9Tdr/pWQypEqTq5dUxw3/cM=
github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8=
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/form3tech-oss/jwt-go v3.2.2+incompatible h1:TcekIExNqud5crz4xD2pavyTgWiPvpYe4Xau31I0PRk=
github.com/form3tech-oss/jwt-go v3.2.2+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k=
github.com/franela/goblin v0.0.0-20200105215937-c9ffbefa60db/go.mod h1:7dvUGVsVBjqR7JHJk0brhHOZYGmfBYOrK0ZhYMEtBr4=
Expand Down Expand Up @@ -384,8 +380,6 @@ github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-test/deep v1.0.7 h1:/VSMRlnY/JSyqxQUzQLKVMAskpY/NZKFA5j2P+0pP2M=
github.com/go-test/deep v1.0.7/go.mod h1:QV8Hv/iy04NyLBxAdO9njL0iVPN1S4d/A3NVv1V36o8=
github.com/goccy/go-graphviz v0.0.9 h1:s/FMMJ1Joj6La3S5ApO3Jk2cwM4LpXECC2muFx3IPQQ=
github.com/goccy/go-graphviz v0.0.9/go.mod h1:wXVsXxmyMQU6TN3zGRttjNn3h+iCAS7xQFC6TlNvLhk=
github.com/godbus/dbus v0.0.0-20151105175453-c7fdd8b5cd55/go.mod h1:/YcGZj5zSblfDWMMoOzV4fas9FZnQYTkDnsGvmh2Grw=
github.com/godbus/dbus v0.0.0-20180201030542-885f9cc04c9c/go.mod h1:/YcGZj5zSblfDWMMoOzV4fas9FZnQYTkDnsGvmh2Grw=
github.com/godbus/dbus v0.0.0-20190422162347-ade71ed3457e h1:BWhy2j3IXJhjCbC68FptL43tDKIq8FladmaTs3Xs7Z8=
Expand All @@ -403,8 +397,6 @@ github.com/gogo/protobuf v1.3.0/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXP
github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
Expand Down Expand Up @@ -544,7 +536,6 @@ github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NH
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
github.com/j-keck/arping v0.0.0-20160618110441-2cf9dc699c56/go.mod h1:ymszkNOg6tORTn+6F6j+Jc8TOr5osrynvN6ivFWZ2GA=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/jmespath/go-jmespath v0.0.0-20160803190731-bd40a432e4c7/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
Expand Down Expand Up @@ -661,8 +652,6 @@ github.com/ncw/swift v1.0.47/go.mod h1:23YIA4yWVnGwv2dQlN4bB7egfYX6YLn0Yo/S6zZO/
github.com/ncw/swift v1.0.49/go.mod h1:23YIA4yWVnGwv2dQlN4bB7egfYX6YLn0Yo/S6zZO/ZM=
github.com/ncw/swift v1.0.53 h1:luHjjTNtekIEvHg5KdAFIBaH7bWfNkefwFnpDffSIks=
github.com/ncw/swift v1.0.53/go.mod h1:23YIA4yWVnGwv2dQlN4bB7egfYX6YLn0Yo/S6zZO/ZM=
github.com/nfnt/resize v0.0.0-20160724205520-891127d8d1b5 h1:BvoENQQU+fZ9uukda/RzCAL/191HHwJA5b13R6diVlY=
github.com/nfnt/resize v0.0.0-20160724205520-891127d8d1b5/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
Expand Down Expand Up @@ -932,7 +921,6 @@ golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200204104054-c9f3fb736b72/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
Expand All @@ -951,8 +939,6 @@ golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EH
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.0.0-20200119044424-58c23975cae1 h1:5h3ngYt7+vXCDZCup/HkCQgW5XwmSvR/nA2JmJ0RErg=
golang.org/x/image v0.0.0-20200119044424-58c23975cae1/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand Down
5 changes: 3 additions & 2 deletions flytectl/pkg/filters/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package filters
var (
DefaultLimit int32 = 100
DefaultFilter = Filters{
Limit: DefaultLimit,
Asc: false,
Limit: DefaultLimit,
SortBy: "created_at",
Asc: false,
}
)

Expand Down
11 changes: 10 additions & 1 deletion flytectl/pkg/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ func OutputFormats() []string {
type Column struct {
Header string
JSONPath string
// Optional Truncation directive to limit content. This will simply truncate the string output.
TruncateTo *int
}

type Printer struct{}
Expand All @@ -65,7 +67,14 @@ func extractRow(data interface{}, columns []Column) []string {
if err != nil || out == nil {
out = ""
}
tableData = append(tableData, fmt.Sprintf("%s", out))
s := fmt.Sprintf("%s", out)
if c.TruncateTo != nil {
t := *c.TruncateTo
if len(s) > t {
s = s[:t]
}
}
tableData = append(tableData, s)
}
return tableData
}
Expand Down
7 changes: 4 additions & 3 deletions flytectl/pkg/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,24 @@ func WorkflowToProtoMessages(l []*admin.Workflow) []proto.Message {

// TODO Convert this to a Testable Example. For some reason the comparison fails
func TestJSONToTable(t *testing.T) {
trunc := 5
d := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
j := []struct {
A string `json:"a"`
B int `json:"b"`
S *Inner `json:"s"`
}{
{"hello", 0, &Inner{"x-hello", nil}},
{"hello", 0, &Inner{"x-hello", &d}},
{"hello world", 0, &Inner{"x-hello", &d}},
{"hello", 0, nil},
}

b, err := json.Marshal(j)
assert.NoError(t, err)
p := Printer{}
assert.NoError(t, p.JSONToTable(b, []Column{
{"A", "$.a"},
{"S", "$.s.y"},
{"A", "$.a", &trunc},
{"S", "$.s.y", nil},
}))
// Output:
// | A | S |
Expand Down

0 comments on commit e569acb

Please sign in to comment.