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 2 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
19 changes: 15 additions & 4 deletions cmd/aem/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,27 @@ 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.PersistentFlags().BoolP("instance-local", "L", cv.GetBool("instance.filter.locals"), "Use only AEM local instance(s)")
_ = cv.BindPFlag("instance.filter.locals", cmd.PersistentFlags().Lookup("instance-local"))

cmd.PersistentFlags().BoolP("instance-remote", "R", cv.GetBool("instance.filter.remotes"), "Use only AEM remote instance(s)")
krystian-panek-vmltech marked this conversation as resolved.
Show resolved Hide resolved
_ = cv.BindPFlag("instance.filter.remotes", cmd.PersistentFlags().Lookup("instance-remote"))

cmd.PersistentFlags().StringP("instance-classifier-prefix", "C", cv.GetString("instance.filter.classifier-prefix"), "Use only AEM instance(s) with proper classifier prefix")
_ = cv.BindPFlag("instance.filter.classifier-prefix", cmd.PersistentFlags().Lookup("instance-classifier-prefix"))

cmd.MarkFlagsMutuallyExclusive("instance-url", "instance-id", "instance-author", "instance-publish")
cmd.MarkFlagsMutuallyExclusive("instance-url", "instance-id", "instance-local", "instance-remote")
cmd.MarkFlagsMutuallyExclusive("instance-url", "instance-id", "instance-classifier-prefix")
krystian-panek-vmltech marked this conversation as resolved.
Show resolved Hide resolved

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
56 changes: 31 additions & 25 deletions pkg/instance_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ type InstanceManager struct {

AdHocURLs []string

FilterID string
FilterAuthors bool
FilterPublishes bool
ProcessingMode string
FilterIDs []string
FilterAuthors bool
FilterPublishes bool
FilterLocals bool
FilterRemotes bool
FilterClassifierPrefix string
ProcessingMode string
}

func NewInstanceManager(aem *AEM) *InstanceManager {
Expand All @@ -35,9 +38,20 @@ 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")
if !result.FilterAuthors && !result.FilterPublishes {
result.FilterAuthors = true
result.FilterPublishes = true
}
result.FilterLocals = cv.GetBool("instance.filter.locals")
result.FilterRemotes = cv.GetBool("instance.filter.remotes")
if !result.FilterLocals && !result.FilterRemotes {
result.FilterLocals = true
result.FilterRemotes = true
}
result.FilterClassifierPrefix = cv.GetString("instance.filter.classifier-prefix")
result.ProcessingMode = cv.GetString("instance.processing_mode")

result.LocalOpts = NewLocalOpts(result)
Expand Down Expand Up @@ -165,30 +179,22 @@ 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 {
filterLocation := im.FilterAuthors && i.IsAuthor() || im.FilterPublishes && i.IsPublish()
filterRole := im.FilterLocals && i.IsLocal() || im.FilterRemotes && i.IsRemote()
filterClassifier := strings.HasPrefix(i.IDInfo().Classifier, im.FilterClassifierPrefix)
if filterLocation && filterRole && filterClassifier {
result = append(result, i)
}
}
}
Expand Down
Loading