Skip to content

Commit

Permalink
Use stderr and exit code to detect errors (#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
janelletavares authored Mar 25, 2022
1 parent db7fb1a commit 7efc1df
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 51 deletions.
21 changes: 6 additions & 15 deletions cmd/meroxa/root/apps/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"os/exec"

turbinejs "github.com/meroxa/cli/cmd/meroxa/turbine_cli/javascript"

"github.com/meroxa/cli/cmd/meroxa/builder"
turbineCLI "github.com/meroxa/cli/cmd/meroxa/turbine_cli"
"github.com/meroxa/cli/log"
Expand Down Expand Up @@ -92,27 +94,16 @@ func (i *Init) Execute(ctx context.Context) error {
switch lang {
case "go", GoLang:
err = turbine.Init(name, i.path)
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/%s\"", i.path, name)
case "js", JavaScript, NodeJs:
cmd := exec.Command("npx", "turbine", "generate", name, i.path)
stdout, err := cmd.CombinedOutput() //nolint:govet
if err != nil {
i.logger.Error(ctx, string(stdout))
return err
}
i.logger.Info(ctx, string(stdout))
err = turbinejs.Init(ctx, i.logger, name, i.path)
default:
return fmt.Errorf("language %q not supported. %s", lang, LanguageNotSupportedError)
}

err = i.GitInit(ctx, 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/%s\"", i.path, name)

return nil
return i.GitInit(ctx, i.path+"/"+name)
}
11 changes: 4 additions & 7 deletions cmd/meroxa/turbine_cli/golang/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"os/exec"

turbinecli "github.com/meroxa/cli/cmd/meroxa/turbine_cli"

"github.com/meroxa/cli/log"
)

Expand All @@ -18,11 +20,6 @@ func buildBinary(ctx context.Context, l log.Logger, appPath, appName string, pla
}
cmd.Dir = appPath

stdout, err := cmd.CombinedOutput()
if err != nil {
l.Errorf(ctx, string(stdout))
return err
}

return nil
_, err := turbinecli.RunCmdWithErrorDetection(ctx, cmd, l)
return err
}
14 changes: 6 additions & 8 deletions cmd/meroxa/turbine_cli/golang/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"path"
"regexp"

turbinecli "github.com/meroxa/cli/cmd/meroxa/turbine_cli"

"github.com/meroxa/cli/cmd/meroxa/global"
"github.com/meroxa/cli/log"
)
Expand Down Expand Up @@ -36,15 +38,11 @@ func runDeployApp(ctx context.Context, l log.Logger, appPath, appName, imageName
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, fmt.Sprintf("ACCESS_TOKEN=%s", accessToken), fmt.Sprintf("REFRESH_TOKEN=%s", refreshToken))

stdout, err := cmd.CombinedOutput()
if err != nil {
l.Errorf(ctx, string(stdout))
return "", err
stdout, err := turbinecli.RunCmdWithErrorDetection(ctx, cmd, l)
if err == nil {
l.Info(ctx, "deploy complete!")
}

l.Info(ctx, string(stdout))
l.Info(ctx, "deploy complete!")
return string(stdout), nil
return stdout, err
}

// needsToBuild reads from the Turbine application to determine whether it needs to be built or not
Expand Down
12 changes: 4 additions & 8 deletions cmd/meroxa/turbine_cli/golang/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"os/exec"
"path"

turbinecli "github.com/meroxa/cli/cmd/meroxa/turbine_cli"

"github.com/meroxa/cli/log"
)

Expand All @@ -27,12 +29,6 @@ func Run(ctx context.Context, appPath string, l log.Logger) error {

cmd := exec.Command("./" + appName) //nolint:gosec

out, err := cmd.CombinedOutput()
if err != nil {
l.Error(ctx, err.Error())
return err
}

l.Info(ctx, string(out))
return nil
_, err = turbinecli.RunCmdWithErrorDetection(ctx, cmd, l)
return err
}
10 changes: 4 additions & 6 deletions cmd/meroxa/turbine_cli/javascript/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"os/exec"

turbinecli "github.com/meroxa/cli/cmd/meroxa/turbine_cli"

"github.com/meroxa/cli/log"
)

Expand All @@ -12,10 +14,6 @@ func Build(ctx context.Context, l log.Logger) error {
// TODO: Handle the requirement of https://github.com/meroxa/turbine-js.git being installed
// cd into the path first
cmd := exec.Command("npx", "turbine", "test")
stdout, err := cmd.CombinedOutput()
if err != nil {
return err
}
l.Info(ctx, string(stdout))
return nil
_, err := turbinecli.RunCmdWithErrorDetection(ctx, cmd, l)
return err
}
10 changes: 3 additions & 7 deletions cmd/meroxa/turbine_cli/javascript/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os/exec"

"github.com/meroxa/cli/cmd/meroxa/global"
turbinecli "github.com/meroxa/cli/cmd/meroxa/turbine_cli"
"github.com/meroxa/cli/log"
)

Expand All @@ -20,11 +21,6 @@ func Deploy(ctx context.Context, path string, l log.Logger) error {
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, fmt.Sprintf("MEROXA_ACCESS_TOKEN=%s", accessToken))

stdout, err := cmd.CombinedOutput()
if err != nil {
l.Error(ctx, string(stdout))
return err
}
l.Info(ctx, string(stdout))
return nil
_, err = turbinecli.RunCmdWithErrorDetection(ctx, cmd, l)
return err
}
15 changes: 15 additions & 0 deletions cmd/meroxa/turbine_cli/javascript/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package turbinejs

import (
"context"
"os/exec"

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

func Init(ctx context.Context, l log.Logger, name, path string) error {
cmd := exec.Command("npx", "turbine", "generate", name, path)
_, err := turbineCLI.RunCmdWithErrorDetection(ctx, cmd, l)
return err
}
19 changes: 19 additions & 0 deletions cmd/meroxa/turbine_cli/utils.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package turbinecli

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
Expand Down Expand Up @@ -183,3 +185,20 @@ func switchToAppDirectory(appPath string) (string, error) {
}
return pwd, os.Chdir(appPath)
}

// RunCmdWithErrorDetection checks exit codes and stderr for failures and logs on success.
func RunCmdWithErrorDetection(ctx context.Context, cmd *exec.Cmd, l log.Logger) (string, error) {
stdout, stderr := bytes.NewBuffer(nil), bytes.NewBuffer(nil)
cmd.Stdout = stdout
cmd.Stderr = stderr
err := cmd.Run()
successMsg := stdout.String()
errMsg := stderr.String()
if err != nil {
return "", err
} else if errMsg != "" {
return "", errors.New(successMsg + errMsg)
}
l.Info(ctx, successMsg)
return successMsg, nil
}

0 comments on commit 7efc1df

Please sign in to comment.