diff --git a/cmd/aem/instance_list_int_test.go b/cmd/aem/instance_list_int_test.go new file mode 100644 index 0000000..f427708 --- /dev/null +++ b/cmd/aem/instance_list_int_test.go @@ -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: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 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"}) +} diff --git a/cmd/aem/root.go b/cmd/aem/root.go index ca77954..de77da5 100644 --- a/cmd/aem/root.go +++ b/cmd/aem/root.go @@ -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") diff --git a/pkg/instance_manager.go b/pkg/instance_manager.go index 893ee77..0a24c0e 100644 --- a/pkg/instance_manager.go +++ b/pkg/instance_manager.go @@ -21,7 +21,7 @@ type InstanceManager struct { AdHocURLs []string - FilterID string + FilterIDs []string FilterAuthors bool FilterPublishes bool ProcessingMode string @@ -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") @@ -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 } } }