Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: CI

on:
pull_request:
branches:
- main
push:
branches:
- main

jobs:
test:
name: Test
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v6
with:
go-version-file: "go.mod"

- name: Run tests
run: go test -v -race -coverprofile=coverage.out ./...

- name: Upload coverage
uses: codecov/codecov-action@v4
with:
file: ./coverage.out
fail_ci_if_error: false

lint:
name: Lint
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v6
with:
go-version-file: "go.mod"

- name: Run golangci-lint
uses: golangci/golangci-lint-action@v9
with:
version: v2.6
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# cobra-lambda

[![CI](https://github.com/JayJamieson/cobra-lambda/actions/workflows/ci.yml/badge.svg)](https://github.com/JayJamieson/cobra-lambda/actions/workflows/ci.yml)

Run your [Cobra](https://github.com/spf13/cobra) CLI applications in AWS Lambda and invoke them remotely as if they were running locally on your machine.

## What is cobra-lambda?
Expand Down
1 change: 0 additions & 1 deletion flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ func parseFuncName(args []string) (string, bool, error) {
if s[1] == '-' {
numMinuses++
if len(s) == 2 { // "--" terminates the flags
args = args[1:]
return "", false, nil
}
}
Expand Down
7 changes: 5 additions & 2 deletions wrapper/lambda_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,17 @@ func TestNewCobrLambdaHandler_EmptyArgs(t *testing.T) {
}

func TestNewCobrLambdaHandler_ContextPropagation(t *testing.T) {
type contextKey string
const testKey contextKey = "test-key"

// Create a command that checks context
receivedCtx := false
cmd := &cobra.Command{
Use: "test",
Run: func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
if ctx != nil {
if val := ctx.Value("test-key"); val == "test-value" {
if val := ctx.Value(testKey); val == "test-value" {
receivedCtx = true
cmd.Println("Context received correctly")
}
Expand All @@ -247,7 +250,7 @@ func TestNewCobrLambdaHandler_ContextPropagation(t *testing.T) {
}

// Create context with value
ctx := context.WithValue(context.Background(), "test-key", "test-value")
ctx := context.WithValue(context.Background(), testKey, "test-value")
result, err := handler(ctx, eventJSON)

if err != nil {
Expand Down
16 changes: 8 additions & 8 deletions wrapper/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func (w *CobraLambda) Execute(args []string) (*OutputCapture, error) {

stderrReader, stderrWriter, err := os.Pipe()
if err != nil {
stdoutWriter.Close()
stdoutReader.Close()
_ = stdoutWriter.Close()
_ = stdoutReader.Close()
return nil, err
}

Expand All @@ -69,15 +69,15 @@ func (w *CobraLambda) Execute(args []string) (*OutputCapture, error) {
go func() {
defer wg.Done()
mw := io.MultiWriter(sharedBuffer, w.originalStdout)
io.Copy(mw, stdoutReader)
_, _ = io.Copy(mw, stdoutReader)
done <- true
}()

wg.Add(1)
go func() {
defer wg.Done()
mw := io.MultiWriter(sharedBuffer, w.originalStderr)
io.Copy(mw, stderrReader)
_, _ = io.Copy(mw, stderrReader)
done <- true
}()

Expand All @@ -88,14 +88,14 @@ func (w *CobraLambda) Execute(args []string) (*OutputCapture, error) {

execErr := w.cmd.Execute()

stdoutWriter.Close()
stderrWriter.Close()
_ = stdoutWriter.Close()
_ = stderrWriter.Close()

wg.Wait()
close(done)

stdoutReader.Close()
stderrReader.Close()
_ = stdoutReader.Close()
_ = stderrReader.Close()

os.Stdout = w.originalStdout
os.Stderr = w.originalStderr
Expand Down
2 changes: 1 addition & 1 deletion wrapper/wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestCobraWrapper_Execute(t *testing.T) {

// Write to os.Stdout
fmt.Println("Hello from os.Stdout!")
fmt.Fprintf(os.Stdout, "Additional stdout output\n")
_, _ = fmt.Fprintf(os.Stdout, "Additional stdout output\n")

// Write to os.Stderr
fmt.Fprintln(os.Stderr, "Error message to stderr")
Expand Down