Skip to content

Commit b7a311a

Browse files
fixed cppst qualit gate
1 parent a00ce0d commit b7a311a

3 files changed

Lines changed: 124 additions & 218 deletions

File tree

src/app/project_runner.go

Lines changed: 56 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,102 +1165,72 @@ func (p *ProjectRunner) UpdateProcesses(processes *types.Processes) (map[string]
11651165
// Returns map of process name -> result ("ok" or error).
11661166
// If namespace contains no processes, returns ErrNamespaceNotFound.
11671167
func (p *ProjectRunner) StopNamespace(namespace string) (map[string]string, error) {
1168-
states, err := p.GetProcessesState()
1169-
if err != nil {
1170-
return nil, err
1171-
}
1172-
1173-
names := make([]string, 0)
1174-
for _, state := range states.States {
1175-
if state.Namespace == namespace {
1176-
names = append(names, state.Name)
1177-
}
1178-
}
1179-
if len(names) == 0 {
1180-
return nil, ErrNamespaceNotFound
1181-
}
1182-
1183-
return p.StopProcesses(names)
1168+
names, err := p.getProcessNamesInNamespace(namespace)
1169+
if err != nil {
1170+
return nil, err
1171+
}
1172+
return p.StopProcesses(names)
11841173
}
11851174

11861175
// DisableNamespace sets Disabled=true for all processes in the namespace.
11871176
// Returns per-process results map and error if any failures occurred.
11881177
func (p *ProjectRunner) DisableNamespace(namespace string) (map[string]string, error) {
1189-
states, err := p.GetProcessesState()
1190-
if err != nil {
1191-
return nil, err
1192-
}
1193-
names := make([]string, 0)
1194-
for _, st := range states.States {
1195-
if st.Namespace == namespace {
1196-
names = append(names, st.Name)
1197-
}
1198-
}
1199-
if len(names) == 0 {
1200-
return nil, ErrNamespaceNotFound
1201-
}
1202-
1203-
results := make(map[string]string)
1204-
failures := 0
1205-
for _, name := range names {
1206-
cfg, err := p.GetProcessInfo(name)
1207-
if err != nil {
1208-
results[name] = err.Error()
1209-
failures++
1210-
continue
1211-
}
1212-
cfg.Disabled = true
1213-
if err := p.UpdateProcess(cfg); err != nil {
1214-
results[name] = err.Error()
1215-
failures++
1216-
} else {
1217-
results[name] = "ok"
1218-
}
1219-
}
1220-
if failures > 0 {
1221-
return results, errors.New("failed to disable some processes")
1222-
}
1223-
return results, nil
1178+
return p.updateNamespaceDisabled(namespace, true, "failed to disable some processes")
12241179
}
12251180

12261181
// EnableNamespace sets Disabled=false for all processes in the namespace.
12271182
// Returns per-process results map and error if any failures occurred.
12281183
func (p *ProjectRunner) EnableNamespace(namespace string) (map[string]string, error) {
1229-
states, err := p.GetProcessesState()
1230-
if err != nil {
1231-
return nil, err
1232-
}
1233-
names := make([]string, 0)
1234-
for _, st := range states.States {
1235-
if st.Namespace == namespace {
1236-
names = append(names, st.Name)
1237-
}
1238-
}
1239-
if len(names) == 0 {
1240-
return nil, ErrNamespaceNotFound
1241-
}
1242-
1243-
results := make(map[string]string)
1244-
failures := 0
1245-
for _, name := range names {
1246-
cfg, err := p.GetProcessInfo(name)
1247-
if err != nil {
1248-
results[name] = err.Error()
1249-
failures++
1250-
continue
1251-
}
1252-
cfg.Disabled = false
1253-
if err := p.UpdateProcess(cfg); err != nil {
1254-
results[name] = err.Error()
1255-
failures++
1256-
} else {
1257-
results[name] = "ok"
1258-
}
1259-
}
1260-
if failures > 0 {
1261-
return results, errors.New("failed to enable some processes")
1262-
}
1263-
return results, nil
1184+
return p.updateNamespaceDisabled(namespace, false, "failed to enable some processes")
1185+
}
1186+
1187+
// getProcessNamesInNamespace returns all process names in the given namespace
1188+
// or ErrNamespaceNotFound if none exist.
1189+
func (p *ProjectRunner) getProcessNamesInNamespace(namespace string) ([]string, error) {
1190+
states, err := p.GetProcessesState()
1191+
if err != nil {
1192+
return nil, err
1193+
}
1194+
names := make([]string, 0)
1195+
for _, st := range states.States {
1196+
if st.Namespace == namespace {
1197+
names = append(names, st.Name)
1198+
}
1199+
}
1200+
if len(names) == 0 {
1201+
return nil, ErrNamespaceNotFound
1202+
}
1203+
return names, nil
1204+
}
1205+
1206+
// updateNamespaceDisabled applies the Disabled flag value to all processes
1207+
// within a namespace and returns per-process results and a partial failure error when needed.
1208+
func (p *ProjectRunner) updateNamespaceDisabled(namespace string, disabled bool, partialMsg string) (map[string]string, error) {
1209+
names, err := p.getProcessNamesInNamespace(namespace)
1210+
if err != nil {
1211+
return nil, err
1212+
}
1213+
results := make(map[string]string)
1214+
failures := 0
1215+
for _, name := range names {
1216+
cfg, err := p.GetProcessInfo(name)
1217+
if err != nil {
1218+
results[name] = err.Error()
1219+
failures++
1220+
continue
1221+
}
1222+
cfg.Disabled = disabled
1223+
if err := p.UpdateProcess(cfg); err != nil {
1224+
results[name] = err.Error()
1225+
failures++
1226+
} else {
1227+
results[name] = "ok"
1228+
}
1229+
}
1230+
if failures > 0 {
1231+
return results, errors.New(partialMsg)
1232+
}
1233+
return results, nil
12641234
}
12651235

12661236
func (p *ProjectRunner) RemoveNamespace(namespace string) (map[string]string, error) {

src/client/client.go

Lines changed: 55 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package client
22

33
import (
4-
"bytes"
5-
"context"
6-
"encoding/json"
7-
"errors"
8-
"fmt"
9-
"net"
10-
"net/http"
11-
"sync"
12-
"time"
4+
"bytes"
5+
"context"
6+
"encoding/json"
7+
"errors"
8+
"fmt"
9+
"io"
10+
"net"
11+
"net/http"
12+
"sync"
13+
"time"
1314

1415
"github.com/f1bonacc1/process-compose/src/api"
1516
"github.com/f1bonacc1/process-compose/src/pclog"
@@ -190,39 +191,49 @@ func (p *PcClient) TruncateProcessLogs(name string) error {
190191
}
191192

192193
func (p *PcClient) UpdateProcesses(processes *types.Processes) (map[string]string, error) {
193-
url := fmt.Sprintf("http://%s/processes", p.address)
194-
jsonData, err := json.Marshal(processes)
195-
if err != nil {
196-
log.Err(err).Msg("failed to marshal processes")
197-
return nil, err
198-
}
199-
req, err := http.NewRequest(http.MethodPut, url, bytes.NewBuffer(jsonData))
200-
if err != nil {
201-
return nil, err
202-
}
203-
resp, err := p.client.Do(req)
204-
if err != nil {
205-
log.Err(err).Msg("failed to update processes")
206-
return nil, err
207-
}
208-
defer resp.Body.Close()
209-
if resp.StatusCode == http.StatusOK {
210-
status := map[string]string{}
211-
if err = json.NewDecoder(resp.Body).Decode(&status); err != nil {
212-
log.Err(err).Msg("failed to decode updated processes")
213-
return status, err
214-
}
215-
log.Info().Msgf("status: %v", status)
216-
return status, nil
217-
}
218-
var respErr pcError
219-
if err = json.NewDecoder(resp.Body).Decode(&respErr); err != nil {
220-
log.Err(err).Msg("failed to decode err update processes")
221-
return nil, err
222-
}
223-
return nil, errors.New(respErr.Error)
224-
}
225-
226-
func (p *PcClient) RemoveNamespace(name string) (map[string]string, error) {
227-
return nil, errors.New("remove namespace not implemented for PC client")
194+
url := fmt.Sprintf("http://%s/namespace", p.address)
195+
jsonData, err := json.Marshal(processes)
196+
if err != nil {
197+
log.Err(err).Msg("failed to marshal processes")
198+
return nil, err
199+
}
200+
// treat 400 as partial failure and still return the map
201+
return p.doMapRequest(http.MethodPut, url, bytes.NewBuffer(jsonData), "failed to update some processes")
202+
}
203+
204+
// doMapRequest executes an HTTP request, expecting a JSON body with
205+
// shape map[string]string on success (200) or partial failure (400).
206+
// For non-200/400 responses, it decodes pcError and returns it as error.
207+
func (p *PcClient) doMapRequest(method, url string, body io.Reader, partialErrMsg string) (map[string]string, error) {
208+
req, err := http.NewRequest(method, url, body)
209+
if err != nil {
210+
return nil, err
211+
}
212+
resp, err := p.client.Do(req)
213+
if err != nil {
214+
return nil, err
215+
}
216+
defer resp.Body.Close()
217+
218+
if resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusBadRequest {
219+
results := map[string]string{}
220+
if err = json.NewDecoder(resp.Body).Decode(&results); err != nil {
221+
log.Err(err).Msg("failed to decode map response")
222+
return nil, err
223+
}
224+
if resp.StatusCode == http.StatusBadRequest {
225+
if partialErrMsg == "" {
226+
partialErrMsg = "partial failure"
227+
}
228+
return results, errors.New(partialErrMsg)
229+
}
230+
return results, nil
231+
}
232+
233+
var respErr pcError
234+
if err = json.NewDecoder(resp.Body).Decode(&respErr); err != nil {
235+
log.Err(err).Msg("failed to decode error response")
236+
return nil, err
237+
}
238+
return nil, errors.New(respErr.Error)
228239
}

src/client/namespace.go

Lines changed: 13 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,32 @@
11
package client
22

33
import (
4-
"encoding/json"
5-
"errors"
64
"fmt"
75
"net/http"
8-
9-
"github.com/rs/zerolog/log"
106
)
117

8+
// namespacePatch is a helper to perform PATCH actions on namespace endpoints
9+
// and unify response decoding and error handling.
10+
func (p *PcClient) namespacePatch(url string, partialErrMsg string) (map[string]string, error) {
11+
return p.doMapRequest(http.MethodPatch, url, nil, partialErrMsg)
12+
}
13+
1214
func (p *PcClient) StopNamespace(name string) (map[string]string, error) {
1315
url := fmt.Sprintf("http://%s/namespace/stop/%s", p.address, name)
14-
req, err := http.NewRequest(http.MethodPatch, url, nil)
15-
if err != nil {
16-
return nil, err
17-
}
18-
resp, err := p.client.Do(req)
19-
if err != nil {
20-
return nil, err
21-
}
22-
defer resp.Body.Close()
23-
24-
if resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusBadRequest {
25-
stopped := map[string]string{}
26-
if err = json.NewDecoder(resp.Body).Decode(&stopped); err != nil {
27-
log.Err(err).Msg("failed to decode stop namespace response")
28-
return stopped, err
29-
}
30-
if resp.StatusCode == http.StatusBadRequest {
31-
return stopped, errors.New("failed to stop some processes")
32-
}
33-
return stopped, nil
34-
}
35-
36-
var respErr pcError
37-
if err = json.NewDecoder(resp.Body).Decode(&respErr); err != nil {
38-
log.Err(err).Msg("failed to decode stop namespace error")
39-
return nil, err
40-
}
41-
return nil, errors.New(respErr.Error)
16+
return p.namespacePatch(url, "failed to stop some processes")
4217
}
4318

4419
func (p *PcClient) DisableNamespace(name string) (map[string]string, error) {
4520
url := fmt.Sprintf("http://%s/namespace/disable/%s", p.address, name)
46-
req, err := http.NewRequest(http.MethodPatch, url, nil)
47-
if err != nil {
48-
return nil, err
49-
}
50-
resp, err := p.client.Do(req)
51-
if err != nil {
52-
return nil, err
53-
}
54-
defer resp.Body.Close()
55-
56-
if resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusBadRequest {
57-
results := map[string]string{}
58-
if err = json.NewDecoder(resp.Body).Decode(&results); err != nil {
59-
log.Err(err).Msg("failed to decode disable namespace response")
60-
return results, err
61-
}
62-
if resp.StatusCode == http.StatusBadRequest {
63-
return results, errors.New("failed to disable some processes")
64-
}
65-
return results, nil
66-
}
67-
68-
var respErr pcError
69-
if err = json.NewDecoder(resp.Body).Decode(&respErr); err != nil {
70-
log.Err(err).Msg("failed to decode disable namespace error")
71-
return nil, err
72-
}
73-
return nil, errors.New(respErr.Error)
21+
return p.namespacePatch(url, "failed to disable some processes")
7422
}
7523

7624
func (p *PcClient) EnableNamespace(name string) (map[string]string, error) {
7725
url := fmt.Sprintf("http://%s/namespace/enable/%s", p.address, name)
78-
req, err := http.NewRequest(http.MethodPatch, url, nil)
79-
if err != nil {
80-
return nil, err
81-
}
82-
resp, err := p.client.Do(req)
83-
if err != nil {
84-
return nil, err
85-
}
86-
defer resp.Body.Close()
87-
88-
if resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusBadRequest {
89-
results := map[string]string{}
90-
if err = json.NewDecoder(resp.Body).Decode(&results); err != nil {
91-
log.Err(err).Msg("failed to decode enable namespace response")
92-
return results, err
93-
}
94-
if resp.StatusCode == http.StatusBadRequest {
95-
return results, errors.New("failed to enable some processes")
96-
}
97-
return results, nil
98-
}
99-
100-
var respErr pcError
101-
if err = json.NewDecoder(resp.Body).Decode(&respErr); err != nil {
102-
log.Err(err).Msg("failed to decode enable namespace error")
103-
return nil, err
104-
}
105-
return nil, errors.New(respErr.Error)
26+
return p.namespacePatch(url, "failed to enable some processes")
10627
}
10728

29+
func (p *PcClient) RemoveNamespace(name string) (map[string]string, error) {
30+
url := fmt.Sprintf("http://%s/namespace?name=%s", p.address, name)
31+
return p.doMapRequest(http.MethodDelete, url, nil, "failed to remove some processes")
32+
}

0 commit comments

Comments
 (0)