Skip to content

Commit

Permalink
move logs and test to controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Vlasic committed Oct 26, 2021
1 parent 247c21a commit d0ec08b
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 200 deletions.
35 changes: 13 additions & 22 deletions cli/cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,35 +203,29 @@ Additionally, you can enable streaming of lambda execution logs by setting the -
}

func newLogsCommand() *cobra.Command {
var a logsArgs
var a controller.LogsArgs
cmd := &cobra.Command{
Use: "logs [function]",
Use: "logs <function>",
Short: "Fetch logs for a specific function/api",
Long: `Fetch logs for a specific function/api
Logs can be filtered using Cloudwatch filter patterns. For more information see:
https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/FilterAndPatternSyntax.html
If the --tail flag is set the process will keep running and polling for new logs every second.`,
Args: cobra.MaximumNArgs(1),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
a.function = args[0]
}
l, err := newLogs(a)
if err != nil {
return log.Wrap(err)
}
if err := l.run(); err != nil {
a.Function = args[0]
if err := controller.Logs(a); err != nil {
return log.Wrap(err)
}
return nil
},
}
cmd.Flags().StringVarP(&a.filter, "filter-pattern", "p", "", "filter pattern to use")
cmd.Flags().DurationVarP(&a.since, "since", "s", 3*time.Hour, "from what time to begin displaying logs, default is 3 hours ago")
cmd.Flags().BoolVarP(&a.tail, "tail", "t", false, "continuously poll for new logs")
cmd.Flags().StringVar(&a.stage, "stage", "", "name of the stage to fetch logs for")
cmd.Flags().StringVarP(&a.Filter, "filter-pattern", "p", "", "filter pattern to use")
cmd.Flags().DurationVarP(&a.Since, "since", "s", 3*time.Hour, "from what time to begin displaying logs, default is 3 hours ago")
cmd.Flags().BoolVarP(&a.Tail, "tail", "t", false, "continuously poll for new logs")
cmd.Flags().StringVar(&a.Stage, "stage", "", "name of the stage to fetch logs for")
return cmd
}

Expand Down Expand Up @@ -277,7 +271,7 @@ func templateList() string {
}

func newTestCommand() *cobra.Command {
var a testArgs
var a controller.TestArgs
cmd := &cobra.Command{
Use: "test",
Short: "Run project integration tests",
Expand All @@ -289,18 +283,15 @@ project api url and runs tests with 'go test -v'.
`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
t, err := newTest(a)
err := controller.Test(a)
if err != nil {
return log.Wrap(err)
}
if err := t.run(); err != nil {
return log.Wrap(err)
}
return nil
},
}
cmd.Flags().StringVarP(&a.runRegexp, "run", "r", "", "run only tests with this pattern in name")
cmd.Flags().StringVarP(&a.stage, "stage", "s", "", "stage name")
cmd.Flags().StringVarP(&a.RunRegexp, "run", "r", "", "run only tests with this pattern in name")
cmd.Flags().StringVarP(&a.Stage, "stage", "s", "", "stage name")
return cmd
}

Expand Down
126 changes: 0 additions & 126 deletions cli/cmd/logs.go

This file was deleted.

52 changes: 0 additions & 52 deletions cli/cmd/test.go

This file was deleted.

75 changes: 75 additions & 0 deletions cli/controller/logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package controller

import (
"fmt"
"time"

"github.com/mantil-io/mantil/aws"
"github.com/mantil-io/mantil/cli/log"
)

type LogsArgs struct {
Function string
Filter string
Stage string
Tail bool
Since time.Duration
}

type LogsCmd struct {
awsClient *aws.AWS
logGroup string
filter string
startTime time.Time
tail bool
}

func Logs(a LogsArgs) error {
fs, err := NewStoreWithStage(a.Stage)
if err != nil {
return log.Wrap(err)
}
stage := fs.Stage(a.Stage)
awsClient, err := AWSClient(stage.Account(), stage.Project(), stage)
if err != nil {
return log.Wrap(err)
}
fn := stage.FindFunction(a.Function)
if fn == nil {
return log.Wrapf("function %s not found", a.Function)
}
logGroup := aws.LambdaLogGroup(fn.LambdaName())
startTime := time.Now().Add(-a.Since)
return printLogs(awsClient, logGroup, a.Filter, startTime, a.Tail)
}

func printLogs(awsClient *aws.AWS, logGroup, filter string, startTime time.Time, tail bool) error {
startTs := startTime.UnixMilli()
var lastEventTs int64

fetchAndPrint := func(ts int64) error {
events, err := awsClient.FetchLogs(logGroup, filter, &ts)
if err != nil {
return log.Wrap(err)
}
for e := range events {
fmt.Printf("%v %v", time.UnixMilli(e.Timestamp), e.Message)
lastEventTs = e.Timestamp
}
return nil
}

if err := fetchAndPrint(startTs); err != nil {
return log.Wrap(err)
}
if tail {
return nil
}
for {
startTs = lastEventTs + 1
if err := fetchAndPrint(startTs); err != nil {
return log.Wrap(err)
}
time.Sleep(time.Second)
}
}
37 changes: 37 additions & 0 deletions cli/controller/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package controller

import (
"fmt"

"github.com/mantil-io/mantil/cli/log"
"github.com/mantil-io/mantil/cli/ui"
"github.com/mantil-io/mantil/shell"
"github.com/mantil-io/mantil/workspace"
)

type TestArgs struct {
RunRegexp string
Stage string
}

func Test(a TestArgs) error {
fs, err := NewStoreWithStage(a.Stage)
if err != nil {
return log.Wrap(err)
}
return runTests(fs.ProjectRoot(), fs.Stage(a.Stage).Endpoints.Rest, a.RunRegexp)
}

func runTests(projectPath, apiURL, runRegexp string) error {
args := []string{"go", "test", "-v"}
if runRegexp != "" {
args = append(args, "--run", runRegexp)
}
return shell.Exec(shell.ExecOptions{
Env: []string{fmt.Sprintf("%s=%s", workspace.EnvApiURL, apiURL)},
Args: args,
WorkDir: projectPath + "/test",
Logger: ui.Info,
ShowShellCmd: false,
})
}

0 comments on commit d0ec08b

Please sign in to comment.