display proper event message for provider services on up and down

Signed-off-by: Guillaume Lours <705411+glours@users.noreply.github.com>
This commit is contained in:
Guillaume Lours 2025-04-28 13:02:37 +02:00
parent 6e35652182
commit d2274ebe6c

View File

@ -61,15 +61,33 @@ func (s *composeService) runPlugin(ctx context.Context, project *types.Project,
cmd := s.setupPluginCommand(ctx, project, provider, plugin.Path, command) cmd := s.setupPluginCommand(ctx, project, provider, plugin.Path, command)
eg := errgroup.Group{} variables, err := s.executePlugin(ctx, cmd, command, service)
stdout, err := cmd.StdoutPipe()
if err != nil { if err != nil {
return err return err
} }
for name, s := range project.Services {
if _, ok := s.DependsOn[service.Name]; ok {
prefix := strings.ToUpper(service.Name) + "_"
for key, val := range variables {
s.Environment[prefix+key] = &val
}
project.Services[name] = s
}
}
return nil
}
func (s *composeService) executePlugin(ctx context.Context, cmd *exec.Cmd, command string, service types.ServiceConfig) (types.Mapping, error) {
eg := errgroup.Group{}
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
err = cmd.Start() err = cmd.Start()
if err != nil { if err != nil {
return err return nil, err
} }
eg.Go(cmd.Wait) eg.Go(cmd.Wait)
@ -79,7 +97,17 @@ func (s *composeService) runPlugin(ctx context.Context, project *types.Project,
variables := types.Mapping{} variables := types.Mapping{}
pw := progress.ContextWriter(ctx) pw := progress.ContextWriter(ctx)
var action string
switch command {
case "up":
pw.Event(progress.CreatingEvent(service.Name)) pw.Event(progress.CreatingEvent(service.Name))
action = "create"
case "down":
pw.Event(progress.RemovingEvent(service.Name))
action = "remove"
default:
return nil, fmt.Errorf("unsupported plugin command: %s", command)
}
for { for {
var msg JsonMessage var msg JsonMessage
err = decoder.Decode(&msg) err = decoder.Decode(&msg)
@ -87,42 +115,37 @@ func (s *composeService) runPlugin(ctx context.Context, project *types.Project,
break break
} }
if err != nil { if err != nil {
return err return nil, err
} }
switch msg.Type { switch msg.Type {
case ErrorType: case ErrorType:
pw.Event(progress.ErrorMessageEvent(service.Name, "error")) pw.Event(progress.ErrorMessageEvent(service.Name, "error"))
return errors.New(msg.Message) return nil, errors.New(msg.Message)
case InfoType: case InfoType:
pw.Event(progress.ErrorMessageEvent(service.Name, msg.Message)) pw.Event(progress.ErrorMessageEvent(service.Name, msg.Message))
case SetEnvType: case SetEnvType:
key, val, found := strings.Cut(msg.Message, "=") key, val, found := strings.Cut(msg.Message, "=")
if !found { if !found {
return fmt.Errorf("invalid response from plugin: %s", msg.Message) return nil, fmt.Errorf("invalid response from plugin: %s", msg.Message)
} }
variables[key] = val variables[key] = val
default: default:
return fmt.Errorf("invalid response from plugin: %s", msg.Type) return nil, fmt.Errorf("invalid response from plugin: %s", msg.Type)
} }
} }
err = eg.Wait() err = eg.Wait()
if err != nil { if err != nil {
pw.Event(progress.ErrorMessageEvent(service.Name, err.Error())) pw.Event(progress.ErrorMessageEvent(service.Name, err.Error()))
return fmt.Errorf("failed to create external service: %s", err.Error()) return nil, fmt.Errorf("failed to %s external service: %s", action, err.Error())
} }
switch command {
case "up":
pw.Event(progress.CreatedEvent(service.Name)) pw.Event(progress.CreatedEvent(service.Name))
case "down":
prefix := strings.ToUpper(service.Name) + "_" pw.Event(progress.RemovedEvent(service.Name))
for name, s := range project.Services {
if _, ok := s.DependsOn[service.Name]; ok {
for key, val := range variables {
s.Environment[prefix+key] = &val
} }
project.Services[name] = s return variables, nil
}
}
return nil
} }
func (s *composeService) getPluginBinaryPath(providerType string) (*manager.Plugin, error) { func (s *composeService) getPluginBinaryPath(providerType string) (*manager.Plugin, error) {