Skip to content

Commit

Permalink
Add apps open command (#654)
Browse files Browse the repository at this point in the history
  • Loading branch information
janelletavares authored Mar 17, 2023
1 parent 2e9ec86 commit f2e292b
Show file tree
Hide file tree
Showing 61 changed files with 495 additions and 53 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 @@ -62,6 +62,7 @@ func (*Apps) SubCommands() []*cobra.Command {
builder.BuildCobraCommand(&Init{}),
builder.BuildCobraCommand(&List{}),
builder.BuildCobraCommand(&Logs{}),
builder.BuildCobraCommand(&Open{}),
builder.BuildCobraCommand(&Remove{}),
builder.BuildCobraCommand(&Run{}),
builder.BuildCobraCommand(&Upgrade{}),
Expand Down
2 changes: 1 addition & 1 deletion cmd/meroxa/root/apps/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (d *Describe) Execute(ctx context.Context) error {
var turbineLibVersion string
nameOrUUID := d.args.NameOrUUID
if nameOrUUID != "" && d.flags.Path != "" {
return fmt.Errorf("supply either NamrOrUUID argument or path flag")
return fmt.Errorf("supply either NameOrUUID argument or --path flag")
}

if nameOrUUID == "" {
Expand Down
2 changes: 1 addition & 1 deletion cmd/meroxa/root/apps/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (l *Logs) Execute(ctx context.Context) error {
var turbineLibVersion string
nameOrUUID := l.args.NameOrUUID
if nameOrUUID != "" && l.flags.Path != "" {
return fmt.Errorf("supply either NamrOrUUID argument or path flag")
return fmt.Errorf("supply either NameOrUUID argument or --path flag")
}

if nameOrUUID == "" {
Expand Down
106 changes: 106 additions & 0 deletions cmd/meroxa/root/apps/open.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
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 apps

import (
"context"
"fmt"

"github.com/skratchdot/open-golang/open"

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

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

type Open struct {
logger log.Logger
path string

args struct {
NameOrUUID string
}
flags struct {
Path string `long:"path" usage:"Path to the app directory (default is local directory)"`
}
}

func (o *Open) Usage() string {
return "open [--path pwd]"
}

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

func (o *Open) ParseArgs(args []string) error {
if len(args) > 0 {
o.args.NameOrUUID = args[0]
}

return nil
}

func (o *Open) Docs() builder.Docs {
return builder.Docs{
Short: "Open the link to a Turbine Data Application in the Dashboard",
Example: `meroxa apps open # assumes that the Application is in the current directory
meroxa apps open --path /my/app
meroxa apps open NAMEorUUID`,
Beta: true,
}
}

func (o *Open) Execute(ctx context.Context) error {
nameOrUUID := o.args.NameOrUUID
if nameOrUUID != "" && o.flags.Path != "" {
return fmt.Errorf("supply either NameOrUUID argument or --path flag")
}

if nameOrUUID == "" {
var err error
if o.path, err = turbine.GetPath(o.flags.Path); err != nil {
return err
}

config, err := turbine.ReadConfigFile(o.path)
if err != nil {
return err
}
nameOrUUID = config.Name
}

// open a browser window to the application details
dashboardURL := fmt.Sprintf("https://dashboard.meroxa.io/apps/%s/detail", nameOrUUID)
err := open.Start(dashboardURL)
if err != nil {
o.logger.Errorf(ctx, "can't open browser to URL %s\n", dashboardURL)
}
return err
}

func (o *Open) Logger(logger log.Logger) {
o.logger = logger
}
161 changes: 161 additions & 0 deletions cmd/meroxa/root/apps/open_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/*
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 apps

import (
"context"
"os"
"path/filepath"
"strings"
"testing"

"github.com/google/uuid"

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

func TestOpenAppArgs(t *testing.T) {
tests := []struct {
args []string
err error
appName string
}{
{args: []string{"my-app-name"}, err: nil, appName: "my-app-name"},
}

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

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

if tt.appName != cc.args.NameOrUUID {
t.Fatalf("expected \"%s\" got \"%s\"", tt.appName, cc.args.NameOrUUID)
}
}
}

func TestOpenAppFlags(t *testing.T) {
expectedFlags := []struct {
name string
required bool
shorthand string
hidden bool
}{
{name: "path", required: false},
}

c := builder.BuildCobraCommand(&Open{})

for _, f := range expectedFlags {
cf := c.Flags().Lookup(f.name)
if cf == nil {
t.Fatalf("expected flag \"%s\" to be present", f.name)
}

if f.shorthand != cf.Shorthand {
t.Fatalf("expected shorthand \"%s\" got \"%s\" for flag \"%s\"", f.shorthand, cf.Shorthand, f.name)
}

if f.required && !utils.IsFlagRequired(cf) {
t.Fatalf("expected flag \"%s\" to be required", f.name)
}

if cf.Hidden != f.hidden {
if cf.Hidden {
t.Fatalf("expected flag \"%s\" not to be hidden", f.name)
} else {
t.Fatalf("expected flag \"%s\" to be hidden", f.name)
}
}
}
}

func TestOpenAppExecution(t *testing.T) {
ctx := context.Background()

testCases := []struct {
desc string
appArg string
appFlag string
errSubstring string
}{
{
desc: "Successfully open app link with arg",
appArg: "app-name",
},
{
desc: "Successfully open app link with flag",
},
{
desc: "Fail with bad path",
appFlag: "/tmp",
errSubstring: "could not find an app.json file on path",
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
os.Setenv("UNIT_TEST", "1")
path := filepath.Join("/tmp", uuid.New().String())
logger := log.NewTestLogger()
cc := &Init{}
cc.Logger(logger)
cc.flags.Path = path
cc.flags.Lang = "golang"
if tc.appArg != "" {
cc.args.appName = tc.appArg
if tc.appFlag == "" {
tc.appFlag = path + "/" + tc.appArg
}
} else {
cc.args.appName = "my-app"
if tc.appFlag == "" {
tc.appFlag = path + "/my-app"
}
}

err := cc.Execute(context.Background())
if err != nil {
t.Fatalf("unexpected error \"%s\"", err)
}

o := &Open{
logger: logger,
}
if tc.appArg != "" {
o.args = struct {
NameOrUUID string
}{NameOrUUID: tc.appArg}
} else if tc.appFlag != "" {
o.flags = struct {
Path string `long:"path" usage:"Path to the app directory (default is local directory)"`
}{Path: tc.appFlag}
}

err = o.Execute(ctx)
if tc.errSubstring == "" && err != nil {
t.Fatalf("not expected error, got \"%s\"", err.Error())
} else if err != nil && !strings.Contains(err.Error(), tc.errSubstring) {
t.Fatalf("failed to find expected error output(%s):\n%s", tc.errSubstring, err.Error())
}
})
}
}
2 changes: 1 addition & 1 deletion cmd/meroxa/root/apps/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (r *Remove) Execute(ctx context.Context) error {
var turbineLibVersion string
nameOrUUID := r.args.NameOrUUID
if nameOrUUID != "" && r.flags.Path != "" {
return fmt.Errorf("supply either NamrOrUUID argument or path flag")
return fmt.Errorf("supply either NameOrUUID argument or --path flag")
}

if nameOrUUID == "" {
Expand Down
1 change: 1 addition & 0 deletions docs/cmd/md/meroxa_apps.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Manage Turbine Data Applications (Beta)
* [meroxa apps init](meroxa_apps_init.md) - Initialize a Turbine Data Application (Beta)
* [meroxa apps list](meroxa_apps_list.md) - List Turbine Data Applications (Beta)
* [meroxa apps logs](meroxa_apps_logs.md) - View relevant logs to the state of the given Turbine Data Application (Beta)
* [meroxa apps open](meroxa_apps_open.md) - Open the link to a Turbine Data Application in the Dashboard (Beta)
* [meroxa apps remove](meroxa_apps_remove.md) - Remove a Turbine Data Application (Beta)
* [meroxa apps run](meroxa_apps_run.md) - Execute a Turbine Data Application locally (Beta)
* [meroxa apps upgrade](meroxa_apps_upgrade.md) - Upgrade a Turbine Data Application (Beta)
Expand Down
36 changes: 36 additions & 0 deletions docs/cmd/md/meroxa_apps_open.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
## meroxa apps open

Open the link to a Turbine Data Application in the Dashboard (Beta)

```
meroxa apps open [--path pwd] [flags]
```

### Examples

```
meroxa apps open # assumes that the Application is in the current directory
meroxa apps open --path /my/app
meroxa apps open NAMEorUUID
```

### Options

```
-h, --help help for open
--path string Path to the app directory (default is local directory)
```

### Options inherited from parent commands

```
--cli-config-file string meroxa configuration file
--debug display any debugging information
--json output json
--timeout duration set the duration of the client timeout in seconds (default 10s)
```

### SEE ALSO

* [meroxa apps](meroxa_apps.md) - Manage Turbine Data Applications (Beta)

43 changes: 43 additions & 0 deletions docs/cmd/www/meroxa-apps-open.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
createdAt:
updatedAt:
title: "meroxa apps open"
slug: meroxa-apps-open
url: /cli/cmd/meroxa-apps-open/
---
## meroxa apps open

Open the link to a Turbine Data Application in the Dashboard (Beta)

```
meroxa apps open [--path pwd] [flags]
```

### Examples

```
meroxa apps open # assumes that the Application is in the current directory
meroxa apps open --path /my/app
meroxa apps open NAMEorUUID
```

### Options

```
-h, --help help for open
--path string Path to the app directory (default is local directory)
```

### Options inherited from parent commands

```
--cli-config-file string meroxa configuration file
--debug display any debugging information
--json output json
--timeout duration set the duration of the client timeout in seconds (default 10s)
```

### SEE ALSO

* [meroxa apps](/cli/cmd/meroxa-apps/) - Manage Turbine Data Applications (Beta)

Loading

0 comments on commit f2e292b

Please sign in to comment.