@@ -2,11 +2,13 @@ package pkg
22
33import (
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
1214type 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)
5457func (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}
0 commit comments