Skip to content

Commit

Permalink
Merge pull request #257 from meroxa/rb-bg/app-deploy
Browse files Browse the repository at this point in the history
feat(apps): Add deploy
  • Loading branch information
jayjayjpg authored Feb 22, 2022
2 parents 9b65d9d + 79a8f88 commit 326f3e7
Show file tree
Hide file tree
Showing 812 changed files with 102,580 additions and 281 deletions.
9 changes: 8 additions & 1 deletion cmd/meroxa/root/apps/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ import (

type Apps struct{}

const (
JavaScript = "javascript"
GoLang = "golang"
NodeJs = "nodejs"
)

var (
_ builder.CommandWithDocs = (*Apps)(nil)
_ builder.CommandWithAliases = (*Apps)(nil)
Expand Down Expand Up @@ -50,7 +56,8 @@ func (*Apps) Docs() builder.Docs {

func (*Apps) SubCommands() []*cobra.Command {
return []*cobra.Command{
builder.BuildCobraCommand(&Run{}),
builder.BuildCobraCommand(&Deploy{}),
builder.BuildCobraCommand(&Init{}),
builder.BuildCobraCommand(&Run{}),
}
}
26 changes: 0 additions & 26 deletions cmd/meroxa/root/apps/common.go

This file was deleted.

125 changes: 125 additions & 0 deletions cmd/meroxa/root/apps/deploy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
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"
"errors"
"fmt"
"os"

"github.com/meroxa/cli/cmd/meroxa/builder"
turbineCLI "github.com/meroxa/cli/cmd/meroxa/turbine_cli"
"github.com/meroxa/cli/config"
"github.com/meroxa/cli/log"
)

const (
dockerHubUserNameEnv = "DOCKER_HUB_USERNAME"
dockerHubAccessTokenEnv = "DOCKER_HUB_ACCESS_TOKEN" // nolint:gosec
)

type Deploy struct {
flags struct {
Path string `long:"path" description:"path to the app directory"`
}

config config.Config
logger log.Logger
path string
goDeploy turbineCLI.GoDeploy
}

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

func (*Deploy) Usage() string {
return "deploy"
}

func (*Deploy) Docs() builder.Docs {
return builder.Docs{
Short: "Deploy a Meroxa Data Application",
Example: "meroxa apps deploy # assumes you run it from the app directory\n" +
"meroxa apps deploy --path ./my-app",
}
}

func (d *Deploy) Config(cfg config.Config) {
d.config = cfg
}

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

func (d *Deploy) getDockerHubUserNameEnv() string {
if v := os.Getenv(dockerHubUserNameEnv); v != "" {
return v
}
return d.config.GetString(dockerHubUserNameEnv)
}

func (d *Deploy) getDockerHubAccessTokenEnv() string {
if v := os.Getenv(dockerHubAccessTokenEnv); v != "" {
return v
}
return d.config.GetString(dockerHubAccessTokenEnv)
}

func (d *Deploy) checkRequiredEnvVars() error {
if d.getDockerHubUserNameEnv() == "" || d.getDockerHubAccessTokenEnv() == "" {
msg := fmt.Sprintf("both %q and %q are required to be set to deploy your application", dockerHubUserNameEnv, dockerHubAccessTokenEnv)
return errors.New(msg)
}

d.goDeploy.DockerHubUserNameEnv = d.getDockerHubUserNameEnv()
d.goDeploy.DockerHubAccessTokenEnv = d.getDockerHubAccessTokenEnv()

return nil
}

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

func (d *Deploy) Execute(ctx context.Context) error {
err := d.checkRequiredEnvVars()
if err != nil {
return err
}

d.path = turbineCLI.GetPath(d.flags.Path)
lang, err := turbineCLI.GetLangFromAppJSON(d.path)
if err != nil {
return err
}

switch lang {
case "go", GoLang:
return d.goDeploy.DeployGoApp(ctx, d.path, d.logger)
case "js", JavaScript, NodeJs:
return turbineCLI.DeployJSApp(ctx, d.path, d.logger)
default:
return fmt.Errorf("language %q not supported. Currently, we support \"javascript\" and \"go\"", lang)
}
}
44 changes: 44 additions & 0 deletions cmd/meroxa/root/apps/deploy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package apps

import (
"testing"

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

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

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

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)
}
}
}
}
27 changes: 13 additions & 14 deletions cmd/meroxa/root/apps/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
"os/exec"

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

type Init struct {
logger log.Logger
path string

args struct {
appName string
Expand Down Expand Up @@ -44,6 +46,10 @@ func (*Init) Docs() builder.Docs {
}
}

func (i *Init) Logger(logger log.Logger) {
i.logger = logger
}

func (i *Init) Flags() []builder.Flag {
return builder.BuildFlags(&i.flags)
}
Expand All @@ -60,23 +66,20 @@ func (i *Init) ParseArgs(args []string) error {
func (i *Init) Execute(ctx context.Context) error {
name := i.args.appName
lang := i.flags.Lang
path := "."

if i.flags.Path != "" {
path = i.flags.Path
}
i.path = turbineCLI.GetPath(i.flags.Path)

switch lang {
case "go", "golang":
i.logger.Infof(ctx, "Initializing application %q in %q", name, path)
err := turbine.Init(path, name)
case "go", GoLang:
i.logger.Infof(ctx, "Initializing application %q in %q...", name, i.path)
err := turbine.Init(i.path, name)
if err != nil {
return err
}
i.logger.Infof(ctx, "Application successfully initialized!\n"+
"You can start interacting with Meroxa in your app located at %s", path)
case "js", "javascript", "nodejs":
cmd := exec.Command("npx", "turbine", "generate", name)
"You can start interacting with Meroxa in your app located at \"%s/%s\"", i.path, name)
case "js", JavaScript, NodeJs:
cmd := exec.Command("npx", "turbine_cli", "generate", name)
stdout, err := cmd.CombinedOutput()
if err != nil {
return err
Expand All @@ -88,7 +91,3 @@ func (i *Init) Execute(ctx context.Context) error {

return nil
}

func (i *Init) Logger(logger log.Logger) {
i.logger = logger
}
Loading

0 comments on commit 326f3e7

Please sign in to comment.