diff --git a/egress_webhooks.go b/egress_webhooks.go index fb7d6bc..ae712b1 100644 --- a/egress_webhooks.go +++ b/egress_webhooks.go @@ -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()) @@ -72,7 +72,7 @@ func egressWebhooksCreate() *cobra.Command { func egressWebhooksGet() *cobra.Command { return &cobra.Command{ - Use: "get", + Use: "get ", Short: "get an egress webhook", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { @@ -84,7 +84,7 @@ func egressWebhooksGet() *cobra.Command { func egressWebhooksRotate() *cobra.Command { cmd := &cobra.Command{ - Use: "rotate", + Use: "rotate ", Short: "rotate egress webhook secret", Args: cobra.ExactArgs(1), } @@ -103,7 +103,7 @@ func egressWebhooksRotate() *cobra.Command { func egressWebhooksStatus() *cobra.Command { return &cobra.Command{ - Use: "status", + Use: "status ", Short: "status an egress webhook", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { @@ -115,7 +115,7 @@ func egressWebhooksStatus() *cobra.Command { func egressWebhooksDelete() *cobra.Command { return &cobra.Command{ - Use: "delete", + Use: "delete ", Short: "delete an egress webhook", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { diff --git a/filters.go b/filters.go new file mode 100644 index 0000000..6fa02fe --- /dev/null +++ b/filters.go @@ -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 ", + 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 ", + 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 ", + 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) + }, + } +} diff --git a/ingress_webhooks.go b/ingress_webhooks.go index cd29cfa..b57266d 100644 --- a/ingress_webhooks.go +++ b/ingress_webhooks.go @@ -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()) @@ -72,7 +72,7 @@ func ingressWebhooksCreate() *cobra.Command { func ingressWebhooksGet() *cobra.Command { return &cobra.Command{ - Use: "get", + Use: "get ", Short: "get an ingress webhook", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { @@ -84,7 +84,7 @@ func ingressWebhooksGet() *cobra.Command { func ingressWebhooksRotate() *cobra.Command { cmd := &cobra.Command{ - Use: "rotate", + Use: "rotate ", Short: "rotate ingress webhook secret", } @@ -104,7 +104,7 @@ func ingressWebhooksRotate() *cobra.Command { func ingressWebhooksDelete() *cobra.Command { return &cobra.Command{ - Use: "delete", + Use: "delete ", Short: "delete an ingress webhook", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { diff --git a/logs.go b/logs.go index 48e2607..81fbef8 100644 --- a/logs.go +++ b/logs.go @@ -65,7 +65,7 @@ func logsCreate() *cobra.Command { func logsGet() *cobra.Command { return &cobra.Command{ - Use: "get log_id", + Use: "get ", Short: "get a log", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { @@ -77,7 +77,7 @@ func logsGet() *cobra.Command { func logsDelete() *cobra.Command { return &cobra.Command{ - Use: "delete log_id", + Use: "delete ", Short: "delete a log", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { diff --git a/main.go b/main.go index 101c2e8..069129f 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/messages.go b/messages.go index dccf3f4..4cc9f5e 100644 --- a/messages.go +++ b/messages.go @@ -17,7 +17,7 @@ import ( func publish() *cobra.Command { cmd := &cobra.Command{ - Use: "publish log_id", + Use: "publish ", Short: "publish a message", Args: cobra.ExactArgs(1), } @@ -75,7 +75,7 @@ func publish() *cobra.Command { func consume() *cobra.Command { cmd := &cobra.Command{ - Use: "consume log_id", + Use: "consume ", Short: "consumes messages", Args: cobra.ExactArgs(1), } diff --git a/offsets.go b/offsets.go index 9ae48a0..d2ecfd1 100644 --- a/offsets.go +++ b/offsets.go @@ -66,7 +66,7 @@ func offsetsCreate() *cobra.Command { func offsetsGet() *cobra.Command { return &cobra.Command{ - Use: "get offset_id", + Use: "get ", Short: "get an offset", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { @@ -78,7 +78,7 @@ func offsetsGet() *cobra.Command { func offsetsSet() *cobra.Command { cmd := &cobra.Command{ - Use: "set offset_id", + Use: "set ", Short: "set log offset", Args: cobra.ExactArgs(1), } @@ -99,7 +99,7 @@ func offsetsSet() *cobra.Command { func offsetsDelete() *cobra.Command { return &cobra.Command{ - Use: "delete offset_id", + Use: "delete ", Short: "delete an offset", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { diff --git a/tokens.go b/tokens.go index af26f58..6825ac5 100644 --- a/tokens.go +++ b/tokens.go @@ -61,7 +61,7 @@ func tokensCreate() *cobra.Command { func tokensGet() *cobra.Command { return &cobra.Command{ - Use: "get token_id", + Use: "get ", Short: "get a token", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { @@ -73,7 +73,7 @@ func tokensGet() *cobra.Command { func tokensDelete() *cobra.Command { return &cobra.Command{ - Use: "delete token_id", + Use: "delete ", Short: "delete a token", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error {