Skip to content

Commit

Permalink
if run on Lambda, the arguments passed at startup are prepended at ea…
Browse files Browse the repository at this point in the history
…ch execution.
  • Loading branch information
mashiike committed Jan 27, 2022
1 parent c5b5a66 commit 354a579
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 35 deletions.
21 changes: 20 additions & 1 deletion cmd/flexentry/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"context"
"flag"
"fmt"
"log"
"os"
"strings"
Expand All @@ -17,6 +19,15 @@ var (
)

func main() {
showVersion := flag.Bool("v", false, "show version")
showHelp := flag.Bool("h", false, "show help")
flag.Usage = func() {
fmt.Fprintln(flag.CommandLine.Output(), "Usage of flexenty:")
fmt.Fprintln(flag.CommandLine.Output(), "flexentry [opts] [commands...]")
flag.PrintDefaults()
}
flag.Parse()

logLevel := "info"
if l := os.Getenv("FLEXENTRY_LOG_LEVEL"); l != "" {
logLevel = l
Expand All @@ -34,13 +45,21 @@ func main() {
}
log.SetOutput(filter)
log.Println("[debug] flexentry version:", Version)
if *showVersion {
fmt.Println("flexentry:", Version)
return
}
if *showHelp {
flag.Usage()
return
}
entrypoint := flexentry.Entrypoint{
Executer: flexentry.NewSSMWrapExecuter(
flexentry.NewShellExecuter(),
time.Minute,
),
}
if err := entrypoint.Run(context.Background()); err != nil {
if err := entrypoint.Run(context.Background(), flag.Args()...); err != nil {
log.Fatalln("[error] ", err)
}
}
74 changes: 40 additions & 34 deletions flexentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ type Entrypoint struct {
query *gojq.Query
}

func (e *Entrypoint) Run(ctx context.Context) error {
func (e *Entrypoint) Run(ctx context.Context, args ...string) error {
if e.isLambda() {
log.Println("[debug] start lambda handler")
lambda.Start(e.handleRequest)
lambda.Start(e.getHandler(args...))
return nil
}
return e.Execute(ctx, Pipe{
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
}, os.Args[1:]...)
}, args...)
}

func (e *Entrypoint) isLambda() bool {
Expand All @@ -51,39 +51,45 @@ func (e *Entrypoint) Execute(ctx context.Context, pipe Pipe, commands ...string)
return e.Executer.Execute(ctx, pipe, commands...)
}

func (e *Entrypoint) handleRequest(ctx context.Context, event Event) (interface{}, error) {
commands, err := e.DetectCommand(ctx, event)
if err != nil {
log.Println("[error] ", err)
return nil, err
}
pipe := Pipe{
Stderr: os.Stderr,
Stdout: os.Stdout,
}
var bufInput, bufOutput bytes.Buffer
if err := json.NewEncoder(&bufInput).Encode(event); err != nil {
log.Println("[warn] failed event encode", err)
} else {
pipe.Stdin = &bufInput
}
if os.Getenv("FLEXENTRY_FUNCTION_OUTPUT") == "enable" {
pipe.Stdout = io.MultiWriter(os.Stdout, &bufOutput)
}
err = e.Execute(ctx, pipe, commands...)
if err != nil {
log.Println("[error] ", err)
return nil, err
}
if bufOutput.Len() > 0 {
var functionOutput interface{}
if err := json.NewDecoder(&bufOutput).Decode(&functionOutput); err != nil {
log.Println("[warn] failed output encode", err)
return nil, nil
func (e *Entrypoint) getHandler(args ...string) func(ctx context.Context, event Event) (interface{}, error) {
return func(ctx context.Context, event Event) (interface{}, error) {
commands, err := e.DetectCommand(ctx, event)
if err != nil {
log.Println("[error] ", err)
return nil, err
}
pipe := Pipe{
Stderr: os.Stderr,
Stdout: os.Stdout,
}
var bufInput, bufOutput bytes.Buffer
if err := json.NewEncoder(&bufInput).Encode(event); err != nil {
log.Println("[warn] failed event encode", err)
} else {
pipe.Stdin = &bufInput
}
if os.Getenv("FLEXENTRY_FUNCTION_OUTPUT") == "enable" {
pipe.Stdout = io.MultiWriter(os.Stdout, &bufOutput)
}
executeCommand := make([]string, 0, len(args)+len(commands))
executeCommand = append(executeCommand, args...)
executeCommand = append(executeCommand, commands...)

err = e.Execute(ctx, pipe, executeCommand...)
if err != nil {
log.Println("[error] ", err)
return nil, err
}
if bufOutput.Len() > 0 {
var functionOutput interface{}
if err := json.NewDecoder(&bufOutput).Decode(&functionOutput); err != nil {
log.Println("[warn] failed output encode", err)
return nil, nil
}
return functionOutput, nil
}
return functionOutput, nil
return nil, nil
}
return nil, nil
}

func (e *Entrypoint) DetectCommand(ctx context.Context, event Event) ([]string, error) {
Expand Down

0 comments on commit 354a579

Please sign in to comment.