-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add timeout handling to legacy cli (snyk#4950)
* fix: use deadline context for timeout in legacy workflow * fix: tests & refactor error handling for deadline * fix: display error better * chore: use context.WithTimeout Co-authored-by: Casey Marshall <[email protected]> * chore: assert error is nil Co-authored-by: Casey Marshall <[email protected]> * chore: implement pr suggestions * fix: test --------- Co-authored-by: Casey Marshall <[email protected]>
- Loading branch information
1 parent
a0308f1
commit 1686db5
Showing
4 changed files
with
96 additions
and
36 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 |
---|---|---|
|
@@ -4,7 +4,9 @@ Entry point class for the CLIv2 version. | |
package cliv2 | ||
|
||
import ( | ||
"context" | ||
_ "embed" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"log" | ||
|
@@ -13,6 +15,7 @@ import ( | |
"path" | ||
"regexp" | ||
"strings" | ||
"time" | ||
|
||
"github.com/gofrs/flock" | ||
"github.com/snyk/go-application-framework/pkg/configuration" | ||
|
@@ -74,6 +77,11 @@ func NewCLIv2(config configuration.Configuration, debugLogger *log.Logger) (*CLI | |
return &cli, nil | ||
} | ||
|
||
// SetV1BinaryLocation for testing purposes | ||
func (c *CLI) SetV1BinaryLocation(filePath string) { | ||
c.v1BinaryLocation = filePath | ||
} | ||
|
||
func (c *CLI) Init() (err error) { | ||
c.DebugLogger.Println("Init start") | ||
|
||
|
@@ -200,7 +208,7 @@ func (c *CLI) GetBinaryLocation() string { | |
} | ||
|
||
func (c *CLI) printVersion() { | ||
fmt.Fprintln(c.stdout, GetFullVersion()) | ||
_, _ = fmt.Fprintln(c.stdout, GetFullVersion()) | ||
} | ||
|
||
func (c *CLI) commandVersion(passthroughArgs []string) error { | ||
|
@@ -235,8 +243,8 @@ func (c *CLI) commandAbout(proxyInfo *proxy.ProxyInfo, passthroughArgs []string) | |
} | ||
|
||
fmt.Printf("Package: %s \n", strings.ReplaceAll(strings.ReplaceAll(fPath, "/licenses/", ""), "/"+f.Name(), "")) | ||
fmt.Fprintln(c.stdout, string(data)) | ||
fmt.Fprint(c.stdout, separator) | ||
_, _ = fmt.Fprintln(c.stdout, string(data)) | ||
_, _ = fmt.Fprint(c.stdout, separator) | ||
} | ||
} | ||
|
||
|
@@ -341,15 +349,15 @@ func PrepareV1EnvironmentVariables( | |
} | ||
|
||
func (c *CLI) PrepareV1Command( | ||
ctx context.Context, | ||
cmd string, | ||
args []string, | ||
proxyInfo *proxy.ProxyInfo, | ||
integrationName string, | ||
integrationVersion string, | ||
) (snykCmd *exec.Cmd, err error) { | ||
proxyAddress := fmt.Sprintf("http://%s:%[email protected]:%d", proxy.PROXY_USERNAME, proxyInfo.Password, proxyInfo.Port) | ||
|
||
snykCmd = exec.Command(cmd, args...) | ||
snykCmd = exec.CommandContext(ctx, cmd, args...) | ||
snykCmd.Env, err = PrepareV1EnvironmentVariables(c.env, integrationName, integrationVersion, proxyAddress, proxyInfo.CertificateLocation, c.globalConfig, args) | ||
|
||
if len(c.WorkingDirectory) > 0 { | ||
|
@@ -360,8 +368,17 @@ func (c *CLI) PrepareV1Command( | |
} | ||
|
||
func (c *CLI) executeV1Default(proxyInfo *proxy.ProxyInfo, passThroughArgs []string) error { | ||
timeout := c.globalConfig.GetInt(configuration.TIMEOUT) | ||
var ctx context.Context | ||
var cancel context.CancelFunc | ||
if timeout == 0 { | ||
ctx = context.Background() | ||
} else { | ||
ctx, cancel = context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second) | ||
defer cancel() | ||
} | ||
|
||
snykCmd, err := c.PrepareV1Command(c.v1BinaryLocation, passThroughArgs, proxyInfo, c.GetIntegrationName(), GetFullVersion()) | ||
snykCmd, err := c.PrepareV1Command(ctx, c.v1BinaryLocation, passThroughArgs, proxyInfo, c.GetIntegrationName(), GetFullVersion()) | ||
|
||
if c.DebugLogger.Writer() != io.Discard { | ||
c.DebugLogger.Println("Launching: ") | ||
|
@@ -397,13 +414,16 @@ func (c *CLI) executeV1Default(proxyInfo *proxy.ProxyInfo, passThroughArgs []str | |
snykCmd.Stderr = c.stderr | ||
|
||
if err != nil { | ||
if evWarning, ok := err.(EnvironmentWarning); ok { | ||
fmt.Fprintln(c.stdout, "WARNING! ", evWarning) | ||
var evWarning EnvironmentWarning | ||
if errors.As(err, &evWarning) { | ||
_, _ = fmt.Fprintln(c.stdout, "WARNING! ", evWarning) | ||
} | ||
} | ||
|
||
err = snykCmd.Run() | ||
|
||
if errors.Is(ctx.Err(), context.DeadlineExceeded) { | ||
return ctx.Err() | ||
} | ||
return err | ||
} | ||
|
||
|
@@ -427,14 +447,17 @@ func DeriveExitCode(err error) int { | |
returnCode := constants.SNYK_EXIT_CODE_OK | ||
|
||
if err != nil { | ||
if exitError, ok := err.(*exec.ExitError); ok { | ||
var exitError *exec.ExitError | ||
|
||
if errors.As(err, &exitError) { | ||
returnCode = exitError.ExitCode() | ||
} else if errors.Is(err, context.DeadlineExceeded) { | ||
returnCode = constants.SNYK_EXIT_CODE_EX_UNAVAILABLE | ||
} else { | ||
// got an error but it's not an ExitError | ||
returnCode = constants.SNYK_EXIT_CODE_ERROR | ||
} | ||
} | ||
|
||
return returnCode | ||
} | ||
|
||
|
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