Skip to content

Commit

Permalink
added saw
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Dec 6, 2024
1 parent a5a8d83 commit 16e6cd7
Show file tree
Hide file tree
Showing 15 changed files with 326 additions and 0 deletions.
18 changes: 18 additions & 0 deletions completers/saw_completer/cmd/completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/spf13/cobra"
)

var completionCmd = &cobra.Command{
Use: "completion",
Short: "Generate the autocompletion script for the specified shell",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(completionCmd).Standalone()

rootCmd.AddCommand(completionCmd)
}
19 changes: 19 additions & 0 deletions completers/saw_completer/cmd/completion_bash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/spf13/cobra"
)

var completion_bashCmd = &cobra.Command{
Use: "bash",
Short: "Generate the autocompletion script for bash",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(completion_bashCmd).Standalone()

completion_bashCmd.Flags().Bool("no-descriptions", false, "disable completion descriptions")
completionCmd.AddCommand(completion_bashCmd)
}
19 changes: 19 additions & 0 deletions completers/saw_completer/cmd/completion_fish.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/spf13/cobra"
)

var completion_fishCmd = &cobra.Command{
Use: "fish",
Short: "Generate the autocompletion script for fish",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(completion_fishCmd).Standalone()

completion_fishCmd.Flags().Bool("no-descriptions", false, "disable completion descriptions")
completionCmd.AddCommand(completion_fishCmd)
}
19 changes: 19 additions & 0 deletions completers/saw_completer/cmd/completion_powershell.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/spf13/cobra"
)

var completion_powershellCmd = &cobra.Command{
Use: "powershell",
Short: "Generate the autocompletion script for powershell",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(completion_powershellCmd).Standalone()

completion_powershellCmd.Flags().Bool("no-descriptions", false, "disable completion descriptions")
completionCmd.AddCommand(completion_powershellCmd)
}
19 changes: 19 additions & 0 deletions completers/saw_completer/cmd/completion_zsh.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/spf13/cobra"
)

var completion_zshCmd = &cobra.Command{
Use: "zsh",
Short: "Generate the autocompletion script for zsh",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(completion_zshCmd).Standalone()

completion_zshCmd.Flags().Bool("no-descriptions", false, "disable completion descriptions")
completionCmd.AddCommand(completion_zshCmd)
}
31 changes: 31 additions & 0 deletions completers/saw_completer/cmd/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/saw"
"github.com/spf13/cobra"
)

var getCmd = &cobra.Command{
Use: "get <log group>",
Short: "Get log events",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(getCmd).Standalone()

getCmd.Flags().Bool("expand", false, "indent JSON log messages")
getCmd.Flags().String("filter", "", "event filter pattern")
getCmd.Flags().Bool("invert", false, "invert colors for light terminal themes")
getCmd.Flags().String("prefix", "", "log group prefix filter")
getCmd.Flags().Bool("pretty", false, "print timestamp and stream name prefix")
getCmd.Flags().Bool("rawString", false, "print JSON strings without escaping")
getCmd.Flags().String("start", "", "start getting the logs from this point")
getCmd.Flags().String("stop", "", "stop getting the logs at this point")
rootCmd.AddCommand(getCmd)

carapace.Gen(getCmd).PositionalCompletion(
saw.ActionGroups(),
)
}
19 changes: 19 additions & 0 deletions completers/saw_completer/cmd/groups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/spf13/cobra"
)

var groupsCmd = &cobra.Command{
Use: "groups",
Short: "List log groups",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(groupsCmd).Standalone()

groupsCmd.Flags().String("prefix", "", "log group prefix filter")
rootCmd.AddCommand(groupsCmd)
}
18 changes: 18 additions & 0 deletions completers/saw_completer/cmd/help.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/spf13/cobra"
)

var helpCmd = &cobra.Command{
Use: "help [command]",
Short: "Help about any command",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(helpCmd).Standalone()

rootCmd.AddCommand(helpCmd)
}
44 changes: 44 additions & 0 deletions completers/saw_completer/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/aws"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

var rootCmd = &cobra.Command{
Use: "saw <command>",
Short: "A fast, multipurpose tool for AWS CloudWatch Logs",
Long: "https://github.com/TylerBrock/saw",
Run: func(cmd *cobra.Command, args []string) {},
}

func Execute() error {
return rootCmd.Execute()
}

func init() {
carapace.Gen(rootCmd).Standalone()

rootCmd.PersistentFlags().String("endpoint-url", "", "override default endpoint URL")
rootCmd.PersistentFlags().String("profile", "", "override default AWS profile")
rootCmd.PersistentFlags().String("region", "", "override profile AWS region")

carapace.Gen(rootCmd).FlagCompletion(carapace.ActionMap{
"profile": aws.ActionProfiles(),
"region": aws.ActionRegions(),
})

carapace.Gen(rootCmd).PreInvoke(func(cmd *cobra.Command, flag *pflag.Flag, action carapace.Action) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if f := cmd.Flag("profile"); f.Changed {
c.Setenv("AWS_PROFILE", f.Value.String())
}
if f := cmd.Flag("region"); f.Changed {
c.Setenv("AWS_REGION", f.Value.String())
}
return action.Invoke(c).ToA()
})
})
}
26 changes: 26 additions & 0 deletions completers/saw_completer/cmd/streams.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/saw"
"github.com/spf13/cobra"
)

var streamsCmd = &cobra.Command{
Use: "streams <log group>",
Short: "List streams in log group",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(streamsCmd).Standalone()

streamsCmd.Flags().Bool("descending", false, "order streams descending")
streamsCmd.Flags().String("orderBy", "", "order streams by LogStreamName or LastEventTime")
streamsCmd.Flags().String("prefix", "", "stream prefix filter")
rootCmd.AddCommand(streamsCmd)

carapace.Gen(streamsCmd).PositionalCompletion(
saw.ActionGroups(),
)
}
18 changes: 18 additions & 0 deletions completers/saw_completer/cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/spf13/cobra"
)

var versionCmd = &cobra.Command{
Use: "version",
Short: "Prints the version string",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(versionCmd).Standalone()

rootCmd.AddCommand(versionCmd)
}
29 changes: 29 additions & 0 deletions completers/saw_completer/cmd/watch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/saw"
"github.com/spf13/cobra"
)

var watchCmd = &cobra.Command{
Use: "watch <log group>",
Short: "Continuously stream log events",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(watchCmd).Standalone()

watchCmd.Flags().Bool("expand", false, "indent JSON log messages")
watchCmd.Flags().String("filter", "", "event filter pattern")
watchCmd.Flags().Bool("invert", false, "invert colors for light terminal themes")
watchCmd.Flags().String("prefix", "", "log stream prefix filter")
watchCmd.Flags().Bool("raw", false, "print raw log event without timestamp or stream prefix")
watchCmd.Flags().Bool("rawString", false, "print JSON strings without escaping")
rootCmd.AddCommand(watchCmd)

carapace.Gen(watchCmd).PositionalCompletion(
saw.ActionGroups(),
)
}
7 changes: 7 additions & 0 deletions completers/saw_completer/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/carapace-sh/carapace-bin/completers/saw_completer/cmd"

func main() {
cmd.Execute()
}
20 changes: 20 additions & 0 deletions pkg/actions/tools/saw/group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package saw

import (
"strings"

"github.com/carapace-sh/carapace"
)

// ActionGroups completes log groups.
//
// lambda/one
// ecs/two
func ActionGroups() carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return carapace.ActionExecCommand("saw", "groups", "--prefix", c.Value)(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")
return carapace.ActionValues(lines...)
})
}).Tag("groups")
}
20 changes: 20 additions & 0 deletions pkg/actions/tools/saw/stream.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package saw

import (
"strings"

"github.com/carapace-sh/carapace"
)

// ActionStreams completes log streams.
//
// streamOne
// streamTwo
func ActionStreams(group string) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return carapace.ActionExecCommand("saw", "streams", "--prefix", c.Value, group)(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")
return carapace.ActionValues(lines...)
})
}).Tag("streams")
}

0 comments on commit 16e6cd7

Please sign in to comment.