Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor instance filters #283

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions cmd/aem/instance_list_int_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//go:build int_test

package main

import (
"github.com/wttech/aemc/pkg"
"sort"
"strings"
"testing"
)

func testInstanceList(t *testing.T, args []string, expectedIDs []string) {
cli := NewCLI()
cmd := cli.cmd
cmd.SetArgs(args)

defer func() {
actualIDs := []string{}
if recover() != nil {
instances := cli.outputResponse.Data["instances"].([]pkg.Instance)
for _, i := range instances {
actualIDs = append(actualIDs, i.ID())
}
}
sort.SliceStable(actualIDs, func(i, j int) bool {
return strings.Compare(actualIDs[i], actualIDs[j]) < 0
})
sort.SliceStable(expectedIDs, func(i, j int) bool {
return strings.Compare(expectedIDs[i], expectedIDs[j]) < 0
})
result := len(actualIDs) == len(expectedIDs)
for i := range actualIDs {
result = result && actualIDs[i] == expectedIDs[i]
}
if !result {
t.Errorf("InstanceList(%v) = %v; want %v", args, actualIDs, expectedIDs)
}
}()

_ = cmd.Execute()
}

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

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

func TestPublishInstances(t *testing.T) {
testInstanceList(t, []string{"instance", "list", "-P", "--output-value", "NONE"}, []string{"local_publish"})
}

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:[email protected]:4502", "-U", "http://admin:[email protected]:4503", "-U", "test_author=http://admin:[email protected]:4502", "--output-value", "NONE"}, []string{"remote_adhoc_1", "remote_adhoc_2", "test_author"})
}

func TestAuthorInstanceByURL(t *testing.T) {
testInstanceList(t, []string{"instance", "list", "-U", "dev-auth_author=http://admin:[email protected]:4502", "-U", "dev-pub1_publish=http://admin:[email protected]:4503", "-U", "dev-pub2_publish=http://admin:[email protected]: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:[email protected]:4502", "-U", "dev-pub1_publish=http://admin:[email protected]:4503", "-U", "dev-pub2_publish=http://admin:[email protected]: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:[email protected]:4502", "-U", "dev-pub1_publish=http://admin:[email protected]:4503", "-U", "dev-pub2_publish=http://admin:[email protected]: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:[email protected]:4502", "-U", "dev-pub1_publish=http://admin:[email protected]:4503", "-U", "dev-pub2_publish=http://admin:[email protected]: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:[email protected]:4502", "-U", "dev-pub1_publish=http://admin:[email protected]:4503", "-U", "dev-pub2_publish=http://admin:[email protected]:4504", "-I", "dev-auth_author", "-I", "dev-pub1_publish", "--output-value", "NONE"}, []string{"dev-auth_author", "dev-pub1_publish"})
}
6 changes: 3 additions & 3 deletions cmd/aem/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ func (c *CLI) rootFlags(cmd *cobra.Command) {
cmd.PersistentFlags().StringSliceP("instance-url", "U", cv.GetStringSlice("instance.adhoc_url"), "Use only AEM instance(s) at ad-hoc specified URL(s)")
_ = cv.BindPFlag("instance.adhoc_url", cmd.PersistentFlags().Lookup("instance-url"))

cmd.PersistentFlags().StringP("instance-id", "I", cv.GetString("instance.filter.id"), "Use only AEM instance configured with the exact ID")
cmd.PersistentFlags().StringSliceP("instance-id", "I", cv.GetStringSlice("instance.filter.id"), "Use only AEM instance(s) configured with the exact ID")
_ = cv.BindPFlag("instance.filter.id", cmd.PersistentFlags().Lookup("instance-id"))

cmd.PersistentFlags().BoolP("instance-author", "A", cv.GetBool("instance.filter.authors"), "Use only AEM author instance")
cmd.PersistentFlags().BoolP("instance-author", "A", cv.GetBool("instance.filter.authors"), "Use only AEM author instance(s)")
_ = cv.BindPFlag("instance.filter.authors", cmd.PersistentFlags().Lookup("instance-author"))

cmd.PersistentFlags().BoolP("instance-publish", "P", cv.GetBool("instance.filter.publishes"), "Use only AEM publish instance")
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")
Expand Down
31 changes: 9 additions & 22 deletions pkg/instance_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type InstanceManager struct {

AdHocURLs []string

FilterID string
FilterIDs []string
FilterAuthors bool
FilterPublishes bool
ProcessingMode string
Expand All @@ -35,7 +35,7 @@ func NewInstanceManager(aem *AEM) *InstanceManager {

result.AdHocURLs = cv.GetStringSlice("instance.adhoc_url")

result.FilterID = cv.GetString("instance.filter.id")
result.FilterIDs = cv.GetStringSlice("instance.filter.id")
result.FilterAuthors = cv.GetBool("instance.filter.authors")
result.FilterPublishes = cv.GetBool("instance.filter.publishes")
result.ProcessingMode = cv.GetString("instance.processing_mode")
Expand Down Expand Up @@ -165,28 +165,15 @@ func (im *InstanceManager) newFromConfig(id string) *Instance {

func (im *InstanceManager) filter(instances []Instance) []Instance {
result := []Instance{}
if im.FilterID != "" {
for _, i := range instances {
if i.id == im.FilterID {
for _, i := range instances {
if im.FilterAuthors == im.FilterPublishes || im.FilterAuthors && i.IsAuthor() || im.FilterPublishes && i.IsPublish() {
if len(im.FilterIDs) == 0 {
result = append(result, i)
break
}
}
} else {
if im.FilterAuthors == im.FilterPublishes {
result = instances
} else {
if im.FilterAuthors {
for _, i := range instances {
if i.IsAuthor() {
result = append(result, i)
}
}
}
if im.FilterPublishes {
for _, i := range instances {
if i.IsPublish() {
} else {
for _, filterID := range im.FilterIDs {
if i.id == filterID {
result = append(result, i)
break
}
}
}
Expand Down
Loading