Skip to content

Commit fee8aee

Browse files
gloursndeloof
authored andcommitted
save provider metadata for Docker LSP
Signed-off-by: Guillaume Lours <705411+glours@users.noreply.github.com>
1 parent 40f5786 commit fee8aee

1 file changed

Lines changed: 24 additions & 10 deletions

File tree

pkg/compose/plugins.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ import (
2525
"io"
2626
"os"
2727
"os/exec"
28+
"path/filepath"
2829
"strings"
2930

3031
"github.com/compose-spec/compose-go/v2/types"
3132
"github.com/docker/cli/cli-plugins/manager"
3233
"github.com/docker/cli/cli-plugins/socket"
34+
"github.com/docker/cli/cli/config"
3335
"github.com/docker/compose/v2/pkg/progress"
3436
"github.com/sirupsen/logrus"
3537
"github.com/spf13/cobra"
@@ -43,10 +45,11 @@ type JsonMessage struct {
4345
}
4446

4547
const (
46-
ErrorType = "error"
47-
InfoType = "info"
48-
SetEnvType = "setenv"
49-
DebugType = "debug"
48+
ErrorType = "error"
49+
InfoType = "info"
50+
SetEnvType = "setenv"
51+
DebugType = "debug"
52+
providerMetadataDirectory = "compose/providers"
5053
)
5154

5255
func (s *composeService) runPlugin(ctx context.Context, project *types.Project, service types.ServiceConfig, command string) error {
@@ -162,7 +165,7 @@ func (s *composeService) getPluginBinaryPath(provider string) (path string, err
162165
}
163166

164167
func (s *composeService) setupPluginCommand(ctx context.Context, project *types.Project, service types.ServiceConfig, path, command string) *exec.Cmd {
165-
cmdOptionsMetadata := s.getPluginMetadata(path)
168+
cmdOptionsMetadata := s.getPluginMetadata(path, service.Provider.Type)
166169
var currentCommandMetadata CommandMetadata
167170
switch command {
168171
case "up":
@@ -211,7 +214,7 @@ func (s *composeService) setupPluginCommand(ctx context.Context, project *types.
211214
return cmd
212215
}
213216

214-
func (s *composeService) getPluginMetadata(path string) ProviderMetadata {
217+
func (s *composeService) getPluginMetadata(path, command string) ProviderMetadata {
215218
cmd := exec.Command(path, "compose", "metadata")
216219
stdout := &bytes.Buffer{}
217220
cmd.Stdout = stdout
@@ -227,6 +230,17 @@ func (s *composeService) getPluginMetadata(path string) ProviderMetadata {
227230
logrus.Debugf("failed to decode plugin metadata: %v - %s", err, output)
228231
return ProviderMetadata{}
229232
}
233+
// Save metadata into docker home directory to be used by Docker LSP tool
234+
// Just log the error as it's not a critical error for the main flow
235+
metadataDir := filepath.Join(config.Dir(), providerMetadataDirectory)
236+
if err := os.MkdirAll(metadataDir, 0o700); err == nil {
237+
metadataFilePath := filepath.Join(metadataDir, command+".json")
238+
if err := os.WriteFile(metadataFilePath, stdout.Bytes(), 0o600); err != nil {
239+
logrus.Debugf("failed to save plugin metadata: %v", err)
240+
}
241+
} else {
242+
logrus.Debugf("failed to create plugin metadata directory: %v", err)
243+
}
230244
return metadata
231245
}
232246

@@ -237,22 +251,22 @@ type ProviderMetadata struct {
237251
}
238252

239253
type CommandMetadata struct {
240-
Parameters []ParametersMetadata `json:"parameters"`
254+
Parameters []ParameterMetadata `json:"parameters"`
241255
}
242256

243-
type ParametersMetadata struct {
257+
type ParameterMetadata struct {
244258
Name string `json:"name"`
245259
Description string `json:"description"`
246260
Required bool `json:"required"`
247261
Type string `json:"type"`
248262
Default string `json:"default,omitempty"`
249263
}
250264

251-
func (c CommandMetadata) GetParameter(paramName string) (ParametersMetadata, bool) {
265+
func (c CommandMetadata) GetParameter(paramName string) (ParameterMetadata, bool) {
252266
for _, p := range c.Parameters {
253267
if p.Name == paramName {
254268
return p, true
255269
}
256270
}
257-
return ParametersMetadata{}, false
271+
return ParameterMetadata{}, false
258272
}

0 commit comments

Comments
 (0)