Skip to content
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
12 changes: 10 additions & 2 deletions internal/temporalcli/commands.extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ var cliArgsToParseForExtension = map[string]bool{
"command-timeout": true,
}

type ExtensionNonZeroExit struct {
*exec.ExitError
}

func (err ExtensionNonZeroExit) Unwrap() error {
return err.ExitError
}

// tryExecuteExtension tries to execute an extension command if the command is not a built-in command.
// It returns an error if the extension command fails, and a boolean indicating whether an extension was executed.
func tryExecuteExtension(cctx *CommandContext, tcmd *TemporalCommand) (error, bool) {
Expand Down Expand Up @@ -77,8 +85,8 @@ func tryExecuteExtension(cctx *CommandContext, tcmd *TemporalCommand) (error, bo
if ctx.Err() != nil {
return fmt.Errorf("program interrupted"), true
}
if _, ok := err.(*exec.ExitError); ok {
return nil, true
if exitError, ok := err.(*exec.ExitError); ok {
return ExtensionNonZeroExit{exitError}, true
}
return fmt.Errorf("extension %s failed: %w", extPath, err), true
}
Expand Down
10 changes: 7 additions & 3 deletions internal/temporalcli/commands.extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/temporalio/cli/internal/temporalcli"
"golang.org/x/tools/imports"
)

Expand Down Expand Up @@ -232,7 +233,7 @@ func TestExtension_FailsOnNonExecutableCommand(t *testing.T) {
h := newExtensionHarness(t)
// Create file without execute permission.
path := filepath.Join(h.binDir, "temporal-foo")
err := os.WriteFile(path, []byte("a text file"), 0644)
err := os.WriteFile(path, []byte("a text file"), 0o644)
require.NoError(t, err)

res := h.Execute("foo")
Expand All @@ -248,7 +249,10 @@ func TestExtension_PassesThroughNonZeroExit(t *testing.T) {
res := h.Execute("foo")

assert.Equal(t, "Args: temporal-foo \n", res.Stdout.String())
assert.NoError(t, res.Err)
var exitError temporalcli.ExtensionNonZeroExit
if assert.ErrorAs(t, res.Err, &exitError) {
assert.Equal(t, 42, exitError.ExitCode())
}
}

func TestExtension_FailsOnCommandTimeout(t *testing.T) {
Expand Down Expand Up @@ -306,7 +310,7 @@ func (h *extensionHarness) createExtension(name string, code ...string) string {

// Write source file.
srcPath := filepath.Join(h.binDir, name+".go")
require.NoError(h.t, os.WriteFile(srcPath, formatted, 0644))
require.NoError(h.t, os.WriteFile(srcPath, formatted, 0o644))

// Build executable.
binPath := filepath.Join(h.binDir, name)
Expand Down
7 changes: 6 additions & 1 deletion internal/temporalcli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ func (c *CommandContext) preprocessOptions() error {
if c.Err() != nil {
err = fmt.Errorf("program interrupted")
}
if exitError, ok := errors.AsType[ExtensionNonZeroExit](err); ok {
// An extension failed after being found and successfully started. Here we defer
// to its own error handling logic, and just copy the exit code through.
os.Exit(exitError.ExitCode())
}
fmt.Fprintf(c.Options.Stderr, "Error: %v\n", err)
os.Exit(1)
}
Expand Down Expand Up @@ -524,7 +529,7 @@ var buildInfo string
func VersionString() string {
// To add build-time information to the version string, use
// go build -ldflags "-X github.com/temporalio/cli/internal.buildInfo=<MyString>"
var bi = buildInfo
bi := buildInfo
if bi != "" {
bi = fmt.Sprintf(", %s", bi)
}
Expand Down
Loading