Skip to content

Commit

Permalink
feat(env): Add describe and remove cmds (#202)
Browse files Browse the repository at this point in the history
* feat(env): Add describe cmd

* feat(env): Add delete cmd

* feat: upate describe display

* feat: update wording on `remove`

* feat(env): Add remove cmd

* chore: use latest meroxa-go

* feat: use environment removed

Takes advantage of meroxa/meroxa-go#65

* chore: use latest meroxa-go
  • Loading branch information
raulb authored Oct 20, 2021
1 parent 1f6dfab commit cf9e35e
Show file tree
Hide file tree
Showing 13 changed files with 644 additions and 4 deletions.
88 changes: 88 additions & 0 deletions cmd/meroxa/root/environments/describe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
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 environments

import (
"context"
"errors"

"github.com/meroxa/cli/utils"

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

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

type describeEnvironmentClient interface {
GetEnvironment(ctx context.Context, nameOrUUID string) (*meroxa.Environment, error)
}

type Describe struct {
client describeEnvironmentClient
logger log.Logger

args struct {
Name string
}
}

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

func (d *Describe) Docs() builder.Docs {
return builder.Docs{
Short: "Describe environment",
}
}

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

d.logger.Info(ctx, utils.EnvironmentTable(environment))
d.logger.JSON(ctx, environment)

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 environment name")
}

d.args.Name = args[0]
return nil
}
105 changes: 105 additions & 0 deletions cmd/meroxa/root/environments/describe_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
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 environments

import (
"context"
"encoding/json"
"errors"

"github.com/golang/mock/gomock"
"github.com/meroxa/cli/log"
mock "github.com/meroxa/cli/mock-cmd"
"github.com/meroxa/cli/utils"
"github.com/meroxa/meroxa-go"

"reflect"
"strings"
"testing"
)

func TestDescribeEnvironmentArgs(t *testing.T) {
tests := []struct {
args []string
err error
name string
}{
{args: nil, err: errors.New("requires environment name"), name: ""},
{args: []string{"environmentName"}, err: nil, name: "environmentName"},
}

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.Name {
t.Fatalf("expected \"%s\" got \"%s\"", tt.name, ar.args.Name)
}
}
}

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

environmentName := "my-env"

e := utils.GenerateEnvironment(environmentName)

client.
EXPECT().
GetEnvironment(
ctx,
e.Name,
).
Return(&e, nil)

dc := &Describe{
client: client,
logger: logger,
}
dc.args.Name = e.Name

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

gotLeveledOutput := logger.LeveledOutput()
wantLeveledOutput := utils.EnvironmentTable(&e)

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

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

if !reflect.DeepEqual(gotEnvironment, e) {
t.Fatalf("expected \"%v\", got \"%v\"", e, gotEnvironment)
}
}
2 changes: 2 additions & 0 deletions cmd/meroxa/root/environments/environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ func (e *Environments) Logger(logger log.Logger) {

func (*Environments) SubCommands() []*cobra.Command {
return []*cobra.Command{
builder.BuildCobraCommand(&Describe{}),
builder.BuildCobraCommand(&List{}),
builder.BuildCobraCommand(&Remove{}),
}
}
98 changes: 98 additions & 0 deletions cmd/meroxa/root/environments/remove.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
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 environments

import (
"context"
"errors"

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

var (
_ builder.CommandWithDocs = (*Remove)(nil)
_ builder.CommandWithAliases = (*Remove)(nil)
_ builder.CommandWithArgs = (*Remove)(nil)
_ builder.CommandWithClient = (*Remove)(nil)
_ builder.CommandWithLogger = (*Remove)(nil)
_ builder.CommandWithExecute = (*Remove)(nil)
_ builder.CommandWithConfirm = (*Remove)(nil)
)

type removeEnvironmentClient interface {
DeleteEnvironment(ctx context.Context, nameOrUUID string) (*meroxa.Environment, error)
}

type Remove struct {
client removeEnvironmentClient
logger log.Logger

args struct {
Name string
}
}

func (r *Remove) Usage() string {
return "remove NAME"
}

func (r *Remove) Docs() builder.Docs {
return builder.Docs{
Short: "Remove environment",
}
}

func (r *Remove) Confirm(_ context.Context) (wantInput string) {
return r.args.Name
}

func (r *Remove) Execute(ctx context.Context) error {
r.logger.Infof(ctx, "Environment %q is being removed...", r.args.Name)

e, err := r.client.DeleteEnvironment(ctx, r.args.Name)
if err != nil {
return err
}

r.logger.Infof(ctx, "Run `meroxa env describe %s` for status.", r.args.Name)
r.logger.JSON(ctx, e)

return nil
}

func (r *Remove) Logger(logger log.Logger) {
r.logger = logger
}

func (r *Remove) Client(client *meroxa.Client) {
r.client = client
}

func (r *Remove) ParseArgs(args []string) error {
if len(args) < 1 {
return errors.New("requires environment name")
}

r.args.Name = args[0]
return nil
}

func (r *Remove) Aliases() []string {
return []string{"rm", "delete"}
}
Loading

0 comments on commit cf9e35e

Please sign in to comment.