Skip to content

Commit

Permalink
Meroxa builds describe (#297)
Browse files Browse the repository at this point in the history
* Build describe and logs commands
  • Loading branch information
ericcheatham authored Apr 4, 2022
1 parent 110bf8b commit c98c955
Show file tree
Hide file tree
Showing 57 changed files with 23,647 additions and 26 deletions.
1 change: 1 addition & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Fixes <GitHub Issue>

- [ ] Unit Tests
- [ ] Tested in staging
- [ ] Tested in minikube

## Demo

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.43.0
version: v1.45.2
64 changes: 64 additions & 0 deletions cmd/meroxa/root/builds/builds.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
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.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package builds

import (
"fmt"

"github.com/spf13/cobra"

"github.com/meroxa/cli/cmd/meroxa/builder"
)

var (
_ builder.CommandWithDocs = (*Builds)(nil)
_ builder.CommandWithAliases = (*Builds)(nil)
_ builder.CommandWithHidden = (*Builds)(nil)
_ builder.CommandWithFeatureFlag = (*Builds)(nil)
_ builder.CommandWithSubCommands = (*Builds)(nil)
)

type Builds struct{}

func (o *Builds) Usage() string {
return "builds"
}

func (*Builds) Aliases() []string {
return []string{"build"}
}

func (*Builds) Hidden() bool {
return true
}

func (*Builds) FeatureFlag() (string, error) {
return "turbine", fmt.Errorf("no access to the Meroxa Data Processes feature")
}

func (o *Builds) Docs() builder.Docs {
return builder.Docs{
Short: "Manage Process builds on Meroxa",
}
}

func (o *Builds) SubCommands() []*cobra.Command {
return []*cobra.Command{
builder.BuildCobraCommand(&Describe{}),
builder.BuildCobraCommand(&Logs{}),
}
}
87 changes: 87 additions & 0 deletions cmd/meroxa/root/builds/describe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
Copyright © 2021 Meroxa Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package builds

import (
"context"
"errors"

"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.CommandWithClient = (*Describe)(nil)
_ builder.CommandWithLogger = (*Describe)(nil)
_ builder.CommandWithExecute = (*Describe)(nil)
)

type describeBuildClient interface {
GetBuild(ctx context.Context, uuid string) (*meroxa.Build, error)
}

type Describe struct {
client describeBuildClient
logger log.Logger

args struct {
UUID string
}
}

func (d *Describe) Usage() string {
return "describe [UUID]"
}

func (d *Describe) Docs() builder.Docs {
return builder.Docs{
Short: "Describe a Meroxa Process Build",
}
}

func (d *Describe) Execute(ctx context.Context) error {
build, err := d.client.GetBuild(ctx, d.args.UUID)
if err != nil {
return err
}

d.logger.Info(ctx, utils.BuildTable(build))
d.logger.JSON(ctx, build)

return nil
}

func (d *Describe) Client(client meroxa.Client) {
d.client = client
}

func (d *Describe) Logger(logger log.Logger) {
d.logger = logger
}

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

d.args.UUID = args[0]
return nil
}
102 changes: 102 additions & 0 deletions cmd/meroxa/root/builds/describe_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
Copyright © 2021 Meroxa Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package builds

import (
"context"
"encoding/json"
"errors"
"reflect"
"strings"
"testing"

"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"
)

func TestDescribeBuildsArgs(t *testing.T) {
tests := []struct {
args []string
err error
name string
}{
{args: nil, err: errors.New("requires build UUID"), name: ""},
{args: []string{"BuildUUID"}, err: nil, name: "BuildUUID"},
}

for _, tt := range tests {
ar := &Describe{}
err := ar.ParseArgs(tt.args)

if err != nil && tt.err.Error() != err.Error() {
t.Fatalf("expected \"%s\" got \"%s\"", tt.err, err)
}

if tt.name != ar.args.UUID {
t.Fatalf("expected \"%s\" got \"%s\"", tt.name, ar.args.UUID)
}
}
}

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

a := utils.GenerateBuild()

client.
EXPECT().
GetBuild(
ctx,
a.Uuid,
).
Return(&a, nil)

dc := &Describe{
client: client,
logger: logger,
}
dc.args.UUID = a.Uuid

err := dc.Execute(ctx)
if err != nil {
t.Fatalf("not expected error, got %q", err.Error())
}

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

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

gotJSONOutput := logger.JSONOutput()
var gotBuild meroxa.Build
err = json.Unmarshal([]byte(gotJSONOutput), &gotBuild)
if err != nil {
t.Fatalf("not expected error, got %q", err.Error())
}

if !reflect.DeepEqual(gotBuild, a) {
t.Fatalf("expected \"%v\", got \"%v\"", a, gotBuild)
}
}
93 changes: 93 additions & 0 deletions cmd/meroxa/root/builds/logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
Copyright © 2021 Meroxa Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package builds

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

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

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

type buildLogsClient interface {
GetBuildLogs(ctx context.Context, uuid string) (*http.Response, error)
}

type Logs struct {
client buildLogsClient
logger log.Logger

args struct {
UUID string
}
}

func (d *Logs) Usage() string {
return "logs [UUID]"
}

func (d *Logs) Docs() builder.Docs {
return builder.Docs{
Short: "List a Meroxa Process Build's Logs",
}
}

func (d *Logs) Execute(ctx context.Context) error {
response, err := d.client.GetBuildLogs(ctx, d.args.UUID)
if err != nil {
return err
}
defer response.Body.Close()

body, err := io.ReadAll(response.Body)
if err != nil {
return err
}

d.logger.Info(ctx, string(body))

return nil
}

func (d *Logs) Client(client meroxa.Client) {
d.client = client
}

func (d *Logs) Logger(logger log.Logger) {
d.logger = logger
}

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

d.args.UUID = args[0]
return nil
}
Loading

0 comments on commit c98c955

Please sign in to comment.