Skip to content

Commit

Permalink
Merge pull request #246 from wttech/bugfix/fix-instance-location-mism…
Browse files Browse the repository at this point in the history
…atch

Remove creating Instance structs by URL alone
  • Loading branch information
krystian-panek-vmltech authored Apr 11, 2024
2 parents 079ff6c + 5d36af5 commit cbc6f2f
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 27 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,16 +201,19 @@ import aemc "github.com/wttech/aemc/pkg"

func main() {
aem := aemc.DefaultAEM()
instance, _ := aem.InstanceManager().NewByURL("http://admin:[email protected]:4502")
instance, _ := aem.InstanceManager().NewByIDAndURL("remote_author", "http://admin:[email protected]:4502")
changed, err := instance.PackageManager().DeployWithChanged("/tmp/my-package.zip")
if err != nil {
fmt.Printf("cannot deploy package: %s\n", err)
os.Exit(1)
}
if changed {
aem.InstanceManager().AwaitStartedOne(*instance)
if err := aem.InstanceManager().AwaitStartedOne(*instance); err != nil {
fmt.Printf("instance not stable after deploying package: %s\n", err)
os.Exit(1)
}
}
fmt.Printf("package deployed properly\n")
fmt.Println("package deployed properly")
os.Exit(0)
}
```
Expand Down
8 changes: 0 additions & 8 deletions pkg/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"github.com/fatih/color"
"github.com/samber/lo"
log "github.com/sirupsen/logrus"
"github.com/wttech/aemc/pkg/common/fmtx"
"github.com/wttech/aemc/pkg/instance"
Expand Down Expand Up @@ -181,13 +180,6 @@ func (i Instance) IsAdHoc() bool {
return i.IDInfo().Role == instance.RoleAdHoc
}

func locationByURL(config *nurl.URL) string {
if lo.Contains(localHosts(), config.Hostname()) {
return instance.LocationLocal
}
return instance.LocationRemote
}

func roleByURL(config *nurl.URL) instance.Role {
if strings.HasSuffix(config.Port(), instance.RoleAuthorPortSuffix) {
return instance.RoleAuthor
Expand Down
18 changes: 3 additions & 15 deletions pkg/instance_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (im *InstanceManager) newFromConfig(id string) *Instance {
return nil
}

i, err := im.NewByURL(httpURL)
i, err := im.NewByIDAndURL(id, httpURL)
if err != nil {
log.Fatalf("cannot create instance from config with ID '%s' using URL '%s': %s", id, httpURL, err)
return nil
Expand Down Expand Up @@ -223,12 +223,12 @@ func (im *InstanceManager) Publishes() []Instance {
}

func (im *InstanceManager) NewLocalAuthor() Instance {
i, _ := im.NewByURL(instance.URLLocalAuthor)
i, _ := im.NewByIDAndURL(instance.LocationLocal+instance.IDDelimiter+string(instance.RoleAuthor), instance.URLLocalAuthor)
return *i
}

func (im *InstanceManager) NewLocalPublish() Instance {
i, _ := im.NewByURL(instance.URLLocalPublish)
i, _ := im.NewByIDAndURL(instance.LocationLocal+instance.IDDelimiter+string(instance.RolePublish), instance.URLLocalPublish)
return *i
}

Expand All @@ -240,18 +240,6 @@ func (im *InstanceManager) NewByID(id string) *Instance {
return im.newFromConfig(id)
}

func (im *InstanceManager) NewByURL(url string) (*Instance, error) {
urlConfig, err := nurl.Parse(url)
if err != nil {
return nil, fmt.Errorf("invalid instance URL '%s': %w", url, err)
}
location := locationByURL(urlConfig)
role := roleByURL(urlConfig)
parts := []string{location, string(role)}
id := strings.Join(parts, instance.IDDelimiter)
return im.NewByIDAndURL(id, url)
}

func (im *InstanceManager) NewByIDAndURL(id string, url string) (*Instance, error) {
urlConfig, err := nurl.Parse(url)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/local_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ func (li LocalInstance) awaitAuth() error {
}
_, err := li.instance.osgi.bundleManager.List()
if err != nil {
defaultInstance, err := li.instance.manager.NewByURL(li.instance.http.BaseURL())
defaultInstance, err := li.instance.manager.NewByIDAndURL(li.instance.id, li.instance.http.BaseURL())
if err != nil {
return err
}
Expand Down

0 comments on commit cbc6f2f

Please sign in to comment.