Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update init command format and add a helpful link #387

Merged
merged 9 commits into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ jobs:
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
args: --timeout 4m0s
vet:
name: Vet
runs-on: ubuntu-latest
Expand Down
9 changes: 0 additions & 9 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ linters:
# inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint
disable-all: true
enable:
- bodyclose
- deadcode
- depguard
- dogsled
Expand All @@ -62,31 +61,23 @@ linters:
- funlen
- gochecknoinits
- goconst
- gocritic
- gocyclo
- godot
- gofmt
- goimports
- gomnd
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- lll
- misspell
- nakedret
- noctx
- nolintlint
- revive
- rowserrcheck
- staticcheck
- structcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused
- varcheck
- whitespace

Expand Down
14 changes: 12 additions & 2 deletions cmd/meroxa/root/apps/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,30 +126,40 @@ func (i *Init) Execute(ctx context.Context) error {
return err
}

i.logger.Infof(ctx, "Initializing application %q in %q...", name, i.path)
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)
if err != nil {
i.logger.StopSpinnerWithStatus("\t", log.Failed)
return err
}
err = turbineCLI.GoInit(ctx, i.logger, i.path+"/"+name, i.flags.SkipModInit, i.flags.ModVendor)
i.logger.StopSpinnerWithStatus("Application directory created!", log.Successful)
err = turbineCLI.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:
err = turbinepy.Init(ctx, i.logger, name, i.path)
default:
i.logger.StopSpinnerWithStatus("\t", log.Failed)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice addition!

return fmt.Errorf("language %q not supported. %s", lang, LanguageNotSupportedError)
}
if err != nil {
i.logger.StopSpinnerWithStatus("\t", log.Failed)
return err
}

if lang != "go" && lang != GoLang {
i.logger.StopSpinnerWithStatus("Application directory created!", log.Successful)
}
i.logger.StartSpinner("\t", "Running git initialization...")
err = i.GitInit(ctx, i.path+"/"+name)
if err != nil {
i.logger.StopSpinnerWithStatus("\t", log.Failed)
return err
}

i.logger.StopSpinnerWithStatus("Git initialized successfully!", log.Successful)
i.logger.Infof(ctx, "Turbine Data Application successfully initialized!\n"+
"You can start interacting with Meroxa in your app located at \"%s/%s\".\n"+
"Your Application will not be visible in the Meroxa Dashboard until after deployment.", i.path, name)
Expand Down
3 changes: 2 additions & 1 deletion cmd/meroxa/root/auth/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"net/url"
"os"
"strings"
"time"

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

Expand Down Expand Up @@ -101,7 +102,7 @@ func (l *Login) authorizeUser(ctx context.Context, clientID, authDomain, audienc
l.logger.Infof(ctx, color.CyanString(authorizationURL))

// start a web server to listen on a callback URL
server := &http.Server{Addr: redirectURL}
server := &http.Server{Addr: redirectURL, ReadHeaderTimeout: time.Minute}

// define a handler that will get the authorization code, call the token endpoint, and close the HTTP server
http.HandleFunc("/oauth/callback", func(w http.ResponseWriter, r *http.Request) {
Expand Down
104 changes: 62 additions & 42 deletions cmd/meroxa/turbine_cli/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,73 +243,93 @@ func GetGitSha(appPath string) (string, error) {
return string(output), nil
}

func GoInit(ctx context.Context, l log.Logger, appPath string, skipInit, vendor bool) error {
func GoInit(l log.Logger, appPath string, skipInit, vendor bool) error {
l.StartSpinner("\t", "Running golang module initializing...")
skipLog := "skipping go module initialization\n\tFor guidance, visit " +
"https://docs.meroxa.com/beta-overview#go-mod-init-for-a-new-golang-turbine-data-application"
goPath := os.Getenv("GOPATH")
if goPath == "" {
l.Warnf(ctx, "$GOPATH not set up; skipping go module initialization")
l.StopSpinnerWithStatus("$GOPATH not set up; "+skipLog, log.Warning)
return nil
}
i := strings.Index(appPath, goPath)
i := strings.Index(appPath, goPath+"/src")
if i == -1 || i != 0 {
l.Warnf(ctx, "%s is not under $GOPATH; skipping go module initialization", appPath)
l.StopSpinnerWithStatus(fmt.Sprintf("%s is not under $GOPATH/src; %s", appPath, skipLog), log.Warning)
return nil
}

// temporarily switching to the app's directory
pwd, err := switchToAppDirectory(appPath)
if err != nil {
l.StopSpinnerWithStatus("\t", log.Failed)
return err
}

// initialize the user's module
err = SetModuleInitInAppJSON(appPath, skipInit)
if err != nil {
l.StopSpinnerWithStatus("\t", log.Failed)
return err
}
if !skipInit {
l.Info(ctx, "Initializing the application's go module...")
cmd := exec.Command("go", "mod", "init")
output, err := cmd.CombinedOutput()
if err != nil {
l.Error(ctx, string(output))
return err
}
cmd = exec.Command("go", "get", "github.com/meroxa/turbine-go")
output, err = cmd.CombinedOutput()
if err != nil {
l.Error(ctx, string(output))
return err
}
cmd = exec.Command("go", "get", "github.com/meroxa/turbine-go/runner")
output, err = cmd.CombinedOutput()
if err != nil {
l.Error(ctx, string(output))
return err
}

// download dependencies
err = SetVendorInAppJSON(appPath, vendor)
if err != nil {
return err
}
depsLog := "Downloading dependencies"
cmd = exec.Command("go", "mod", "download")
if vendor {
depsLog += " to vendor"
cmd = exec.Command("go", "mod", "vendor")
}
depsLog += "..."
l.Info(ctx, depsLog)
output, err = cmd.CombinedOutput()
if err != nil {
l.Error(ctx, string(output))
return err
}
err = modulesInit(l, appPath, skipInit, vendor)
if err != nil {
l.StopSpinnerWithStatus("\t", log.Failed)
return err
}

return os.Chdir(pwd)
}

func modulesInit(l log.Logger, appPath string, skipInit, vendor bool) error {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lint complained... made me do it

if skipInit {
return nil
}

cmd := exec.Command("go", "mod", "init")
output, err := cmd.CombinedOutput()
if err != nil {
l.StopSpinnerWithStatus(fmt.Sprintf("\t%s", string(output)), log.Failed)
return err
}
l.StopSpinnerWithStatus("go mod init succeeded!", log.Successful)
l.StartSpinner("\t", "Getting latest turbine-go and turbine-go/running dependencies...")
cmd = exec.Command("go", "get", "github.com/meroxa/turbine-go")
output, err = cmd.CombinedOutput()
if err != nil {
l.StopSpinnerWithStatus(fmt.Sprintf("\t%s", string(output)), log.Failed)
return err
}
cmd = exec.Command("go", "get", "github.com/meroxa/turbine-go/runner")
output, err = cmd.CombinedOutput()
if err != nil {
l.StopSpinnerWithStatus(fmt.Sprintf("\t%s", string(output)), log.Failed)
return err
}
l.StopSpinnerWithStatus("Downloaded latest turbine-go and turbine-go/running dependencies successfully!", log.Successful)

// download dependencies
err = SetVendorInAppJSON(appPath, vendor)
if err != nil {
return err
}
depsLog := "Downloading dependencies"
cmd = exec.Command("go", "mod", "download")
if vendor {
depsLog += " to vendor"
cmd = exec.Command("go", "mod", "vendor")
}
depsLog += "..."
l.StartSpinner("\t", depsLog)
output, err = cmd.CombinedOutput()
if err != nil {
l.StopSpinnerWithStatus(fmt.Sprintf("\t%s", string(output)), log.Failed)
return err
}
l.StopSpinnerWithStatus("Downloaded all other dependencies successfully!", log.Successful)
return nil
}

// switchToAppDirectory switches temporarily to the application's directory.
func switchToAppDirectory(appPath string) (string, error) {
pwd, err := os.Getwd()
Expand Down
7 changes: 7 additions & 0 deletions log/spinner.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
const (
Successful = "successful"
Failed = "failed"
Warning = "warning"
)

type SpinnerLogger interface {
Expand Down Expand Up @@ -50,6 +51,8 @@ func (l *spinnerLogger) StopSpinnerWithStatus(msg, status string) {
msg = fmt.Sprintf("\t%s %s", l.FailedMark(), msg)
} else if status == Successful {
msg = fmt.Sprintf("\t%s %s", l.SuccessfulCheck(), msg)
} else if status == Warning {
msg = fmt.Sprintf("\t%s %s", l.WarningMark(), msg)
}
l.s.Stop()
l.l.Printf(msg)
Expand All @@ -62,3 +65,7 @@ func (l *spinnerLogger) SuccessfulCheck() string {
func (l *spinnerLogger) FailedMark() string {
return color.New(color.FgRed).Sprintf("x")
}

func (l *spinnerLogger) WarningMark() string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️

return color.New(color.FgYellow).Sprintf("⚡")
}