mirror of https://github.com/knative/func.git
Merge 8f60d4c559
into 9e117217a6
This commit is contained in:
commit
301157ca66
|
@ -45,7 +45,7 @@ and an 'extension' attribute for the value 'my-extension-value'.
|
||||||
|
|
||||||
func runSubscribe(cmd *cobra.Command) (err error) {
|
func runSubscribe(cmd *cobra.Command) (err error) {
|
||||||
var (
|
var (
|
||||||
cfg subscibeConfig
|
cfg subscribeConfig
|
||||||
f fn.Function
|
f fn.Function
|
||||||
)
|
)
|
||||||
cfg = newSubscribeConfig(cmd)
|
cfg = newSubscribeConfig(cmd)
|
||||||
|
@ -61,66 +61,69 @@ func runSubscribe(cmd *cobra.Command) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// add subscription to function
|
// add subscription to function
|
||||||
f.Deploy.Subscriptions = updateOrAddSubscription(f.Deploy.Subscriptions, cfg)
|
f.Deploy.Subscriptions = addNewSubscriptions(f.Deploy.Subscriptions, cfg)
|
||||||
|
|
||||||
// pump it
|
// pump it
|
||||||
return f.Write()
|
return f.Write()
|
||||||
}
|
}
|
||||||
|
|
||||||
func extractFilterMap(filters []string) map[string]string {
|
func extractNewSubscriptions(filters []string, source string) (newSubscriptions []*subscriptionConfig) {
|
||||||
subscriptionFilters := make(map[string]string)
|
|
||||||
for _, filter := range filters {
|
for _, filter := range filters {
|
||||||
kv := strings.Split(filter, "=")
|
kv := strings.Split(filter, "=")
|
||||||
if len(kv) != 2 {
|
if len(kv) != 2 {
|
||||||
fmt.Println("Invalid pair:", filter)
|
fmt.Println("Invalid pair:", filter)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
key := kv[0]
|
filterKey := kv[0]
|
||||||
value := kv[1]
|
filterValue := kv[1]
|
||||||
subscriptionFilters[key] = value
|
newSubscriptions = append(newSubscriptions, &subscriptionConfig{
|
||||||
|
Source: source,
|
||||||
|
FilterKey: filterKey,
|
||||||
|
FilterValue: filterValue,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
return subscriptionFilters
|
return newSubscriptions
|
||||||
}
|
}
|
||||||
|
|
||||||
type subscibeConfig struct {
|
type subscriptionConfig struct {
|
||||||
|
Source string
|
||||||
|
FilterKey string
|
||||||
|
FilterValue string
|
||||||
|
}
|
||||||
|
|
||||||
|
type subscribeConfig struct {
|
||||||
Filter []string
|
Filter []string
|
||||||
Source string
|
Source string
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateOrAddSubscription(subscriptions []fn.KnativeSubscription, cfg subscibeConfig) []fn.KnativeSubscription {
|
func addNewSubscriptions(subscriptions []fn.KnativeSubscription, cfg subscribeConfig) []fn.KnativeSubscription {
|
||||||
found := false
|
newSubscriptions := extractNewSubscriptions(cfg.Filter, cfg.Source)
|
||||||
newFilters := extractFilterMap(cfg.Filter)
|
for _, newSubscription := range newSubscriptions {
|
||||||
|
isNew := true
|
||||||
// Iterate over subscriptions to find if one with the same source already exists
|
for _, subscription := range subscriptions {
|
||||||
for i, subscription := range subscriptions {
|
if subscription.Source == newSubscription.Source {
|
||||||
if subscription.Source == cfg.Source {
|
for k, v := range subscription.Filters {
|
||||||
found = true
|
if k == newSubscription.FilterKey && v == newSubscription.FilterValue {
|
||||||
|
isNew = false
|
||||||
if subscription.Filters == nil {
|
break
|
||||||
subscription.Filters = make(map[string]string)
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 isNew {
|
||||||
|
newFilter := make(map[string]string)
|
||||||
// If a subscription with the source was not found, add a new one
|
newFilter[newSubscription.FilterKey] = newSubscription.FilterValue
|
||||||
if !found {
|
subscriptions = append(subscriptions, fn.KnativeSubscription{
|
||||||
subscriptions = append(subscriptions, fn.KnativeSubscription{
|
Source: cfg.Source,
|
||||||
Source: cfg.Source,
|
Filters: newFilter,
|
||||||
Filters: newFilters,
|
})
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
return subscriptions
|
return subscriptions
|
||||||
}
|
}
|
||||||
|
|
||||||
func newSubscribeConfig(cmd *cobra.Command) (c subscibeConfig) {
|
func newSubscribeConfig(cmd *cobra.Command) (c subscribeConfig) {
|
||||||
c = subscibeConfig{
|
c = subscribeConfig{
|
||||||
Filter: viper.GetStringSlice("filter"),
|
Filter: viper.GetStringSlice("filter"),
|
||||||
Source: viper.GetString("source"),
|
Source: viper.GetString("source"),
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue