Skip to content

Commit

Permalink
Add go report card recommendations for gofmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Xoán Mallón committed Apr 8, 2020
1 parent 661fc04 commit 6bc8704
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 97 deletions.
40 changes: 20 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,31 @@ Go to [release page](https://github.com/xoanmm/log-stream-filter/releases) and d
## Usage

NAME:
aws-log-filter - retrieves all event logs from all streamLogGroup of a specific logGroup of AWS

log-stream-filter - retrieves all event logs from all streamLogGroup of a specific logGroup of AWS
USAGE:
aws-log-filter [--log-group <log-group-name>] [--log-stream-filter <filter>] [--log-stream-filter-position <position>][--aws-profile <aws-profile>] [--aws-region <aws-region>] [--path <path>] [--start-date <date>] [--end-date <date>]

log-stream-filter [--log-group <log-group-name>] [--log-stream-filter <filter>] [--log-stream-filter-position <position>][--aws-profile <aws-profile>] [--aws-region <aws-region>] [--path <path>] [--start-date <date>] [--end-date <date>]
VERSION:
1.0.0

1.0.0
AUTHOR:
Xoan Mallon <[email protected]>

Xoan Mallon <[email protected]>
COMMANDS:
help, h Shows a list of commands or help for one command

help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--log-group value, -n value log group name on which all logStreams will be obtained and will apply the filtering (default: "my-lo-group")
--log-stream-filter value, -l value filter to apply on logStreams name to retrieve eventLogs or not (default: "service-name-1")
--log-stream-filter-position value, -f value position in which to apply the log-stream-filter in the logStreams of the logGroup by splitting by the character / (Example of logStreamGroup: log-group/log-stream-group-prefix/ccc7b271-83ee-4487-b8f0-4246ce2d90ad) (default: 1)
--aws-profile value, -a value aws-profile to use for credentials (default: "my-profile")
--aws-region value, -r value aws region to use for call operations to aws sdk (default: "us-east-1")
--path value, -p value path where to store the logs (default: "/tmp/")
--start-date value, -s value filter only from a date specified (UTC format) (default: "03/17/2020 05:33:54")
--end-date value, -e value filter only until a date specified (UTC format) (default: "03/17/2020 13:33:54")
--help, -h show help (default: false)
--version, -v print the version (default: false)
--log-group value, -n value log group name on which all logStreams will be obtained and will apply the filtering (default: "my-lo-group")
--log-stream-filter value, -l value filter to apply on logStreams name to retrieve eventLogs or not (default: "service-name-1")
--log-stream-filter-position value, -f value position in which to apply the log-stream-filter in the logStreams of the logGroup by splitting by the character / (Example of logStreamGroup: log-group/log-stream-group-prefix/ccc7b271-83ee-4487-b8f0-4246ce2d90ad) (default: 1)
--aws-profile value, -a value aws-profile to use for credentials (default: "my-profile")
--aws-region value, -r value aws region to use for call operations to aws sdk (default: "us-east-1")
--path value, -p value path where to store the logs (default: "/tmp/")
--start-date value, -s value filter only from a date specified ('mm/dd/yyyy hh:mm:ss' format UTC time) (default: $ACTUAL_DATE - 8hours)
--end-date value, -e value filter only until a date specified ('mm/dd/yyyy hh:mm:ss' format UTC time) (default: $ACTUAL_DATE)
--help, -h show help (default: false)
--version, -v print the version (default: false)

### Dependencies & Refs

Expand Down
10 changes: 5 additions & 5 deletions cmd/doc.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*
NAME:
aws-log-filter - retrieves all event logs from all streamLogGroup of a specific logGroup of AWS
log-stream-filter - retrieves all event logs from all streamLogGroup of a specific logGroup of AWS
USAGE:
aws-log-filter [--log-group <log-group-name>] [--log-stream-filter <filter>] [--log-stream-filter-position <position>][--aws-profile <aws-profile>] [--aws-region <aws-region>] [--path <path>] [--start-date <date>] [--end-date <date>]
log-stream-filter [--log-group <log-group-name>] [--log-stream-filter <filter>] [--log-stream-filter-position <position>][--aws-profile <aws-profile>] [--aws-region <aws-region>] [--path <path>] [--start-date <date>] [--end-date <date>]
VERSION:
1.0.0
Expand All @@ -21,10 +21,10 @@ GLOBAL OPTIONS:
--aws-profile value, -a value aws-profile to use for credentials (default: "my-profile")
--aws-region value, -r value aws region to use for call operations to aws sdk (default: "us-east-1")
--path value, -p value path where to store the logs (default: "/tmp/")
--start-date value, -s value filter only from a date specified (UTC format) (default: "03/17/2020 01:59:27")
--end-date value, -e value filter only until a date specified (UTC format) (default: "03/17/2020 09:59:27")
--start-date value, -s value filter only from a date specified ('mm/dd/yyyy hh:mm:ss' format UTC time) (default: $ACTUAL_DATE - 8hours)
--end-date value, -e value filter only until a date specified ('mm/dd/yyyy hh:mm:ss' format UTC time) (default: $ACTUAL_DATE)
--help, -h show help (default: false)
--version, -v print the version (default: false)
*/
*/
package main
97 changes: 50 additions & 47 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// Package main contains code tu run dir-cleaner as a CLI command.
// Package main contains code tu run logs-stream-filter as a CLI command.
package main

import (
"fmt"
"github.com/xoanmm/log-stream-filter/pkg/filter"
"log"
"os"
"path/filepath"
"time"

"github.com/xoanmm/log-stream-filter/pkg/filter"

"github.com/urfave/cli/v2"
)

Expand All @@ -32,11 +33,11 @@ func main() {
func buildCLI(app *filter.App) *cli.App {
d, _ := time.Parse(time.RFC3339, date)
return &cli.App{
Name: "aws-log-filter",
Usage: "retrieves all event logs from all streamLogGroup of a specific logGroup of AWS",
Version: version,
Compiled: d,
UsageText: "aws-log-filter [--log-group <log-group-name>] [--log-stream-filter <filter>] " +
Name: "log-stream-filter",
Usage: "retrieves all event logs from all streamLogGroup of a specific logGroup of AWS",
Version: version,
Compiled: d,
UsageText: "log-stream-filter [--log-group <log-group-name>] [--log-stream-filter <filter>] " +
"[--log-stream-filter-position <position>]" +
"[--aws-profile <aws-profile>] [--aws-region <aws-region>] " +
"[--path <path>] [--start-date <date>] [--end-date <date>]",
Expand All @@ -48,75 +49,77 @@ func buildCLI(app *filter.App) *cli.App {
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "log-group",
Usage: "log group name on which all logStreams will be obtained and will apply the filtering",
Value: "my-lo-group",
Aliases: []string{"n"},
Name: "log-group",
Usage: "log group name on which all logStreams will be obtained and will apply the filtering",
Value: "my-lo-group",
Aliases: []string{"n"},
},

&cli.StringFlag{
Name: "log-stream-filter",
Usage: "filter to apply on logStreams name to retrieve eventLogs or not",
Value: "service-name-1",
Aliases: []string{"l"},
Name: "log-stream-filter",
Usage: "filter to apply on logStreams name to retrieve eventLogs or not",
Value: "service-name-1",
Aliases: []string{"l"},
},

&cli.IntFlag{
Name: "log-stream-filter-position",
Usage: "position in which to apply the log-stream-filter in the logStreams of the logGroup by splitting by the character / (Example of logStreamGroup: " +
Name: "log-stream-filter-position",
Usage: "position in which to apply the log-stream-filter in the logStreams of the logGroup by splitting by the character / (Example of logStreamGroup: " +
"log-group/log-stream-group-prefix/ccc7b271-83ee-4487-b8f0-4246ce2d90ad)",
Value: 1,
Aliases: []string{"f"},
Value: 1,
Aliases: []string{"f"},
},

&cli.StringFlag{
Name: "aws-profile",
Usage: "aws-profile to use for credentials",
Value: "my-profile",
Aliases: []string{"a"},
Name: "aws-profile",
Usage: "aws-profile to use for credentials",
Value: "my-profile",
Aliases: []string{"a"},
},

&cli.StringFlag{
Name: "aws-region",
Usage: "aws region to use for call operations to aws sdk",
Value: "us-east-1",
Aliases: []string{"r"},
Name: "aws-region",
Usage: "aws region to use for call operations to aws sdk",
Value: "us-east-1",
Aliases: []string{"r"},
},

&cli.StringFlag{
Name: "path",
Usage: "path where to store the logs",
Value: "/tmp/",
Aliases: []string{"p"},
Name: "path",
Usage: "path where to store the logs",
Value: "/tmp/",
Aliases: []string{"p"},
},

&cli.StringFlag{
Name: "start-date",
Usage: "filter only from a date specified (UTC format)",
Value: nowDateLessEightHours,
Aliases: []string{"s"},
Name: "start-date",
Usage: "filter only from a date specified ('mm/dd/yyyy hh:mm:ss' format UTC time)",
DefaultText: "$ACTUAL_DATE - 8hours",
Value: nowDateLessEightHours,
Aliases: []string{"s"},
},

&cli.StringFlag{
Name: "end-date",
Usage: "filter only until a date specified (UTC format)",
Value: nowDate,
Aliases: []string{"e"},
Name: "end-date",
Usage: "filter only until a date specified ('mm/dd/yyyy hh:mm:ss' format UTC time)",
DefaultText: "$ACTUAL_DATE",
Value: nowDate,
Aliases: []string{"e"},
},
},
Action: func(c *cli.Context) error {
path, _ := filepath.Abs(c.String("path"))
logGroup := c.String("log-group")

logsFileGenerated := app.FilterLogs(&filter.Options{
LogGroup: logGroup,
AwsProfile: c.String("aws-profile"),
AwsRegion: c.String("aws-region"),
LogStreamFilter: c.String("log-stream-filter"),
LogGroup: logGroup,
AwsProfile: c.String("aws-profile"),
AwsRegion: c.String("aws-region"),
LogStreamFilter: c.String("log-stream-filter"),
LogStreamFilterPosition: c.Int("log-stream-filter-position"),
Path: path,
StartDate: c.String("start-date"),
EndDate: c.String("end-date"),
Path: path,
StartDate: c.String("start-date"),
EndDate: c.String("end-date"),
})
fmt.Println(len(logsFileGenerated), "files generated for logs of logStreams filtered for logGroup", logGroup)
for k := range logsFileGenerated {
Expand All @@ -125,4 +128,4 @@ func buildCLI(app *filter.App) *cli.App {
return nil
},
}
}
}
18 changes: 7 additions & 11 deletions pkg/filter/app.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Package cleaner contains the app with methods to scan and retrieve logs from logGroup.
// Package filter contains the app with methods to scan and retrieve logs from logGroup.
package filter

import (
Expand All @@ -12,11 +12,11 @@ import (
// App contains the streamGroup to scan and the streamLogs to get from it
type App struct{}

// Filter logs creates a sessions of in aws and get all the logStreams for a specific logGroup
// FilterLogs creates a sessions of in aws and get all the logStreams for a specific logGroup
func (a *App) FilterLogs(options *Options) map[string][]string {
checkArgsConditions(options.StartDate, options.EndDate, options.LogStreamFilterPosition)
fmt.Printf("Filtering logs for logGroup %s\n params: " +
"[aws-profile %s] [log-stream-filter: %s] [path: %s] " +
fmt.Printf("Filtering logs for logGroup %s\n params: "+
"[aws-profile %s] [log-stream-filter: %s] [path: %s] "+
"[start-date: %s] [end-date: %s]\n",
options.LogGroup, options.AwsProfile, options.LogStreamFilter, options.Path, options.StartDate, options.EndDate)
sess, _ := createAwsSession(options.AwsProfile, options.AwsRegion)
Expand Down Expand Up @@ -90,10 +90,6 @@ func saveLogsToFile(filenameW string, logStreamName string, resp *cloudwatchlogs
if *event.IngestionTime <= timestampFromEndDate {
log.Println(*event.Message)
} else {
//fmt.Println("No more log messages will be added to logStream", logStreamName, "file", filenameW,
// "because the date of ingestion of the messages until this one",
// getTimeInUTCFromMilliseconds(*event.IngestionTime),
// "is greater than that marked by the value of the end-date parameter")
cont = false
break
}
Expand All @@ -103,6 +99,7 @@ func saveLogsToFile(filenameW string, logStreamName string, resp *cloudwatchlogs
return cont
}

// CheckErr checks if given error is not nil and exit program with signal 1
func CheckErr(e error, errString string) {
if e != nil {
fmt.Print(errString)
Expand All @@ -125,7 +122,6 @@ func checkArgsConditions(startDate string, endDate string, logStreamPosition int
func inBetween(i, min, max int) bool {
if (i >= min) && (i <= max) {
return true
} else {
return false
}
}
return false
}
4 changes: 2 additions & 2 deletions pkg/filter/aws_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
// Create session in aws using specific aws-profile and aws-region specified as arguments
func createAwsSession(awsProfile string, awsRegion string) (*session.Session, error) {
sess, err := session.NewSessionWithOptions(session.Options{
Config: aws.Config{Region: aws.String(awsRegion)},
Config: aws.Config{Region: aws.String(awsRegion)},
Profile: awsProfile,
})
CheckErr(err, "Could not create session!")
return sess, err
}
}
14 changes: 6 additions & 8 deletions pkg/filter/aws_logstreams.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func getAllLogStreamsOfLogGroup(svc *cloudwatchlogs.CloudWatchLogs, logGroupName string, logStreamFilter string, logStreamFilterPosition int) []*logStreamGroups {
fmt.Println("Getting logStreams for logGroup", logGroupName, "applying filter", logStreamFilter)
resp, err := svc.DescribeLogStreams(&cloudwatchlogs.DescribeLogStreamsInput{
LogGroupName: aws.String(logGroupName),
LogGroupName: aws.String(logGroupName),
})

if err != nil {
Expand All @@ -20,12 +20,12 @@ func getAllLogStreamsOfLogGroup(svc *cloudwatchlogs.CloudWatchLogs, logGroupName
os.Exit(1)
}

var s[]*logStreamGroups
var s []*logStreamGroups
s, c, cont := getLogStreamsOfLogGroup(resp, s, logStreamFilter, logStreamFilterPosition)
for cont == true {
resp, _ := svc.DescribeLogStreams(&cloudwatchlogs.DescribeLogStreamsInput{
LogGroupName: aws.String(logGroupName),
NextToken: c,
LogGroupName: aws.String(logGroupName),
NextToken: c,
})
s, c, cont = getLogStreamsOfLogGroup(resp, s, logStreamFilter, logStreamFilterPosition)
}
Expand All @@ -51,8 +51,7 @@ func getLogEventsForLogStreamCallWithTime(logGroupName string, logStreamName str
LogGroupName: aws.String(logGroupName),
LogStreamName: aws.String(logStreamName),
StartFromHead: aws.Bool(true),
StartTime: aws.Int64(startTime),

StartTime: aws.Int64(startTime),
})
if err != nil {
fmt.Println("Got error getting log events:")
Expand All @@ -66,9 +65,8 @@ func getLogEventsForLogStreamCallWithNextToken(logGroupName string, logStreamNam
resp, err := svc.GetLogEvents(&cloudwatchlogs.GetLogEventsInput{
LogGroupName: aws.String(logGroupName),
LogStreamName: aws.String(logStreamName),
NextToken: aws.String(nextToken),
NextToken: aws.String(nextToken),
StartFromHead: aws.Bool(true),

})
if err != nil {
fmt.Println("Got error getting log events:")
Expand Down
4 changes: 2 additions & 2 deletions pkg/filter/time_filter_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ func getTimeStampUnixFromDate(startDate string) int64 {
}

func getTimeInUTCFromMilliseconds(timestamp int64) string {
return time.Unix(0, timestamp * int64(1000000)).UTC().Format(shortDateFormat)
}
return time.Unix(0, timestamp*int64(1000000)).UTC().Format(shortDateFormat)
}
4 changes: 2 additions & 2 deletions pkg/filter/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Options struct {

//LogStreamGrups contains information about each LogStreamGroup
type logStreamGroups struct {
CreationTime int64
CreationTime int64
LastEventTime int64
LogStreamName string
LogStreamName string
}

0 comments on commit 6bc8704

Please sign in to comment.