@@ -45,7 +45,7 @@ and an 'extension' attribute for the value 'my-extension-value'.
45
45
46
46
func runSubscribe (cmd * cobra.Command ) (err error ) {
47
47
var (
48
- cfg subscibeConfig
48
+ cfg subscribeConfig
49
49
f fn.Function
50
50
)
51
51
cfg = newSubscribeConfig (cmd )
@@ -61,66 +61,69 @@ func runSubscribe(cmd *cobra.Command) (err error) {
61
61
}
62
62
63
63
// add subscription to function
64
- f .Deploy .Subscriptions = updateOrAddSubscription (f .Deploy .Subscriptions , cfg )
64
+ f .Deploy .Subscriptions = addNewSubscriptions (f .Deploy .Subscriptions , cfg )
65
65
66
66
// pump it
67
67
return f .Write ()
68
68
}
69
69
70
- func extractFilterMap (filters []string ) map [string ]string {
71
- subscriptionFilters := make (map [string ]string )
70
+ func extractNewSubscriptions (filters []string , source string ) (newSubscriptions []* subscriptionConfig ) {
72
71
for _ , filter := range filters {
73
72
kv := strings .Split (filter , "=" )
74
73
if len (kv ) != 2 {
75
74
fmt .Println ("Invalid pair:" , filter )
76
75
continue
77
76
}
78
- key := kv [0 ]
79
- value := kv [1 ]
80
- subscriptionFilters [key ] = value
77
+ filterKey := kv [0 ]
78
+ filterValue := kv [1 ]
79
+ newSubscriptions = append (newSubscriptions , & subscriptionConfig {
80
+ Source : source ,
81
+ FilterKey : filterKey ,
82
+ FilterValue : filterValue ,
83
+ })
81
84
}
82
- return subscriptionFilters
85
+ return newSubscriptions
86
+ }
87
+
88
+ type subscriptionConfig struct {
89
+ Source string
90
+ FilterKey string
91
+ FilterValue string
83
92
}
84
93
85
- type subscibeConfig struct {
94
+ type subscribeConfig struct {
86
95
Filter []string
87
96
Source string
88
97
}
89
98
90
- func updateOrAddSubscription (subscriptions []fn.KnativeSubscription , cfg subscibeConfig ) []fn.KnativeSubscription {
91
- found := false
92
- newFilters := extractFilterMap (cfg .Filter )
93
-
94
- // Iterate over subscriptions to find if one with the same source already exists
95
- for i , subscription := range subscriptions {
96
- if subscription .Source == cfg .Source {
97
- found = true
98
-
99
- if subscription .Filters == nil {
100
- subscription .Filters = make (map [string ]string )
99
+ func addNewSubscriptions (subscriptions []fn.KnativeSubscription , cfg subscribeConfig ) []fn.KnativeSubscription {
100
+ newSubscriptions := extractNewSubscriptions (cfg .Filter , cfg .Source )
101
+ for _ , newSubscription := range newSubscriptions {
102
+ isNew := true
103
+ for _ , subscription := range subscriptions {
104
+ if subscription .Source == newSubscription .Source {
105
+ for k , v := range subscription .Filters {
106
+ if k == newSubscription .FilterKey && v == newSubscription .FilterValue {
107
+ isNew = false
108
+ break
109
+ }
110
+ }
101
111
}
102
-
103
- // Update filters. Override if the key already exists.
104
- for newKey , newValue := range newFilters {
105
- subscription .Filters [newKey ] = newValue
106
- }
107
- subscriptions [i ] = subscription // Reassign the updated subscription
108
- break
109
112
}
110
- }
111
-
112
- // If a subscription with the source was not found, add a new one
113
- if ! found {
114
- subscriptions = append ( subscriptions , fn. KnativeSubscription {
115
- Source : cfg . Source ,
116
- Filters : newFilters ,
117
- })
113
+ if isNew {
114
+ newFilter := make ( map [ string ] string )
115
+ newFilter [ newSubscription . FilterKey ] = newSubscription . FilterValue
116
+ subscriptions = append ( subscriptions , fn. KnativeSubscription {
117
+ Source : cfg . Source ,
118
+ Filters : newFilter ,
119
+ })
120
+ }
118
121
}
119
122
return subscriptions
120
123
}
121
124
122
- func newSubscribeConfig (cmd * cobra.Command ) (c subscibeConfig ) {
123
- c = subscibeConfig {
125
+ func newSubscribeConfig (cmd * cobra.Command ) (c subscribeConfig ) {
126
+ c = subscribeConfig {
124
127
Filter : viper .GetStringSlice ("filter" ),
125
128
Source : viper .GetString ("source" ),
126
129
}
0 commit comments