Skip to content

Commit

Permalink
feat: add executeCLI command
Browse files Browse the repository at this point in the history
  • Loading branch information
bastiandoetsch committed Aug 19, 2024
1 parent 6807fa9 commit a586b0e
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 1 deletion.
1 change: 1 addition & 0 deletions application/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ func initializeHandler(srv *jrpc2.Server) handler.Func {
types.CodeFixCommand,
types.CodeSubmitFixFeedback,
types.CodeFixDiffsCommand,
types.ExecuteCLICommand,
},
},
},
Expand Down
1 change: 1 addition & 0 deletions application/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ func Test_initialize_shouldSupportAllCommands(t *testing.T) {
assert.Contains(t, result.Capabilities.ExecuteCommandProvider.Commands, types.CodeFixCommand)
assert.Contains(t, result.Capabilities.ExecuteCommandProvider.Commands, types.CodeSubmitFixFeedback)
assert.Contains(t, result.Capabilities.ExecuteCommandProvider.Commands, types.CodeFixDiffsCommand)
assert.Contains(t, result.Capabilities.ExecuteCommandProvider.Commands, types.ExecuteCLICommand)
}

func Test_initialize_shouldSupportDocumentSaving(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions domain/ide/command/command_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ func CreateFromCommandData(c *config.Config, commandData types.CommandData, srv
issueProvider: issueProvider,
notifier: notifier,
}, nil
case types.ExecuteCLICommand:
return &executeCLICommand{command: commandData, authService: authService, notifier: notifier, logger: c.Logger()}, nil
}

return nil, fmt.Errorf("unknown command %v", commandData)
Expand Down
58 changes: 58 additions & 0 deletions domain/ide/command/execute_cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* © 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"
"fmt"

"github.com/rs/zerolog"

"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
}

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)

var args []string
for _, argument := range cmd.command.Arguments[1:] {
args = append(args, argument.(string))
}
resp, err := cmd.cli.Execute(ctx, args, workDir)
if err != nil {
return nil, err
}
return string(resp), nil
}
3 changes: 2 additions & 1 deletion internal/types/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const (
GetFeatureFlagStatus = "snyk.getFeatureFlagStatus"
GetActiveUserCommand = "snyk.getActiveUser"
ReportAnalyticsCommand = "snyk.reportAnalytics"
ExecuteCLICommand = "snyk.executeCLI"

// Snyk Code specific commands
CodeFixCommand = "snyk.code.fix"
Expand Down Expand Up @@ -100,7 +101,7 @@ func NewCommandServiceMock() *CommandServiceMock {
return &CommandServiceMock{}
}

func (service *CommandServiceMock) ExecuteCommandData(_ context.Context, command CommandData, server Server) (any, error) {
func (service *CommandServiceMock) ExecuteCommandData(_ context.Context, command CommandData, _ Server) (any, error) {
service.m.Lock()
service.executedCommands = append(service.executedCommands, command)
service.m.Unlock()
Expand Down

0 comments on commit a586b0e

Please sign in to comment.