Skip to content

Commit 9867e1d

Browse files
Replication fixed
1 parent 82cb9dd commit 9867e1d

File tree

5 files changed

+84
-7
lines changed

5 files changed

+84
-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: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package pkg
22

33
import (
44
"fmt"
5+
"io"
6+
57
log "github.com/sirupsen/logrus"
68
"github.com/spf13/cast"
79
"github.com/wttech/aemc/pkg/replication"
810
"github.com/wttech/aemc/pkg/sling"
9-
"io"
1011
)
1112

1213
type Replication struct {
@@ -63,16 +64,16 @@ func (r Replication) replicate(cmd string, path string) error {
6364
} else if response.IsError() {
6465
return fmt.Errorf("%s > cannot do replication command '%s' for path '%s': %s", r.instance.IDColor(), cmd, path, response.Status())
6566
}
66-
htmlBytes, err := io.ReadAll(response.RawBody())
67+
jsonBytes, err := io.ReadAll(response.RawBody())
6768
if err != nil {
6869
return fmt.Errorf("%s > cannot read replication command '%s' response for path '%s': %w", r.instance.IDColor(), cmd, path, err)
6970
}
70-
htmlData, err := sling.HtmlData(string(htmlBytes))
71+
jsonData, err := sling.JsonData(string(jsonBytes))
7172
if err != nil {
72-
return fmt.Errorf("%s > cannot parse replication command '%s' response for path '%s': %w", r.instance.IDColor(), cmd, path, err)
73+
return fmt.Errorf("%s > cannot parse replication command '%s' JSON response for path '%s': %w", r.instance.IDColor(), cmd, path, err)
7374
}
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)
75+
if jsonData.IsError() {
76+
return fmt.Errorf("%s > replication command '%s' failed for path '%s': %s", r.instance.IDColor(), cmd, path, jsonData.Message)
7677
}
7778
return nil
7879
}

pkg/sling/html_response.go

Lines changed: 2 additions & 1 deletion
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 {

pkg/sling/json_response.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 <= 0 || d.Status > 399
34+
}

0 commit comments

Comments
 (0)