Skip to content

Commit

Permalink
Change argument syntax
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Wessendorf <mwessend@redhat.com>
  • Loading branch information
matzew committed Oct 11, 2023
1 parent 365bb02 commit 555d90e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
51 changes: 38 additions & 13 deletions cmd/subscribe.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cmd

import (
"fmt"
"strings"

"github.com/ory/viper"
"github.com/spf13/cobra"
fn "knative.dev/func/pkg/functions"
Expand All @@ -18,19 +21,20 @@ func NewSubscribeCmd() *cobra.Command {
},
}

cmd.Flags().StringP("filter", "f", "", "The event metadata to filter for")
// cmd.Flags().StringP("filter", "f", "", "Filter for the Cloud Event metadata")
cmd.Flags().StringArrayP("filter", "f", []string{}, "Filter for the Cloud Event metadata")

cmd.Flags().StringP("source", "s", "default", "The source, like a Knative Broker")

return cmd
}

// /
func runSubscribe(cmd *cobra.Command, args []string) (err error) {
var (
cfg subscibeConfig
f fn.Function
)
cfg = newSubscribeConfig()
cfg = newSubscribeConfig(cmd)

if f, err = fn.NewFunction(""); err != nil {
return
Expand All @@ -42,29 +46,50 @@ func runSubscribe(cmd *cobra.Command, args []string) (err error) {
return fn.NewErrNotInitialized(f.Root)
}

// add it
// add subscription to function
f.Subscription = append(f.Subscription, fn.SubscriptionSpec{
Source: cfg.Source,
Filters: map[string]string{
"type": cfg.Filter,
},
Source: cfg.Source,
Filters: extractFilterMap(cfg),
})

// pump it
return f.Write()

}

func extractFilterMap(cfg subscibeConfig) map[string]string {
//separatedFilters := strings.Split(cfg.Filter, ",")
subscriptionFilters := make(map[string]string)
for _, filter := range cfg.Filter {
kv := strings.Split(filter, "=")
if len(kv) != 2 {
fmt.Println("Invalid pair:", filter)
continue
}
key := kv[0]
value := kv[1]
subscriptionFilters[key] = value
}
return subscriptionFilters
}

type subscibeConfig struct {
Filter string
Filter []string
Source string
}

func newSubscribeConfig() subscibeConfig {
c := subscibeConfig{
Filter: viper.GetString("filter"),
func newSubscribeConfig(cmd *cobra.Command) (c subscibeConfig) {
c = subscibeConfig{
Filter: viper.GetStringSlice("filter"),
Source: viper.GetString("source"),
}
// NOTE: .Filter should be viper.GetStringSlice, but this returns unparsed
// results and appears to be an open issue since 2017:
// https://github.com/spf13/viper/issues/380
var err error
if c.Filter, err = cmd.Flags().GetStringArray("filter"); err != nil {
fmt.Fprintf(cmd.OutOrStdout(), "error reading filter arguments: %v", err)
}

return c
return
}
6 changes: 3 additions & 3 deletions docs/reference/func_subscribe.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ func subscribe
### Options

```
-f, --filter string The event metadata to filter for
-h, --help help for subscribe
-s, --source string The source, like a Knative Broker (default "default")
-f, --filter stringArray Filter for the Cloud Event metadata
-h, --help help for subscribe
-s, --source string The source, like a Knative Broker (default "default")
```

### SEE ALSO
Expand Down

0 comments on commit 555d90e

Please sign in to comment.