-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ivan Vlasic
committed
Oct 26, 2021
1 parent
247c21a
commit d0ec08b
Showing
5 changed files
with
125 additions
and
200 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
} |