-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: bastiandoetsch <[email protected]>
- Loading branch information
1 parent
da8fd45
commit e0971ab
Showing
29 changed files
with
282 additions
and
120 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
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
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
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,75 @@ | ||
/* | ||
* © 2023-2024 Snyk Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package command | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"os/exec" | ||
|
||
"github.com/rs/zerolog" | ||
|
||
"github.com/snyk/snyk-ls/application/config" | ||
"github.com/snyk/snyk-ls/infrastructure/authentication" | ||
"github.com/snyk/snyk-ls/infrastructure/cli" | ||
noti "github.com/snyk/snyk-ls/internal/notification" | ||
"github.com/snyk/snyk-ls/internal/types" | ||
) | ||
|
||
type executeCLICommand struct { | ||
command types.CommandData | ||
authService authentication.AuthenticationService | ||
notifier noti.Notifier | ||
logger *zerolog.Logger | ||
cli cli.Executor | ||
} | ||
|
||
type cliScanResult struct { | ||
ExitCode int `json:"exitCode"` | ||
StdOut string `json:"stdOut"` | ||
} | ||
|
||
func (cmd *executeCLICommand) Command() types.CommandData { | ||
return cmd.command | ||
} | ||
|
||
func (cmd *executeCLICommand) Execute(ctx context.Context) (any, error) { | ||
if len(cmd.command.Arguments) < 2 { | ||
return nil, fmt.Errorf("invalid usage of executeCLICommand. First arg needs to be the workDir, then CLI arguments without binary path") | ||
} | ||
workDir := cmd.command.Arguments[0].(string) | ||
|
||
args := []string{config.CurrentConfig().CliSettings().Path()} | ||
for _, argument := range cmd.command.Arguments[1:] { | ||
args = append(args, argument.(string)) | ||
} | ||
|
||
args = cmd.cli.ExpandParametersFromConfig(args) | ||
var exitCode int | ||
resp, err := cmd.cli.Execute(ctx, args, workDir) | ||
if err != nil { | ||
var exitError *exec.ExitError | ||
if errors.As(err, &exitError) { | ||
exitCode = exitError.ExitCode() | ||
} | ||
} | ||
return cliScanResult{ | ||
ExitCode: exitCode, | ||
StdOut: string(resp), | ||
}, nil | ||
} |
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,55 @@ | ||
/* | ||
* © 2024 Snyk Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package command | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
cli2 "github.com/snyk/snyk-ls/infrastructure/cli" | ||
"github.com/snyk/snyk-ls/internal/testutil" | ||
"github.com/snyk/snyk-ls/internal/types" | ||
) | ||
|
||
func Test_executeCLI_callsCli(t *testing.T) { | ||
c := testutil.UnitTest(t) | ||
expected := `{ "outputKey": "outputValue" }` | ||
dir := t.TempDir() | ||
|
||
cli := cli2.NewTestExecutorWithResponse(expected) | ||
|
||
args := []any{dir, "iac", "test", "--json"} | ||
cut := executeCLICommand{ | ||
command: types.CommandData{ | ||
Title: "testCMD", | ||
CommandId: types.ExecuteCLICommand, | ||
Arguments: args, | ||
}, | ||
logger: c.Logger(), | ||
cli: cli, | ||
} | ||
|
||
response, err := cut.Execute(context.Background()) | ||
require.NoError(t, err) | ||
|
||
assert.True(t, cli.WasExecuted()) | ||
assert.IsType(t, cliScanResult{}, response) | ||
assert.Equal(t, expected, response.(cliScanResult).StdOut) | ||
} |
Oops, something went wrong.