Skip to content

Commit

Permalink
add filters; docs improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ingon committed Jan 28, 2024
1 parent 47e6113 commit 74be880
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 20 deletions.
12 changes: 6 additions & 6 deletions egress_webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func egressWebhooksList() *cobra.Command {
metadata := cmd.Flags().String("metadata", "", "webhook metadata")

cmd.RunE = func(cmd *cobra.Command, args []string) error {
if md := *metadata; md != "" {
out, err := klient.EgressWebhooks.Find(cmd.Context(), md)
if cmd.Flags().Changed("metadata") {
out, err := klient.EgressWebhooks.Find(cmd.Context(), *metadata)
return output(out, err)
} else {
out, err := klient.EgressWebhooks.List(cmd.Context())
Expand Down Expand Up @@ -72,7 +72,7 @@ func egressWebhooksCreate() *cobra.Command {

func egressWebhooksGet() *cobra.Command {
return &cobra.Command{
Use: "get",
Use: "get <egress-webhook-id>",
Short: "get an egress webhook",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -84,7 +84,7 @@ func egressWebhooksGet() *cobra.Command {

func egressWebhooksRotate() *cobra.Command {
cmd := &cobra.Command{
Use: "rotate",
Use: "rotate <egress-webhook-id>",
Short: "rotate egress webhook secret",
Args: cobra.ExactArgs(1),
}
Expand All @@ -103,7 +103,7 @@ func egressWebhooksRotate() *cobra.Command {

func egressWebhooksStatus() *cobra.Command {
return &cobra.Command{
Use: "status",
Use: "status <egress-webhook-id>",
Short: "status an egress webhook",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -115,7 +115,7 @@ func egressWebhooksStatus() *cobra.Command {

func egressWebhooksDelete() *cobra.Command {
return &cobra.Command{
Use: "delete",
Use: "delete <egress-webhook-id>",
Short: "delete an egress webhook",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
108 changes: 108 additions & 0 deletions filters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package main

import (
"github.com/spf13/cobra"

"github.com/klev-dev/klev-api-go/filters"
"github.com/klev-dev/klev-api-go/logs"
)

func filtersRoot() *cobra.Command {
cmd := &cobra.Command{
Use: "filters",
Short: "interact with filters",
}

cmd.AddCommand(filtersList())
cmd.AddCommand(filtersCreate())
cmd.AddCommand(filtersGet())
cmd.AddCommand(filtersStatus())
cmd.AddCommand(filtersDelete())

return cmd
}

func filtersList() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "list filters",
}

metadata := cmd.Flags().String("metadata", "", "webhook metadata")

cmd.RunE = func(cmd *cobra.Command, args []string) error {
if cmd.Flags().Changed("metadata") {
out, err := klient.Filters.Find(cmd.Context(), *metadata)
return output(out, err)
} else {
out, err := klient.Filters.List(cmd.Context())
return output(out, err)
}
}

return cmd
}

func filtersCreate() *cobra.Command {
cmd := &cobra.Command{
Use: "create",
Short: "create new filter",
}

var in filters.CreateParams

sourceID := cmd.Flags().String("source-id", "", "source log id of the filter")
targetID := cmd.Flags().String("target-id", "", "target log id of the filter")
cmd.Flags().StringVar(&in.Metadata, "metadata", "", "machine readable metadata")
cmd.Flags().StringVar(&in.Expression, "expression", "", "expression to eval")

cmd.MarkFlagRequired("source-id")
cmd.MarkFlagRequired("target-id")
cmd.MarkFlagRequired("expression")

cmd.RunE = func(cmd *cobra.Command, args []string) error {
in.SourceID = logs.LogID(*sourceID)
in.TargetID = logs.LogID(*targetID)

out, err := klient.Filters.Create(cmd.Context(), in)
return output(out, err)
}

return cmd
}

func filtersGet() *cobra.Command {
return &cobra.Command{
Use: "get <filter-id>",
Short: "get a filter",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
out, err := klient.Filters.Get(cmd.Context(), filters.FilterID(args[0]))
return output(out, err)
},
}
}

func filtersStatus() *cobra.Command {
return &cobra.Command{
Use: "status <filter-id>",
Short: "status of a filter",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
out, err := klient.Filters.Status(cmd.Context(), filters.FilterID(args[0]))
return output(out, err)
},
}
}

func filtersDelete() *cobra.Command {
return &cobra.Command{
Use: "delete <filter-id>",
Short: "delete a filter",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
out, err := klient.Filters.Delete(cmd.Context(), filters.FilterID(args[0]))
return output(out, err)
},
}
}
10 changes: 5 additions & 5 deletions ingress_webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func ingressWebhooksList() *cobra.Command {
metadata := cmd.Flags().String("metadata", "", "webhook metadata")

cmd.RunE = func(cmd *cobra.Command, args []string) error {
if md := *metadata; md != "" {
out, err := klient.IngressWebhooks.Find(cmd.Context(), md)
if cmd.Flags().Changed("metadata") {
out, err := klient.IngressWebhooks.Find(cmd.Context(), *metadata)
return output(out, err)
} else {
out, err := klient.IngressWebhooks.List(cmd.Context())
Expand Down Expand Up @@ -72,7 +72,7 @@ func ingressWebhooksCreate() *cobra.Command {

func ingressWebhooksGet() *cobra.Command {
return &cobra.Command{
Use: "get",
Use: "get <ingress-webhook-id>",
Short: "get an ingress webhook",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -84,7 +84,7 @@ func ingressWebhooksGet() *cobra.Command {

func ingressWebhooksRotate() *cobra.Command {
cmd := &cobra.Command{
Use: "rotate",
Use: "rotate <ingress-webhook-id>",
Short: "rotate ingress webhook secret",
}

Expand All @@ -104,7 +104,7 @@ func ingressWebhooksRotate() *cobra.Command {

func ingressWebhooksDelete() *cobra.Command {
return &cobra.Command{
Use: "delete",
Use: "delete <ingress-webhook-id>",
Short: "delete an ingress webhook",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
4 changes: 2 additions & 2 deletions logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func logsCreate() *cobra.Command {

func logsGet() *cobra.Command {
return &cobra.Command{
Use: "get log_id",
Use: "get <log-id>",
Short: "get a log",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -77,7 +77,7 @@ func logsGet() *cobra.Command {

func logsDelete() *cobra.Command {
return &cobra.Command{
Use: "delete log_id",
Use: "delete <log-id>",
Short: "delete a log",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func main() {
rootCmd.AddCommand(tokensRoot())
rootCmd.AddCommand(ingressWebhooksRoot())
rootCmd.AddCommand(egressWebhooksRoot())
rootCmd.AddCommand(filtersRoot())

if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
Expand Down
4 changes: 2 additions & 2 deletions messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

func publish() *cobra.Command {
cmd := &cobra.Command{
Use: "publish log_id",
Use: "publish <log-id>",
Short: "publish a message",
Args: cobra.ExactArgs(1),
}
Expand Down Expand Up @@ -75,7 +75,7 @@ func publish() *cobra.Command {

func consume() *cobra.Command {
cmd := &cobra.Command{
Use: "consume log_id",
Use: "consume <log-id>",
Short: "consumes messages",
Args: cobra.ExactArgs(1),
}
Expand Down
6 changes: 3 additions & 3 deletions offsets.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func offsetsCreate() *cobra.Command {

func offsetsGet() *cobra.Command {
return &cobra.Command{
Use: "get offset_id",
Use: "get <offset-id>",
Short: "get an offset",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -78,7 +78,7 @@ func offsetsGet() *cobra.Command {

func offsetsSet() *cobra.Command {
cmd := &cobra.Command{
Use: "set offset_id",
Use: "set <offset-id>",
Short: "set log offset",
Args: cobra.ExactArgs(1),
}
Expand All @@ -99,7 +99,7 @@ func offsetsSet() *cobra.Command {

func offsetsDelete() *cobra.Command {
return &cobra.Command{
Use: "delete offset_id",
Use: "delete <offset-id>",
Short: "delete an offset",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
4 changes: 2 additions & 2 deletions tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func tokensCreate() *cobra.Command {

func tokensGet() *cobra.Command {
return &cobra.Command{
Use: "get token_id",
Use: "get <token-id>",
Short: "get a token",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -73,7 +73,7 @@ func tokensGet() *cobra.Command {

func tokensDelete() *cobra.Command {
return &cobra.Command{
Use: "delete token_id",
Use: "delete <token-id>",
Short: "delete a token",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down

0 comments on commit 74be880

Please sign in to comment.