Skip to content

Commit

Permalink
added new instance filters
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik Przybyl committed Nov 21, 2024
1 parent 69b07c6 commit 001d0f1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 29 deletions.
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)")
_ = 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")

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
57 changes: 32 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,33 +179,26 @@ 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)
}
}
}

sort.SliceStable(result, func(i, j int) bool {
return strings.Compare(result[i].id, result[j].id) < 0
})
Expand Down

0 comments on commit 001d0f1

Please sign in to comment.