Skip to content

Commit

Permalink
feat(apps): Add init (#250)
Browse files Browse the repository at this point in the history
* meroxa apps run initial spike

* feat(apps): Add init cmd

Brett Goulder <[email protected]>

Co-authored-by: Brett Goulder <[email protected]>
  • Loading branch information
raulb and brettgoulder authored Feb 16, 2022
1 parent cd74985 commit 9b65d9d
Show file tree
Hide file tree
Showing 152 changed files with 11,300 additions and 7,778 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ build: docs
install:
go build -o $$(go env GOPATH)/bin/meroxa cmd/meroxa/main.go

PRIVATE_REPOS = github.com/meroxa/meroxa-go
PRIVATE_REPOS = github.com/meroxa/meroxa-go,github.com/meroxa/turbine
.PHONY: gomod
gomod:
GOPRIVATE=$(PRIVATE_REPOS) go mod tidy && go mod vendor
Expand Down
1 change: 1 addition & 0 deletions cmd/meroxa/root/apps/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ func (*Apps) Docs() builder.Docs {
func (*Apps) SubCommands() []*cobra.Command {
return []*cobra.Command{
builder.BuildCobraCommand(&Run{}),
builder.BuildCobraCommand(&Init{}),
}
}
94 changes: 94 additions & 0 deletions cmd/meroxa/root/apps/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package apps

import (
"context"
"errors"
"fmt"
"os/exec"

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

type Init struct {
logger log.Logger

args struct {
appName string
}

flags struct {
Lang string `long:"lang" short:"l" usage:"language to use (js|go)" required:"true"`
Path string `long:"path" usage:"path where application will be initialized (current directory as default)"`
}
}

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

func (*Init) Usage() string {
return "init [APP_NAME] [--path pwd] --lang js|go"
}

func (*Init) Docs() builder.Docs {
return builder.Docs{
Short: "Initialize a Meroxa Data Application",
Example: "meroxa apps init my-app --path ~/code --lang js" +
"meroxa apps init my-app --lang go # will be initialized in current directory",
}
}

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

func (i *Init) ParseArgs(args []string) error {
if len(args) < 1 {
return errors.New("requires an application name")
}

i.args.appName = args[0]
return nil
}

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
}

switch lang {
case "go", "golang":
i.logger.Infof(ctx, "Initializing application %q in %q", name, path)
err := turbine.Init(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)
stdout, err := cmd.CombinedOutput()
if err != nil {
return err
}
i.logger.Info(ctx, string(stdout))
default:
return fmt.Errorf("language %q not supported. Currently, we support \"javascript\" and \"go\"", lang)
}

return nil
}

func (i *Init) Logger(logger log.Logger) {
i.logger = logger
}
70 changes: 70 additions & 0 deletions cmd/meroxa/root/apps/init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package apps

import (
"errors"
"testing"

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

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

for _, tt := range tests {
cc := &Init{}
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.appName {
t.Fatalf("expected \"%s\" got \"%s\"", tt.appName, cc.args.appName)
}
}
}

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

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

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)
}
}
}
}
3 changes: 2 additions & 1 deletion cmd/meroxa/root/apps/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ var (
)

type AppConfig struct {
Language string
Name string `json:"name"`
Language string `json:"language"`
}

func (*Run) Usage() string {
Expand Down
4 changes: 2 additions & 2 deletions etc/completion/meroxa.completion.fish
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@ function __meroxa_prepare_completions
# We don't need descriptions anyway since there is only a single
# real completion which the shell will expand immediately.
set -l split (string split --max 1 \t $__meroxa_comp_results[1])

# Fish won't add a space if the completion ends with any
# of the following characters: @=/:.,
set -l lastChar (string sub -s -1 -- $split)
if not string match -r -q "[@=/:.,]" -- "$lastChar"
if not string match -r -q "[@=/:.,]" -- "$lastChar"
# In other cases, to support the "nospace" directive we trick the shell
# by outputting an extra, longer completion.
__meroxa_debug "Adding second completion to perform nospace directive"
Expand Down
14 changes: 9 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,22 @@ require (
github.com/pkg/browser v0.0.0-20210115035449-ce105d075bb4
github.com/rivo/uniseg v0.2.0 // indirect
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966
github.com/spf13/cobra v1.2.0
github.com/spf13/cobra v1.2.1
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.8.1
golang.org/x/net v0.0.0-20211101193420-4a448f8816b3 // indirect
golang.org/x/oauth2 v0.0.0-20211028175245-ba495a64dcb5
)

require github.com/mattn/go-shellwords v1.0.12

require (
github.com/cristalhq/jwt/v3 v3.1.0 // indirect
github.com/meroxa/turbine v0.0.0-20220215220759-6865e405b0dd
)

require (
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/cristalhq/jwt/v3 v3.1.0 // indirect
github.com/dewski/jsonpath v0.0.0-20210103075638-af6da8f1a897 // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/golang/protobuf v1.5.2 // indirect
Expand All @@ -40,7 +43,7 @@ require (
github.com/kelseyhightower/envconfig v1.4.0 // indirect
github.com/lunixbochs/vtclean v0.0.0-20180621232353-2d01aacdc34a // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/pelletier/go-toml v1.9.3 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
Expand All @@ -49,7 +52,8 @@ require (
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e // indirect
golang.org/x/net v0.0.0-20211101193420-4a448f8816b3 // indirect
golang.org/x/sys v0.0.0-20211124211545-fe61309f8881 // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/appengine v1.6.7 // indirect
Expand Down
Loading

0 comments on commit 9b65d9d

Please sign in to comment.