Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(env): Add list cmd #200

Merged
merged 4 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/meroxa/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ func buildCommandWithFeatureFlag(cmd *cobra.Command, c Command) {
userFeatureFlags := global.Config.GetStringSlice(global.UserFeatureFlagsEnv)

if !hasFeatureFlag(userFeatureFlags, flagRequired) {
return fmt.Errorf("your account does not have access to the %q feature."+
return fmt.Errorf("your account does not have access to the %q feature. "+
"Reach out to [email protected] for more information", flagRequired)
}

Expand Down
12 changes: 6 additions & 6 deletions cmd/meroxa/root/environments/environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ limitations under the License.
package environments

import (
"context"
"fmt"
"github.com/spf13/cobra"

"github.com/meroxa/cli/cmd/meroxa/builder"
"github.com/meroxa/cli/log"
Expand All @@ -31,8 +30,8 @@ type Environments struct {
var (
_ builder.CommandWithAliases = (*Environments)(nil)
_ builder.CommandWithDocs = (*Environments)(nil)
_ builder.CommandWithExecute = (*Environments)(nil)
_ builder.CommandWithFeatureFlag = (*Environments)(nil)
_ builder.CommandWithSubCommands = (*Environments)(nil)
)

func (*Environments) Usage() string {
Expand All @@ -57,7 +56,8 @@ func (e *Environments) Logger(logger log.Logger) {
e.logger = logger
}

func (e *Environments) Execute(ctx context.Context) error {
fmt.Println("Welcome to a new world")
return nil
func (*Environments) SubCommands() []*cobra.Command {
return []*cobra.Command{
builder.BuildCobraCommand(&List{}),
}
}
84 changes: 84 additions & 0 deletions cmd/meroxa/root/environments/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
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"

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

var (
_ builder.CommandWithDocs = (*List)(nil)
_ builder.CommandWithClient = (*List)(nil)
_ builder.CommandWithLogger = (*List)(nil)
_ builder.CommandWithExecute = (*List)(nil)
_ builder.CommandWithAliases = (*List)(nil)
_ builder.CommandWithNoHeaders = (*List)(nil)
)

type listEnvironmentsClient interface {
ListEnvironments(ctx context.Context) ([]*meroxa.Environment, error)
}

type List struct {
client listEnvironmentsClient
logger log.Logger
hideHeaders bool
}

func (l *List) Usage() string {
return "list"
}

func (l *List) Docs() builder.Docs {
return builder.Docs{
Short: "List environments",
}
}

func (l *List) Aliases() []string {
return []string{"ls"}
}

func (l *List) Execute(ctx context.Context) error {
var err error
environments, err := l.client.ListEnvironments(ctx)
if err != nil {
return err
}

l.logger.JSON(ctx, environments)
l.logger.Info(ctx, utils.EnvironmentsTable(environments, l.hideHeaders))

return nil
}

func (l *List) Logger(logger log.Logger) {
l.logger = logger
}

func (l *List) Client(client *meroxa.Client) {
l.client = client
}

func (l *List) HideHeaders(hide bool) {
l.hideHeaders = hide
}
90 changes: 90 additions & 0 deletions cmd/meroxa/root/environments/list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
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"
"reflect"
"strings"
"testing"

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

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

ee := &meroxa.Environment{
Type: "dedicated",
Name: "environment-1234",
Provider: "aws",
Region: "aws:us-east",
Status: meroxa.EnvironmentStatus{State: "provisioned"},
UUID: "531428f7-4e86-4094-8514-d397d49026f7",
}

environments := []*meroxa.Environment{ee}

client.
EXPECT().
ListEnvironments(ctx).
Return(environments, nil)

l := &List{
client: client,
logger: logger,
}

err := l.Execute(ctx)

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

gotLeveledOutput := logger.LeveledOutput()
wantLeveledOutput := utils.EnvironmentsTable(environments, false)

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

gotJSONOutput := logger.JSONOutput()
var gotEnvironments []meroxa.Environment
err = json.Unmarshal([]byte(gotJSONOutput), &gotEnvironments)

var lp []meroxa.Environment

for _, p := range environments {
lp = append(lp, *p)
}

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

if !reflect.DeepEqual(gotEnvironments, lp) {
t.Fatalf("expected \"%v\", got \"%v\"", environments, gotEnvironments)
}
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/gorilla/mux v1.7.3
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-runewidth v0.0.10 // indirect
github.com/meroxa/meroxa-go v0.0.0-20210928081857-bcbce33ea9f4
github.com/meroxa/meroxa-go v0.0.0-20211013160423-7447f282edb1
github.com/nirasan/go-oauth-pkce-code-verifier v0.0.0-20170819232839-0fbfe93532da
github.com/pkg/browser v0.0.0-20210115035449-ce105d075bb4
github.com/rivo/uniseg v0.2.0 // indirect
Expand All @@ -20,7 +20,7 @@ require (
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.8.1
golang.org/x/net v0.0.0-20210614182718-04defd469f4e // indirect
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f
golang.org/x/oauth2 v0.0.0-20211005180243-6b3c2da341f1
)

require (
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHX
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-runewidth v0.0.10 h1:CoZ3S2P7pvtP45xOtBw+/mDL2z0RKI576gSkzRRpdGg=
github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/meroxa/meroxa-go v0.0.0-20210928081857-bcbce33ea9f4 h1:7jpUh3WZ9r3vZ0mYdCENg1/F15wkWi/AqkXmZYLsAPk=
github.com/meroxa/meroxa-go v0.0.0-20210928081857-bcbce33ea9f4/go.mod h1:gGKULnbPeFk//HW3XRxkZwZ3jgMuoMB+43EiGZ7QoGU=
github.com/meroxa/meroxa-go v0.0.0-20211013160423-7447f282edb1 h1:NNyXbCGgxkLkeV9GSVFoSGU1pu1+A78inUhLcooSbOM=
github.com/meroxa/meroxa-go v0.0.0-20211013160423-7447f282edb1/go.mod h1:gGKULnbPeFk//HW3XRxkZwZ3jgMuoMB+43EiGZ7QoGU=
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
Expand Down Expand Up @@ -388,8 +388,8 @@ golang.org/x/oauth2 v0.0.0-20210220000619-9bb904979d93/go.mod h1:KelEdhl1UZF7XfJ
golang.org/x/oauth2 v0.0.0-20210313182246-cd4f82c27b84/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/oauth2 v0.0.0-20210402161424-2e8d93401602/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/oauth2 v0.0.0-20210413134643-5e61552d6c78/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f h1:Qmd2pbz05z7z6lm0DrgQVVPuBm92jqujBKMHMOlOQEw=
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/oauth2 v0.0.0-20211005180243-6b3c2da341f1 h1:B333XXssMuKQeBwiNODx4TupZy7bf4sxFZnN2ZOcvUE=
golang.org/x/oauth2 v0.0.0-20211005180243-6b3c2da341f1/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down
51 changes: 51 additions & 0 deletions mock-cmd/list_environments.go

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

39 changes: 39 additions & 0 deletions utils/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,45 @@ func PrintPipelinesTable(pipelines []*meroxa.Pipeline, hideHeaders bool) {
fmt.Println(PipelinesTable(pipelines, hideHeaders))
}

func EnvironmentsTable(environments []*meroxa.Environment, hideHeaders bool) string {
if len(environments) != 0 {
table := simpletable.New()

if !hideHeaders {
table.Header = &simpletable.Header{
Cells: []*simpletable.Cell{
{Align: simpletable.AlignCenter, Text: "UUID"},
{Align: simpletable.AlignCenter, Text: "NAME"},
{Align: simpletable.AlignCenter, Text: "TYPE"},
{Align: simpletable.AlignCenter, Text: "PROVIDER"},
{Align: simpletable.AlignCenter, Text: "REGION"},
{Align: simpletable.AlignCenter, Text: "STATE"},
},
}
}

for _, p := range environments {
r := []*simpletable.Cell{
{Align: simpletable.AlignRight, Text: p.UUID},
{Align: simpletable.AlignCenter, Text: p.Name},
{Align: simpletable.AlignCenter, Text: p.Type},
{Align: simpletable.AlignCenter, Text: p.Provider},
{Align: simpletable.AlignCenter, Text: p.Region},
{Align: simpletable.AlignCenter, Text: p.Status.State},
}

table.Body.Cells = append(table.Body.Cells, r)
}
table.SetStyle(simpletable.StyleCompact)
return table.String()
}
return ""
}

func PrintEnvironmentsTable(environments []*meroxa.Environment, hideHeaders bool) {
fmt.Println(EnvironmentsTable(environments, hideHeaders))
}

func truncateString(oldString string, l int) string {
str := oldString

Expand Down
Loading