Skip to content

Commit e890c56

Browse files
Merge pull request #326 from wttech/replication-fix
Replication fixed
2 parents 82cb9dd + 6cfea97 commit e890c56

File tree

6 files changed

+111
-7
lines changed

6 files changed

+111
-7
lines changed

.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,16 @@ $RECYCLE.BIN/
223223
dist/
224224
**/aem/home/
225225
!pkg/instance/resource
226+
227+
228+
# VSCode
229+
.vscode/*
230+
!.vscode/settings.json
231+
!.vscode/tasks.json
232+
!.vscode/launch.json
233+
!.vscode/extensions.json
234+
!.vscode/*.code-snippets
235+
!*.code-workspace
236+
237+
# Built Visual Studio Code Extensions
238+
*.vsix

.vscode/launch.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Print help",
9+
"type": "go",
10+
"request": "launch",
11+
"mode": "auto",
12+
"program": "${workspaceFolder}/cmd/aem",
13+
"args": ["--help"],
14+
"env": {},
15+
"cwd": "${workspaceFolder}"
16+
},
17+
{
18+
"name": "Replicate node",
19+
"type": "go",
20+
"request": "launch",
21+
"mode": "auto",
22+
"program": "${workspaceFolder}/cmd/aem",
23+
"args": ["repl", "activate", "-A", "--path", "/content/projects/jcr:content"],
24+
"env": {},
25+
"cwd": "${workspaceFolder}"
26+
}
27+
]
28+
}

pkg/repl.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package pkg
22

33
import (
44
"fmt"
5+
"io"
6+
"strings"
7+
58
log "github.com/sirupsen/logrus"
69
"github.com/spf13/cast"
710
"github.com/wttech/aemc/pkg/replication"
811
"github.com/wttech/aemc/pkg/sling"
9-
"io"
1012
)
1113

1214
type Replication struct {
@@ -51,6 +53,7 @@ func (r Replication) Deactivate(path string) error {
5153
return nil
5254
}
5355

56+
// replicate a path to a specific agent; respect response format (older AEM uses HTML, newer uses JSON)
5457
func (r Replication) replicate(cmd string, path string) error {
5558
response, err := r.instance.http.Request().
5659
SetFormData(map[string]string{
@@ -63,16 +66,27 @@ func (r Replication) replicate(cmd string, path string) error {
6366
} else if response.IsError() {
6467
return fmt.Errorf("%s > cannot do replication command '%s' for path '%s': %s", r.instance.IDColor(), cmd, path, response.Status())
6568
}
66-
htmlBytes, err := io.ReadAll(response.RawBody())
69+
70+
responseBytes, err := io.ReadAll(response.RawBody())
6771
if err != nil {
6872
return fmt.Errorf("%s > cannot read replication command '%s' response for path '%s': %w", r.instance.IDColor(), cmd, path, err)
6973
}
70-
htmlData, err := sling.HtmlData(string(htmlBytes))
74+
75+
contentType := response.Header().Get("Content-Type")
76+
responseBody := string(responseBytes)
77+
78+
var responseData sling.ResponseData
79+
if strings.Contains(strings.ToLower(contentType), "application/json") ||
80+
(strings.HasPrefix(strings.TrimSpace(responseBody), "{") && strings.HasSuffix(strings.TrimSpace(responseBody), "}")) {
81+
responseData, err = sling.JsonData(responseBody)
82+
} else {
83+
responseData, err = sling.HtmlData(responseBody)
84+
}
7185
if err != nil {
7286
return fmt.Errorf("%s > cannot parse replication command '%s' response for path '%s': %w", r.instance.IDColor(), cmd, path, err)
7387
}
74-
if htmlData.IsError() {
75-
return fmt.Errorf("%s > cannot do replication command '%s' for path '%s': %s", r.instance.IDColor(), cmd, path, htmlData.Message)
88+
if responseData.IsError() {
89+
return fmt.Errorf("%s > replication command '%s' failed for path '%s': %s", r.instance.IDColor(), cmd, path, responseData.GetMessage())
7690
}
7791
return nil
7892
}

pkg/sling/html_response.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package sling
22

33
import (
4+
"strings"
5+
46
"github.com/PuerkitoBio/goquery"
57
"github.com/spf13/cast"
6-
"strings"
78
)
89

910
type HTMLData struct {
@@ -34,7 +35,11 @@ func HtmlData(html string) (data HTMLData, err error) {
3435
}
3536

3637
func (d HTMLData) IsError() bool {
37-
return d.Status <= 0 || d.Status > 399
38+
return d.Status < 200 || d.Status >= 400
39+
}
40+
41+
func (d HTMLData) GetMessage() string {
42+
return d.Message
3843
}
3944

4045
func htmlElementText(doc *goquery.Document, selector string, defaultValue string) string {

pkg/sling/json_response.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package sling
2+
3+
import (
4+
"encoding/json"
5+
6+
"github.com/spf13/cast"
7+
)
8+
9+
type JSONData struct {
10+
Status int
11+
Message string
12+
Path []string
13+
StatusMessage string
14+
StatusCode int
15+
}
16+
17+
func JsonData(jsonStr string) (data JSONData, err error) {
18+
var rawData map[string]any
19+
if err := json.Unmarshal([]byte(jsonStr), &rawData); err != nil {
20+
return data, err
21+
}
22+
23+
data.Path = cast.ToStringSlice(rawData["path"])
24+
data.StatusMessage = cast.ToString(rawData["status.message"])
25+
data.Message = data.StatusMessage
26+
data.StatusCode = cast.ToInt(rawData["status.code"])
27+
data.Status = data.StatusCode
28+
29+
return data, nil
30+
}
31+
32+
func (d JSONData) IsError() bool {
33+
return d.Status < 200 || d.Status >= 400
34+
}
35+
36+
func (d JSONData) GetMessage() string {
37+
return d.Message
38+
}

pkg/sling/response.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package sling
2+
3+
type ResponseData interface {
4+
IsError() bool
5+
GetMessage() string
6+
}

0 commit comments

Comments
 (0)