Skip to content

Commit

Permalink
match saw get output format to saw watch
Browse files Browse the repository at this point in the history
Changes the way the formatting occurs for the log events, so that
the format can be shared between the different commands. Also adds
the flags available on the watch command to the get command for
feature parity.
  • Loading branch information
will-ockmore authored and TylerBrock committed Sep 24, 2018
1 parent 6336105 commit 143b5f4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
18 changes: 10 additions & 8 deletions blade/blade.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,12 @@ func (b *Blade) GetLogStreams() []*cloudwatchlogs.LogStream {

// GetEvents gets events from AWS given the blade configuration
func (b *Blade) GetEvents() {
formatter := b.output.Formatter()
input := b.config.FilterLogEventsInput()

handlePage := func(page *cloudwatchlogs.FilterLogEventsOutput, lastPage bool) bool {
for _, event := range page.Events {
fmt.Println(*event.Message)
fmt.Print(formatEvent(formatter, event))
}
return !lastPage
}
Expand Down Expand Up @@ -131,7 +132,7 @@ func (b *Blade) StreamEvents() {
for _, event := range page.Events {
updateLastSeenTime(event.Timestamp)
if _, seen := seenEventIDs[*event.EventId]; !seen {
printEvent(formatter, event)
fmt.Print(formatEvent(formatter, event))
addSeenEventIDs(event.EventId)
}
}
Expand All @@ -151,8 +152,8 @@ func (b *Blade) StreamEvents() {
}
}

// printEvent prints a filtered CloudWatch log event using the provided formatter
func printEvent(formatter *colorjson.Formatter, event *cloudwatchlogs.FilteredLogEvent) {
// formatEvent returns a CloudWatch log event as a formatted string using the provided formatter
func formatEvent(formatter *colorjson.Formatter, event *cloudwatchlogs.FilteredLogEvent) string {
red := color.New(color.FgRed).SprintFunc()
white := color.New(color.FgWhite).SprintFunc()

Expand All @@ -162,10 +163,11 @@ func printEvent(formatter *colorjson.Formatter, event *cloudwatchlogs.FilteredLo
dateStr := date.Format(time.RFC3339)
streamStr := aws.StringValue(event.LogStreamName)
jl := map[string]interface{}{}

if err := json.Unmarshal(bytes, &jl); err != nil {
fmt.Printf("[%s] (%s) %s\n", red(dateStr), white(streamStr), str)
} else {
output, _ := formatter.Marshal(jl)
fmt.Printf("[%s] (%s) %s\n", red(dateStr), white(streamStr), output)
return fmt.Sprintf("[%s] (%s) %s\n", red(dateStr), white(streamStr), str)
}

output, _ := formatter.Marshal(jl)
return fmt.Sprintf("[%s] (%s) %s\n", red(dateStr), white(streamStr), output)
}
6 changes: 5 additions & 1 deletion cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
)

var getConfig config.Configuration
var getOutputConfig config.OutputConfiguration

var getCommand = &cobra.Command{
Use: "get <log group>",
Expand All @@ -24,7 +25,7 @@ var getCommand = &cobra.Command{
},
Run: func(cmd *cobra.Command, args []string) {
getConfig.Group = args[0]
b := blade.NewBlade(&getConfig, &awsConfig, nil)
b := blade.NewBlade(&getConfig, &awsConfig, &getOutputConfig)
if getConfig.Prefix != "" {
streams := b.GetLogStreams()
if len(streams) == 0 {
Expand Down Expand Up @@ -57,4 +58,7 @@ Takes an absolute timestamp in RFC3339 format, or a relative time (eg. -2h).
Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".`,
)
getCommand.Flags().StringVar(&getConfig.Filter, "filter", "", "event filter pattern")
getCommand.Flags().BoolVar(&getOutputConfig.Expand, "expand", false, "indent JSON log messages")
getCommand.Flags().BoolVar(&getOutputConfig.Invert, "invert", false, "invert colors for light terminal themes")
getCommand.Flags().BoolVar(&getOutputConfig.RawString, "rawString", false, "print JSON strings without escaping")
}
10 changes: 5 additions & 5 deletions cmd/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

var watchConfig config.Configuration
var outputConfig config.OutputConfiguration
var watchOutputConfig config.OutputConfiguration

var watchCommand = &cobra.Command{
Use: "watch <log group>",
Expand All @@ -25,7 +25,7 @@ var watchCommand = &cobra.Command{
},
Run: func(cmd *cobra.Command, args []string) {
watchConfig.Group = args[0]
b := blade.NewBlade(&watchConfig, &awsConfig, &outputConfig)
b := blade.NewBlade(&watchConfig, &awsConfig, &watchOutputConfig)
if watchConfig.Prefix != "" {
streams := b.GetLogStreams()
if len(streams) == 0 {
Expand All @@ -42,7 +42,7 @@ var watchCommand = &cobra.Command{
func init() {
watchCommand.Flags().StringVar(&watchConfig.Prefix, "prefix", "", "log stream prefix filter")
watchCommand.Flags().StringVar(&watchConfig.Filter, "filter", "", "event filter pattern")
watchCommand.Flags().BoolVar(&outputConfig.Expand, "expand", false, "indent JSON log messages")
watchCommand.Flags().BoolVar(&outputConfig.Invert, "invert", false, "invert colors for light terminal themes")
watchCommand.Flags().BoolVar(&outputConfig.RawString, "rawString", false, "print JSON strings without escaping")
watchCommand.Flags().BoolVar(&watchOutputConfig.Expand, "expand", false, "indent JSON log messages")
watchCommand.Flags().BoolVar(&watchOutputConfig.Invert, "invert", false, "invert colors for light terminal themes")
watchCommand.Flags().BoolVar(&watchOutputConfig.RawString, "rawString", false, "print JSON strings without escaping")
}

0 comments on commit 143b5f4

Please sign in to comment.