Skip to content

Commit f0e2f66

Browse files
committed
save provider metadata for Docker LSP
Signed-off-by: Guillaume Lours <[email protected]>
1 parent c0def9e commit f0e2f66

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

pkg/compose/plugins.go

Lines changed: 19 additions & 6 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,16 @@ 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+
logrus.Debugf("failed to create plugin metadata directory: %v", err)
238+
}
239+
metadataFilePath := filepath.Join(metadataDir, command+".json")
240+
if err := os.WriteFile(metadataFilePath, stdout.Bytes(), 0o600); err != nil {
241+
logrus.Debugf("failed to save plugin metadata: %v", err)
242+
}
230243
return metadata
231244
}
232245

0 commit comments

Comments
 (0)