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 6 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
89 changes: 89 additions & 0 deletions cmd/aem/instance_list_int_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//go:build int_test

package main

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

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

defer func() {
err := recover()
actualResult := err != nil
if actualResult != expectedResult {
t.Errorf("InstanceList(%v) = %v; want %v", args, actualResult, expectedResult)
} else if actualResult && expectedResult {
actualIDs := []string{}
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"}, true, []string{"local_author", "local_publish"})
}

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

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

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

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"}, true, []string{"remote_adhoc_1", "remote_adhoc_2", "test_author"})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

much better with adhoc urls ;) thx

}

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"}, true, []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"}, true, []string{"dev-pub1_publish", "dev-pub2_publish"})
}

func TestConflictAuthorAndPublish(t *testing.T) {
testInstanceList(t, []string{"instance", "list", "-A", "-P"}, false, []string{})
}

func TestConflictByIDAndAuthor(t *testing.T) {
testInstanceList(t, []string{"instance", "list", "-I", "local_author", "-A"}, false, []string{})
}

func TestConflictByIDAndPublish(t *testing.T) {
testInstanceList(t, []string{"instance", "list", "-I", "local_author", "-P"}, false, []string{})
}

func TestConflictIDAndURL(t *testing.T) {
testInstanceList(t, []string{"instance", "list", "-I", "local_author", "-U", "http://admin:[email protected]:4502"}, false, []string{})
}
9 changes: 5 additions & 4 deletions cmd/aem/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,17 @@ 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")
cmd.MarkFlagsMutuallyExclusive("instance-url", "instance-id")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is wrong; you could always define all urls and then filter one to be used only with instance-id

cmd.MarkFlagsMutuallyExclusive("instance-id", "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"))
Expand Down
33 changes: 11 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,30 +165,19 @@ func (im *InstanceManager) newFromConfig(id string) *Instance {

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