Skip to content

Commit

Permalink
Simply apps describe command and add apps log command (#386)
Browse files Browse the repository at this point in the history
* Collapse apps describe command with the extended flag and create separate logs command

* chore: Use upstream version of meroxa-go

* fix: code using latest meroxa-go

* chore: update docs

* fix: tests

* refactor: display functions and tests

I noticed the display and tests file were getting very big, and I decided to refactor this pkg.

This was will become easier to spot those new functions that are not being tested (I spotted some doing this refactor).

* test: AppLogsTable and change output

Certain bits are shown depending on its content

Co-authored-by: Raúl Barroso <[email protected]>
  • Loading branch information
janelletavares and raulb authored Aug 3, 2022
1 parent 147fa1f commit 3ec62b1
Show file tree
Hide file tree
Showing 116 changed files with 2,596 additions and 1,993 deletions.
1 change: 1 addition & 0 deletions cmd/meroxa/root/apps/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func (*Apps) SubCommands() []*cobra.Command {
builder.BuildCobraCommand(&Describe{}),
builder.BuildCobraCommand(&Init{}),
builder.BuildCobraCommand(&List{}),
builder.BuildCobraCommand(&Logs{}),
builder.BuildCobraCommand(&Remove{}),
builder.BuildCobraCommand(&Run{}),
}
Expand Down
83 changes: 27 additions & 56 deletions cmd/meroxa/root/apps/describe.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright © 2021 Meroxa Inc
Copyright © 2022 Meroxa Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -17,29 +17,26 @@ limitations under the License.
package apps

import (
"bytes"
"context"
"errors"
"net/http"

"github.com/meroxa/cli/utils/display"

"github.com/meroxa/cli/cmd/meroxa/builder"
"github.com/meroxa/cli/log"
"github.com/meroxa/cli/utils"
"github.com/meroxa/meroxa-go/pkg/meroxa"
)

var (
_ builder.CommandWithDocs = (*Describe)(nil)
_ builder.CommandWithArgs = (*Describe)(nil)
_ builder.CommandWithFlags = (*Describe)(nil)
_ builder.CommandWithClient = (*Describe)(nil)
_ builder.CommandWithLogger = (*Describe)(nil)
_ builder.CommandWithExecute = (*Describe)(nil)
)

type describeApplicationClient interface {
GetApplication(ctx context.Context, nameOrUUID string) (*meroxa.Application, error)
GetFunctionLogs(ctx context.Context, nameOrUUID string) (*http.Response, error)
GetResourceByNameOrID(ctx context.Context, nameOrID string) (*meroxa.Resource, error)
GetConnectorByNameOrID(ctx context.Context, nameOrID string) (*meroxa.Connector, error)
GetFunction(ctx context.Context, nameOrUUID string) (*meroxa.Function, error)
Expand All @@ -52,14 +49,6 @@ type Describe struct {
args struct {
NameOrUUID string
}

flags struct {
Extended bool `long:"extended" usage:"whether to show additional details about the Turbine Data Application"`
}
}

func (d *Describe) Flags() []builder.Flag {
return builder.BuildFlags(&d.flags)
}

func (d *Describe) Usage() string {
Expand All @@ -81,53 +70,35 @@ func (d *Describe) Execute(ctx context.Context) error {
return err
}

if d.flags.Extended {
resources := make([]*meroxa.Resource, 0)
connectors := make(map[string]*meroxa.Connector)
functions := make([]*meroxa.Function, 0)

for _, rr := range app.Resources {
resource, err := d.client.GetResourceByNameOrID(ctx, rr.Name.String)
if err != nil {
return err
}
resources = append(resources, resource)
resources := make([]*meroxa.Resource, 0)
connectors := make([]*meroxa.Connector, 0)
functions := make([]*meroxa.Function, 0)

for _, rr := range app.Resources {
resource, err := d.client.GetResourceByNameOrID(ctx, rr.Name.String)
if err != nil {
return err
}
for _, cc := range app.Connectors {
connector, err := d.client.GetConnectorByNameOrID(ctx, cc.Name.String)
if err != nil {
return err
}
connectors[connector.ResourceName] = connector
resources = append(resources, resource)
}
for _, cc := range app.Connectors {
connector, err := d.client.GetConnectorByNameOrID(ctx, cc.Name.String)
if err != nil {
return err
}
for _, ff := range app.Functions {
function, err := d.client.GetFunction(ctx, ff.UUID.String)
if err != nil {
return err
}

// Include logs
resp, err := d.client.GetFunctionLogs(ctx, ff.Name.String)
if err != nil {
return err
}

buf := new(bytes.Buffer)
_, err = buf.ReadFrom(resp.Body)
if err != nil {
return err
}

function.Logs = buf.String()

functions = append(functions, function)
connectors = append(connectors, connector)
}
for _, ff := range app.Functions {
function, err := d.client.GetFunction(ctx, ff.Name.String)
if err != nil {
return err
}

output = utils.AppExtendedTable(app, resources, connectors, functions)
} else {
output = utils.AppTable(app)
functions = append(functions, function)
}

output = display.AppTable(app, resources, connectors, functions)

d.logger.Info(ctx, output)
d.logger.JSON(ctx, app)

Expand All @@ -144,7 +115,7 @@ func (d *Describe) Logger(logger log.Logger) {

func (d *Describe) ParseArgs(args []string) error {
if len(args) < 1 {
return errors.New("requires app name")
return errors.New("requires app name or UUID")
}

d.args.NameOrUUID = args[0]
Expand Down
93 changes: 41 additions & 52 deletions cmd/meroxa/root/apps/describe_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright © 2021 Meroxa Inc
Copyright © 2022 Meroxa Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,8 @@ import (
"strings"
"testing"

"github.com/meroxa/cli/utils/display"

"github.com/volatiletech/null/v8"

"github.com/golang/mock/gomock"
Expand All @@ -39,7 +41,7 @@ func TestDescribeApplicationArgs(t *testing.T) {
err error
name string
}{
{args: nil, err: errors.New("requires app name"), name: ""},
{args: nil, err: errors.New("requires app name or UUID"), name: ""},
{args: []string{"ApplicationName"}, err: nil, name: "ApplicationName"},
}

Expand Down Expand Up @@ -68,66 +70,53 @@ func TestDescribeApplicationExecution(t *testing.T) {
a := utils.GenerateApplication("")
a.Name = appName

client.
EXPECT().
GetApplication(
ctx,
a.Name,
).
Return(&a, nil)

dc := &Describe{
client: client,
logger: logger,
a.Resources = []meroxa.ApplicationResource{
{
EntityIdentifier: meroxa.EntityIdentifier{
Name: null.StringFrom("res1"),
},
Collection: meroxa.ResourceCollection{
Name: null.StringFrom("res1"),
Source: null.StringFrom("source"),
},
},
{
EntityIdentifier: meroxa.EntityIdentifier{
Name: null.StringFrom("res2"),
},
Collection: meroxa.ResourceCollection{
Name: null.StringFrom("res2"),
Destination: null.StringFrom("destination"),
},
},
}
dc.args.NameOrUUID = a.Name

err := dc.Execute(ctx)
if err != nil {
t.Fatalf("not expected error, got %q", err.Error())
resources := []*meroxa.Resource{
{Name: "res1", UUID: "abc-def", Type: meroxa.ResourceTypePostgres},
{Name: "res2", UUID: "abc-def", Type: meroxa.ResourceTypeBigquery},
}

gotLeveledOutput := logger.LeveledOutput()
wantLeveledOutput := utils.AppTable(&a)

if !strings.Contains(gotLeveledOutput, wantLeveledOutput) {
t.Fatalf("expected output:\n%s\ngot:\n%s", wantLeveledOutput, gotLeveledOutput)
a.Connectors = []meroxa.EntityIdentifier{
{Name: null.StringFrom("conn1")},
{Name: null.StringFrom("conn2")},
}

gotJSONOutput := logger.JSONOutput()
var gotApp meroxa.Application
err = json.Unmarshal([]byte(gotJSONOutput), &gotApp)
if err != nil {
t.Fatalf("not expected error, got %q", err.Error())
connectors := []*meroxa.Connector{
{Name: "conn1", ResourceName: "res1", Type: meroxa.ConnectorTypeSource, State: meroxa.ConnectorStateRunning},
{Name: "conn2", ResourceName: "res2", Type: meroxa.ConnectorTypeDestination, State: meroxa.ConnectorStateRunning},
}

if !reflect.DeepEqual(gotApp, a) {
t.Fatalf("expected \"%v\", got \"%v\"", a, gotApp)
functions := []*meroxa.Function{
{Name: "fun1", UUID: "abc-def", Status: meroxa.FunctionStatus{State: "running"}},
}
}

func TestDescribeApplicationExecutionWithFunctions(t *testing.T) {
ctx := context.Background()
ctrl := gomock.NewController(t)
client := mock.NewMockClient(ctrl)
logger := log.NewTestLogger()

appName := "my-app-with-funcs"

a := utils.GenerateApplication("")
a.Name = appName
a.Functions = []meroxa.EntityIdentifier{
{Name: null.StringFrom("fun1")},
{Name: null.StringFrom("fun2")},
}

client.
EXPECT().
GetApplication(
ctx,
a.Name,
).
Return(&a, nil)
client.EXPECT().GetApplication(ctx, a.Name).Return(&a, nil)
client.EXPECT().GetResourceByNameOrID(ctx, "res1").Return(resources[0], nil)
client.EXPECT().GetResourceByNameOrID(ctx, "res2").Return(resources[1], nil)
client.EXPECT().GetConnectorByNameOrID(ctx, "conn1").Return(connectors[0], nil)
client.EXPECT().GetConnectorByNameOrID(ctx, "conn2").Return(connectors[1], nil)
client.EXPECT().GetFunction(ctx, "fun1").Return(functions[0], nil)

dc := &Describe{
client: client,
Expand All @@ -141,7 +130,7 @@ func TestDescribeApplicationExecutionWithFunctions(t *testing.T) {
}

gotLeveledOutput := logger.LeveledOutput()
wantLeveledOutput := utils.AppTable(&a)
wantLeveledOutput := display.AppTable(&a, resources, connectors, functions)

if !strings.Contains(gotLeveledOutput, wantLeveledOutput) {
t.Fatalf("expected output:\n%s\ngot:\n%s", wantLeveledOutput, gotLeveledOutput)
Expand Down
7 changes: 4 additions & 3 deletions cmd/meroxa/root/apps/list.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright © 2021 Meroxa Inc
Copyright © 2022 Meroxa Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -19,9 +19,10 @@ package apps
import (
"context"

"github.com/meroxa/cli/utils/display"

"github.com/meroxa/cli/cmd/meroxa/builder"
"github.com/meroxa/cli/log"
"github.com/meroxa/cli/utils"
"github.com/meroxa/meroxa-go/pkg/meroxa"
)

Expand Down Expand Up @@ -67,7 +68,7 @@ func (l *List) Execute(ctx context.Context) error {
}

l.logger.JSON(ctx, apps)
l.logger.Info(ctx, utils.AppsTable(apps, l.hideHeaders))
l.logger.Info(ctx, display.AppsTable(apps, l.hideHeaders))

return nil
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/meroxa/root/apps/list_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright © 2021 Meroxa Inc
Copyright © 2022 Meroxa Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -23,9 +23,10 @@ import (
"strings"
"testing"

"github.com/meroxa/cli/utils/display"

"github.com/golang/mock/gomock"
"github.com/meroxa/cli/log"
"github.com/meroxa/cli/utils"
"github.com/meroxa/meroxa-go/pkg/meroxa"
"github.com/meroxa/meroxa-go/pkg/mock"
)
Expand Down Expand Up @@ -62,7 +63,7 @@ func TestListAppsExecution(t *testing.T) {
}

gotLeveledOutput := logger.LeveledOutput()
wantLeveledOutput := utils.AppsTable(apps, false)
wantLeveledOutput := display.AppsTable(apps, false)

if !strings.Contains(gotLeveledOutput, wantLeveledOutput) {
t.Fatalf("expected output:\n%s\ngot:\n%s", wantLeveledOutput, gotLeveledOutput)
Expand Down
Loading

0 comments on commit 3ec62b1

Please sign in to comment.