Skip to content

Commit d2274eb

Browse files
committed
display proper event message for provider services on up and down
Signed-off-by: Guillaume Lours <705411+glours@users.noreply.github.com>
1 parent 6e35652 commit d2274eb

1 file changed

Lines changed: 42 additions & 19 deletions

File tree

pkg/compose/plugins.go

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,33 @@ func (s *composeService) runPlugin(ctx context.Context, project *types.Project,
6161

6262
cmd := s.setupPluginCommand(ctx, project, provider, plugin.Path, command)
6363

64+
variables, err := s.executePlugin(ctx, cmd, command, service)
65+
if err != nil {
66+
return err
67+
}
68+
69+
for name, s := range project.Services {
70+
if _, ok := s.DependsOn[service.Name]; ok {
71+
prefix := strings.ToUpper(service.Name) + "_"
72+
for key, val := range variables {
73+
s.Environment[prefix+key] = &val
74+
}
75+
project.Services[name] = s
76+
}
77+
}
78+
return nil
79+
}
80+
81+
func (s *composeService) executePlugin(ctx context.Context, cmd *exec.Cmd, command string, service types.ServiceConfig) (types.Mapping, error) {
6482
eg := errgroup.Group{}
6583
stdout, err := cmd.StdoutPipe()
6684
if err != nil {
67-
return err
85+
return nil, err
6886
}
6987

7088
err = cmd.Start()
7189
if err != nil {
72-
return err
90+
return nil, err
7391
}
7492
eg.Go(cmd.Wait)
7593

@@ -79,50 +97,55 @@ func (s *composeService) runPlugin(ctx context.Context, project *types.Project,
7997
variables := types.Mapping{}
8098

8199
pw := progress.ContextWriter(ctx)
82-
pw.Event(progress.CreatingEvent(service.Name))
100+
var action string
101+
switch command {
102+
case "up":
103+
pw.Event(progress.CreatingEvent(service.Name))
104+
action = "create"
105+
case "down":
106+
pw.Event(progress.RemovingEvent(service.Name))
107+
action = "remove"
108+
default:
109+
return nil, fmt.Errorf("unsupported plugin command: %s", command)
110+
}
83111
for {
84112
var msg JsonMessage
85113
err = decoder.Decode(&msg)
86114
if errors.Is(err, io.EOF) {
87115
break
88116
}
89117
if err != nil {
90-
return err
118+
return nil, err
91119
}
92120
switch msg.Type {
93121
case ErrorType:
94122
pw.Event(progress.ErrorMessageEvent(service.Name, "error"))
95-
return errors.New(msg.Message)
123+
return nil, errors.New(msg.Message)
96124
case InfoType:
97125
pw.Event(progress.ErrorMessageEvent(service.Name, msg.Message))
98126
case SetEnvType:
99127
key, val, found := strings.Cut(msg.Message, "=")
100128
if !found {
101-
return fmt.Errorf("invalid response from plugin: %s", msg.Message)
129+
return nil, fmt.Errorf("invalid response from plugin: %s", msg.Message)
102130
}
103131
variables[key] = val
104132
default:
105-
return fmt.Errorf("invalid response from plugin: %s", msg.Type)
133+
return nil, fmt.Errorf("invalid response from plugin: %s", msg.Type)
106134
}
107135
}
108136

109137
err = eg.Wait()
110138
if err != nil {
111139
pw.Event(progress.ErrorMessageEvent(service.Name, err.Error()))
112-
return fmt.Errorf("failed to create external service: %s", err.Error())
140+
return nil, fmt.Errorf("failed to %s external service: %s", action, err.Error())
113141
}
114-
pw.Event(progress.CreatedEvent(service.Name))
115-
116-
prefix := strings.ToUpper(service.Name) + "_"
117-
for name, s := range project.Services {
118-
if _, ok := s.DependsOn[service.Name]; ok {
119-
for key, val := range variables {
120-
s.Environment[prefix+key] = &val
121-
}
122-
project.Services[name] = s
123-
}
142+
switch command {
143+
case "up":
144+
pw.Event(progress.CreatedEvent(service.Name))
145+
case "down":
146+
pw.Event(progress.RemovedEvent(service.Name))
124147
}
125-
return nil
148+
return variables, nil
126149
}
127150

128151
func (s *composeService) getPluginBinaryPath(providerType string) (*manager.Plugin, error) {

0 commit comments

Comments
 (0)