Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik Przybyl committed Nov 22, 2024
1 parent da3a674 commit 69ac17e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 24 deletions.
18 changes: 13 additions & 5 deletions cmd/aem/instance_list_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,30 @@ func TestPublishInstances(t *testing.T) {
testInstanceList(t, []string{"instance", "list", "-P", "--output-value", "NONE"}, []string{"local_publish"})
}

func TestInstanceByID(t *testing.T) {
func TestIDInstance(t *testing.T) {
testInstanceList(t, []string{"instance", "list", "-I", "local_author", "--output-value", "NONE"}, []string{"local_author"})
}

func TestInstanceByURL(t *testing.T) {
testInstanceList(t, []string{"instance", "list", "-U", "http://admin:admin@127.0.0.1:4502", "-U", "http://admin:admin@127.0.0.1:4503", "-U", "test_author=http://admin:admin@127.0.0.1:4502", "--output-value", "NONE"}, []string{"remote_adhoc_1", "remote_adhoc_2", "test_author"})
}

func TestInstanceByIDOrURL(t *testing.T) {
testInstanceList(t, []string{"instance", "list", "-I", "local_publish", "-U", "http://admin:admin@127.0.0.1:4502", "--output-value", "NONE"}, []string{"local_publish", "remote_adhoc"})
}

func TestAuthorInstanceByURL(t *testing.T) {
testInstanceList(t, []string{"instance", "list", "-U", "dev-auth_author=http://admin:admin@127.0.0.1:4502", "-U", "dev-pub1_publish=http://admin:admin@127.0.0.1:4503", "-U", "dev-pub2_publish=http://admin:admin@127.0.0.1:4504", "-A", "--output-value", "NONE"}, []string{"dev-auth_author"})
}

func TestPublishInstanceByURL(t *testing.T) {
testInstanceList(t, []string{"instance", "list", "-U", "dev-auth_author=http://admin:admin@127.0.0.1:4502", "-U", "dev-pub1_publish=http://admin:admin@127.0.0.1:4503", "-U", "dev-pub2_publish=http://admin:admin@127.0.0.1:4504", "-P", "--output-value", "NONE"}, []string{"dev-pub1_publish", "dev-pub2_publish"})
}

func TestAuthorIDInstanceByURL(t *testing.T) {
testInstanceList(t, []string{"instance", "list", "-U", "dev-auth_author=http://admin:admin@127.0.0.1:4502", "-U", "dev-pub1_publish=http://admin:admin@127.0.0.1:4503", "-U", "dev-pub2_publish=http://admin:admin@127.0.0.1:4504", "-A", "-I", "dev-auth_author", "--output-value", "NONE"}, []string{"dev-auth_author"})
}

func TestPublishIDInstanceByURL(t *testing.T) {
testInstanceList(t, []string{"instance", "list", "-U", "dev-auth_author=http://admin:admin@127.0.0.1:4502", "-U", "dev-pub1_publish=http://admin:admin@127.0.0.1:4503", "-U", "dev-pub2_publish=http://admin:admin@127.0.0.1:4504", "-P", "-I", "dev-pub1_publish", "--output-value", "NONE"}, []string{"dev-pub1_publish"})
}

func TestIDInstanceByURL(t *testing.T) {
testInstanceList(t, []string{"instance", "list", "-U", "dev-auth_author=http://admin:admin@127.0.0.1:4502", "-U", "dev-pub1_publish=http://admin:admin@127.0.0.1:4503", "-U", "dev-pub2_publish=http://admin:admin@127.0.0.1:4504", "-I", "dev-auth_author", "-I", "dev-pub1_publish", "--output-value", "NONE"}, []string{"dev-auth_author", "dev-pub1_publish"})
}
2 changes: 2 additions & 0 deletions cmd/aem/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ func (c *CLI) rootFlags(cmd *cobra.Command) {
cmd.PersistentFlags().BoolP("instance-publish", "P", cv.GetBool("instance.filter.publishes"), "Use only AEM publish instance(s)")
_ = cv.BindPFlag("instance.filter.publishes", cmd.PersistentFlags().Lookup("instance-publish"))

cmd.MarkFlagsMutuallyExclusive("instance-author", "instance-publish")

cmd.PersistentFlags().String("instance-processing", cv.GetString("instance.processing_mode"), "Controls processing mode for instances ("+(strings.Join(instance.ProcessingModes(), "|")+")"))
_ = cv.BindPFlag("instance.processing_mode", cmd.PersistentFlags().Lookup("instance-processing"))
}
35 changes: 16 additions & 19 deletions pkg/instance_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,45 +84,33 @@ func (im *InstanceManager) All() []Instance {
}

func (im *InstanceManager) newAdHocOrFromConfig() []Instance {
var result []Instance
if len(im.AdHocURLs) > 0 {
var result []Instance
for adHocIndex, adHocValue := range im.AdHocURLs {
iURL, err := im.newAdhoc(adHocValue, adHocIndex, len(im.AdHocURLs))
if err != nil {
log.Fatalf("cannot create instance from ad hoc value '%s': %s", adHocValue, err)
}
result = append(result, *iURL)
}
return result
}
var instances []Instance
cv := im.aem.config.Values()
configIDs := maps.Keys(cv.GetStringMap("instance.config"))
if len(configIDs) > 0 {
var result []Instance
for _, id := range configIDs {
cv.SetDefault(fmt.Sprintf("instance.config.%s.active", id), true)
active := cv.GetBool(fmt.Sprintf("instance.config.%s.active", id))
if active {
if i := im.newFromConfig(id); i != nil {
instances = append(instances, *i)
result = append(result, *i)
}
}
}
} else {
instances = im.NewLocalPair()
return result
}
if len(im.FilterIDs) > 0 {
for _, i := range instances {
for _, filterID := range im.FilterIDs {
if i.id == filterID {
result = append(result, i)
break
}
}
}
} else if len(im.AdHocURLs) == 0 {
result = instances
}
return result
return im.NewLocalPair()
}

func (im *InstanceManager) newAdhoc(value string, current int, total int) (*Instance, error) {
Expand Down Expand Up @@ -179,7 +167,16 @@ func (im *InstanceManager) filter(instances []Instance) []Instance {
result := []Instance{}
for _, i := range instances {
if im.FilterAuthors == im.FilterPublishes || im.FilterAuthors && i.IsAuthor() || im.FilterPublishes && i.IsPublish() {
result = append(result, i)
if len(im.FilterIDs) == 0 {
result = append(result, i)
} else {
for _, filterID := range im.FilterIDs {
if i.id == filterID {
result = append(result, i)
break
}
}
}
}
}
sort.SliceStable(result, func(i, j int) bool {
Expand Down

0 comments on commit 69ac17e

Please sign in to comment.