Skip to content

Commit a72e076

Browse files
committed
Handle Softaculous edge case
1 parent 8aca61e commit a72e076

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

plugins_softaculous.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package directadmin
22

33
import (
4+
"encoding/json"
45
"errors"
56
"fmt"
67
"net/http"
@@ -203,37 +204,46 @@ func (c *UserContext) SoftaculousInstallScript(script *SoftaculousScript, script
203204

204205
// SoftaculousListInstallations lists all installations accessible to the authenticated user.
205206
func (c *UserContext) SoftaculousListInstallations() ([]*SoftaculousInstallation, error) {
206-
response := struct {
207-
Error map[string]string `json:"error"`
208-
Installations map[string]map[string]*SoftaculousInstallation `json:"installations"`
209-
}{
210-
Error: make(map[string]string),
211-
Installations: make(map[string]map[string]*SoftaculousInstallation),
207+
type rawResponse struct {
208+
Error map[string]string `json:"error"`
209+
Installations json.RawMessage `json:"installations"`
212210
}
213211

214-
// Softaculous requires a genuine session ID
212+
var raw rawResponse
213+
215214
if c.sessionID == "" {
216215
if err := c.CreateSession(); err != nil {
217216
return nil, fmt.Errorf("failed to create user session: %w", err)
218217
}
219218
}
220219

221-
if _, err := c.makeRequestOld(http.MethodPost, "PLUGINS/softaculous/index.raw?act=installations&api=json", nil, &response); err != nil {
220+
if _, err := c.makeRequestOld(http.MethodPost, "PLUGINS/softaculous/index.raw?act=installations&api=json", nil, &raw); err != nil {
222221
return nil, err
223222
}
224223

225-
if len(response.Error) > 0 {
226-
return nil, fmt.Errorf("failed to uninstall script: %v", response.Error)
224+
if len(raw.Error) > 0 {
225+
return nil, fmt.Errorf("failed to list installations: %v", raw.Error)
227226
}
228227

229-
installations := make([]*SoftaculousInstallation, 0, len(response.Installations))
230-
for _, userInstallations := range response.Installations {
231-
for _, installation := range userInstallations {
232-
installations = append(installations, installation)
228+
// Try unmarshalling as a map first.
229+
var installationsMap map[string]map[string]*SoftaculousInstallation
230+
if err := json.Unmarshal(raw.Installations, &installationsMap); err == nil {
231+
var installations []*SoftaculousInstallation
232+
for _, userInstalls := range installationsMap {
233+
for _, install := range userInstalls {
234+
installations = append(installations, install)
235+
}
233236
}
237+
return installations, nil
238+
}
239+
240+
// Fallback: Check if it's an empty array.
241+
var installationsArray []any
242+
if err := json.Unmarshal(raw.Installations, &installationsArray); err == nil && len(installationsArray) == 0 {
243+
return []*SoftaculousInstallation{}, nil
234244
}
235245

236-
return installations, nil
246+
return nil, errors.New("unexpected format for installations field")
237247
}
238248

239249
// SoftaculousUninstallScript calls Softaculous's install script API endpoint.

0 commit comments

Comments
 (0)