Skip to content

Commit

Permalink
Add upgrade command for Application; start using mocks (#412)
Browse files Browse the repository at this point in the history
* Add upgrade command for Application; start using mocks
  • Loading branch information
janelletavares authored Aug 21, 2022
1 parent 7ef7607 commit c5783c0
Show file tree
Hide file tree
Showing 45 changed files with 1,035 additions and 256 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ endif
.PHONY: lint
lint:
docker run --rm -v $(CURDIR):/app -w /app golangci/golangci-lint:latest golangci-lint run --timeout 5m -v

.PHONY: mock
mock:
mockgen -source cmd/meroxa/turbine_cli/interface.go -package mock > cmd/meroxa/turbine_cli/mock/cli.go
1 change: 1 addition & 0 deletions cmd/meroxa/root/apps/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,6 @@ func (*Apps) SubCommands() []*cobra.Command {
builder.BuildCobraCommand(&Logs{}),
builder.BuildCobraCommand(&Remove{}),
builder.BuildCobraCommand(&Run{}),
builder.BuildCobraCommand(&Upgrade{}),
}
}
20 changes: 5 additions & 15 deletions cmd/meroxa/root/apps/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ import (
"github.com/coreos/go-semver/semver"
"github.com/meroxa/cli/cmd/meroxa/builder"
"github.com/meroxa/cli/cmd/meroxa/global"
turbineCLI "github.com/meroxa/cli/cmd/meroxa/turbine_cli"
turbineGo "github.com/meroxa/cli/cmd/meroxa/turbine_cli/golang"
turbineJS "github.com/meroxa/cli/cmd/meroxa/turbine_cli/javascript"
turbinePY "github.com/meroxa/cli/cmd/meroxa/turbine_cli/python"
turbineCLI "github.com/meroxa/cli/cmd/meroxa/turbine"
turbineGo "github.com/meroxa/cli/cmd/meroxa/turbine/golang"
turbineJS "github.com/meroxa/cli/cmd/meroxa/turbine/javascript"
turbinePY "github.com/meroxa/cli/cmd/meroxa/turbine/python"
"github.com/meroxa/cli/config"
"github.com/meroxa/cli/log"
"github.com/meroxa/meroxa-go/pkg/meroxa"
Expand Down Expand Up @@ -609,17 +609,7 @@ func (d *Deploy) prepareDeployment(ctx context.Context) error {

func (d *Deploy) rmBinary() {
if d.lang == GoLang {
localBinary := filepath.Join(d.path, d.appName)
err := os.Remove(localBinary)
if err != nil {
fmt.Printf("warning: failed to clean up %s\n", localBinary)
}

crossCompiledBinary := filepath.Join(d.path, d.appName) + ".cross"
err = os.Remove(crossCompiledBinary)
if err != nil {
fmt.Printf("warning: failed to clean up %s\n", crossCompiledBinary)
}
turbineGo.RunCleanup(d.path, d.appName)
}
}

Expand Down
26 changes: 13 additions & 13 deletions cmd/meroxa/root/apps/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/golang/mock/gomock"
"github.com/meroxa/cli/cmd/meroxa/builder"
turbineCLI "github.com/meroxa/cli/cmd/meroxa/turbine_cli"
"github.com/meroxa/cli/cmd/meroxa/turbine"
"github.com/meroxa/cli/config"
"github.com/meroxa/cli/log"
"github.com/meroxa/cli/utils"
Expand Down Expand Up @@ -368,13 +368,13 @@ func TestTearDownExistingResourcesWithAppNotFound(t *testing.T) {
func TestGetResourceCheckErrorMessage(t *testing.T) {
testCases := []struct {
name string
resources []turbineCLI.ApplicationResource
resources []turbine.ApplicationResource
resourceState string
expectedErrorMessage string
}{
{
name: "getResourceCheckErrorMessage returns an empty response if all resources are found and available",
resources: []turbineCLI.ApplicationResource{
resources: []turbine.ApplicationResource{
{
Name: "nozzle",
},
Expand All @@ -387,7 +387,7 @@ func TestGetResourceCheckErrorMessage(t *testing.T) {
},
{
name: "getResourceCheckErrorMessage returns an error response if resources are unavailable",
resources: []turbineCLI.ApplicationResource{
resources: []turbine.ApplicationResource{
{
Name: "nozzle",
},
Expand Down Expand Up @@ -438,12 +438,12 @@ func TestGetResourceCheckErrorMessage(t *testing.T) {
func TestValidateCollections(t *testing.T) {
testCases := []struct {
name string
resources []turbineCLI.ApplicationResource
resources []turbine.ApplicationResource
err string
}{
{
name: "Different source and destination resources reference different collections",
resources: []turbineCLI.ApplicationResource{
resources: []turbine.ApplicationResource{
{
Name: "source",
Source: true,
Expand All @@ -458,7 +458,7 @@ func TestValidateCollections(t *testing.T) {
},
{
name: "Different source and destination resources reference same collection",
resources: []turbineCLI.ApplicationResource{
resources: []turbine.ApplicationResource{
{
Name: "source",
Source: true,
Expand All @@ -473,7 +473,7 @@ func TestValidateCollections(t *testing.T) {
},
{
name: "Multiple destination resources",
resources: []turbineCLI.ApplicationResource{
resources: []turbine.ApplicationResource{
{
Name: "source",
Source: true,
Expand All @@ -493,7 +493,7 @@ func TestValidateCollections(t *testing.T) {
},
{
name: "Same source and destination resources reference same collection",
resources: []turbineCLI.ApplicationResource{
resources: []turbine.ApplicationResource{
{
Name: "pg",
Source: true,
Expand All @@ -509,7 +509,7 @@ func TestValidateCollections(t *testing.T) {
},
{
name: "One resource is both source and destination",
resources: []turbineCLI.ApplicationResource{
resources: []turbine.ApplicationResource{
{
Name: "source",
Source: true,
Expand All @@ -521,7 +521,7 @@ func TestValidateCollections(t *testing.T) {
},
{
name: "Destination resource used in another app",
resources: []turbineCLI.ApplicationResource{
resources: []turbine.ApplicationResource{
{
Name: "source",
Source: true,
Expand All @@ -538,7 +538,7 @@ func TestValidateCollections(t *testing.T) {
},
{
name: "Two same destination resources",
resources: []turbineCLI.ApplicationResource{
resources: []turbine.ApplicationResource{
{
Name: "source",
Source: true,
Expand All @@ -559,7 +559,7 @@ func TestValidateCollections(t *testing.T) {
},
{
name: "Ignore resources without collection info",
resources: []turbineCLI.ApplicationResource{
resources: []turbine.ApplicationResource{
{
Name: "source",
},
Expand Down
13 changes: 7 additions & 6 deletions cmd/meroxa/root/apps/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import (
"strings"

"github.com/meroxa/cli/cmd/meroxa/builder"
turbineCLI "github.com/meroxa/cli/cmd/meroxa/turbine_cli"
turbinejs "github.com/meroxa/cli/cmd/meroxa/turbine_cli/javascript"
turbinepy "github.com/meroxa/cli/cmd/meroxa/turbine_cli/python"
turbineCLI "github.com/meroxa/cli/cmd/meroxa/turbine"
utils "github.com/meroxa/cli/cmd/meroxa/turbine/golang"
turbinejs "github.com/meroxa/cli/cmd/meroxa/turbine/javascript"
turbinepy "github.com/meroxa/cli/cmd/meroxa/turbine/python"
"github.com/meroxa/cli/log"
turbine "github.com/meroxa/turbine-go/init"
turbinego "github.com/meroxa/turbine-go/init"
)

type Init struct {
Expand Down Expand Up @@ -129,13 +130,13 @@ func (i *Init) Execute(ctx context.Context) error {
i.logger.StartSpinner("\t", fmt.Sprintf("Initializing application %q in %q...", name, i.path))
switch lang {
case "go", GoLang:
err = turbine.Init(name, i.path)
err = turbinego.Init(name, i.path)
if err != nil {
i.logger.StopSpinnerWithStatus("\t", log.Failed)
return err
}
i.logger.StopSpinnerWithStatus("Application directory created!", log.Successful)
err = turbineCLI.GoInit(ctx, i.logger, i.path+"/"+name, i.flags.SkipModInit, i.flags.ModVendor)
err = utils.GoInit(i.logger, i.path+"/"+name, i.flags.SkipModInit, i.flags.ModVendor)
case "js", JavaScript, NodeJs:
err = turbinejs.Init(ctx, i.logger, name, i.path)
case "py", Python3, Python:
Expand Down
20 changes: 12 additions & 8 deletions cmd/meroxa/root/apps/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,15 @@ import (
"fmt"

"github.com/meroxa/cli/cmd/meroxa/builder"
turbineCLI "github.com/meroxa/cli/cmd/meroxa/turbine_cli"
turbineGo "github.com/meroxa/cli/cmd/meroxa/turbine_cli/golang"
turbineJS "github.com/meroxa/cli/cmd/meroxa/turbine_cli/javascript"
turbinepy "github.com/meroxa/cli/cmd/meroxa/turbine_cli/python"
turbineCLI "github.com/meroxa/cli/cmd/meroxa/turbine"
turbineGo "github.com/meroxa/cli/cmd/meroxa/turbine/golang"
turbineJS "github.com/meroxa/cli/cmd/meroxa/turbine/javascript"
turbinepy "github.com/meroxa/cli/cmd/meroxa/turbine/python"
"github.com/meroxa/cli/log"
)

type Run struct {
flags struct {
// `--lang` is not required unless language is not specified via app.json
Lang string `long:"lang" short:"l" usage:"language to use (go | js)"`
Path string `long:"path" usage:"path of application to run"`
}

Expand Down Expand Up @@ -75,14 +73,20 @@ func (r *Run) Execute(ctx context.Context) error {
if err != nil {
return err
}
lang, err := turbineCLI.GetLang(ctx, r.logger, r.flags.Lang, r.path)
lang, err := turbineCLI.GetLangFromAppJSON(ctx, r.logger, r.path)
if err != nil {
return err
}
appName, err := turbineCLI.GetAppNameFromAppJSON(ctx, r.logger, r.path)
if err != nil {
return err
}

switch lang {
case GoLang:
return turbineGo.Run(ctx, r.path, r.logger)
err = turbineGo.Run(ctx, r.path, r.logger)
turbineGo.RunCleanup(r.path, appName)
return err
case "js", JavaScript, NodeJs:
return turbineJS.Build(ctx, r.logger, r.path)
case "py", Python3, Python:
Expand Down
1 change: 0 additions & 1 deletion cmd/meroxa/root/apps/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ func TestRunAppFlags(t *testing.T) {
shorthand string
hidden bool
}{
{name: "lang", shorthand: "l"},
{name: "path", required: false},
}

Expand Down
132 changes: 132 additions & 0 deletions cmd/meroxa/root/apps/upgrade.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
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"
"strconv"

"github.com/meroxa/cli/cmd/meroxa/builder"
"github.com/meroxa/cli/cmd/meroxa/turbine"
turbineGo "github.com/meroxa/cli/cmd/meroxa/turbine/golang"
turbineJS "github.com/meroxa/cli/cmd/meroxa/turbine/javascript"
turbinePY "github.com/meroxa/cli/cmd/meroxa/turbine/python"
"github.com/meroxa/cli/log"
)

type Upgrade struct {
logger log.Logger
turbineCLI turbine.CLI
run builder.CommandWithExecute
path string

flags struct {
Path string `long:"path" usage:"path where application exists (current directory as default)"`
}
}

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

func (*Upgrade) Usage() string {
return "upgrade [APP_NAME] [--path pwd]"
}

func (*Upgrade) Docs() builder.Docs {
return builder.Docs{
Short: "Upgrade a Turbine Data Application",
Example: `meroxa apps upgrade my-app --path ~/code`,
Beta: true,
}
}

func (u *Upgrade) Logger(logger log.Logger) {
u.logger = logger
}

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

func (u *Upgrade) Execute(ctx context.Context) error {
var err error
u.path, err = turbine.GetPath(u.flags.Path)
if err != nil {
return err
}

u.logger.StartSpinner("\t", fmt.Sprintf(" Fetching details of application in %q...", u.path))
config, err := turbine.ReadConfigFile(u.path)
if err != nil {
u.logger.StopSpinnerWithStatus("\t", log.Failed)
return err
}
u.logger.StopSpinnerWithStatus(fmt.Sprintf("Determined the details of the %q Application", config.Name), log.Successful)

lang := config.Language
vendor, _ := strconv.ParseBool(config.Vendor)
switch lang {
case "go", GoLang:
if u.turbineCLI == nil {
u.turbineCLI = turbineGo.New(u.logger)
}
err = u.turbineCLI.Upgrade(u.path, vendor)
case "js", JavaScript, NodeJs:
if u.turbineCLI == nil {
u.turbineCLI = turbineJS.New(u.logger)
}
err = u.turbineCLI.Upgrade(u.path, vendor)
case "py", Python3, Python:
if u.turbineCLI == nil {
u.turbineCLI = turbinePY.New(u.logger)
}
err = u.turbineCLI.Upgrade(u.path, vendor)
default:
return fmt.Errorf("language %q not supported. %s", lang, LanguageNotSupportedError)
}
if err != nil {
return err
}

u.logger.StartSpinner("\t", " Testing upgrades locally...")
if u.run == nil {
u.run = &Run{
logger: log.NewWithDevNull(),
flags: struct {
Path string `long:"path" usage:"path of application to run"`
}{
Path: u.path,
},
}
}
err = u.run.Execute(ctx)
if err != nil {
u.logger.StopSpinnerWithStatus("Upgrades were not entirely successful."+
" Fix any issues before adding and committing all upgrades.", log.Failed)
return err
}
u.logger.StopSpinnerWithStatus("Tested upgrades locally successfully!", log.Successful)

u.logger.Infof(ctx, "Your Turbine Application %s has been upgraded successfully!"+
" To finish, add and commit the upgrades.", config.Name)
return nil
}
Loading

0 comments on commit c5783c0

Please sign in to comment.