-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* meroxa apps run initial spike * feat(apps): Add init cmd Brett Goulder <[email protected]> Co-authored-by: Brett Goulder <[email protected]>
- Loading branch information
1 parent
cd74985
commit 9b65d9d
Showing
152 changed files
with
11,300 additions
and
7,778 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.