Skip to content

Commit

Permalink
We do not want to add duplicated subscriptiosn
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Wessendorf <[email protected]>
  • Loading branch information
matzew committed Jan 11, 2024
1 parent 8a4e662 commit ac68eb1
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions cmd/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,10 @@ func runSubscribe(cmd *cobra.Command, args []string) (err error) {
}

// add subscription to function
f.Deploy.Subscriptions = append(f.Deploy.Subscriptions, fn.KnativeSubscription{
Source: cfg.Source,
Filters: extractFilterMap(cfg.Filter),
})
f.Deploy.Subscriptions = updateOrAddSubscription(f.Deploy.Subscriptions, cfg)

// pump it
return f.Write()

}

func extractFilterMap(filters []string) map[string]string {
Expand All @@ -91,6 +87,33 @@ type subscibeConfig struct {
Source string
}

func updateOrAddSubscription(subscriptions []fn.KnativeSubscription, cfg subscibeConfig) []fn.KnativeSubscription {
found := false
newFilters := extractFilterMap(cfg.Filter)

// Iterate over subscriptions to find if one with the same source already exists
for i, subscription := range subscriptions {
if subscription.Source == cfg.Source {
found = true
// Update filters. Override if the key already exists.
for newKey, newValue := range newFilters {
subscription.Filters[newKey] = newValue
}
subscriptions[i] = subscription // Reassign the updated subscription
break
}
}

// If a subscription with the source was not found, add a new one
if !found {
subscriptions = append(subscriptions, fn.KnativeSubscription{
Source: cfg.Source,
Filters: newFilters,
})
}
return subscriptions
}

func newSubscribeConfig(cmd *cobra.Command) (c subscibeConfig) {
c = subscibeConfig{
Filter: viper.GetStringSlice("filter"),
Expand Down

0 comments on commit ac68eb1

Please sign in to comment.