Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V1/archive #327

Merged
merged 4 commits into from
Oct 13, 2023
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
30 changes: 23 additions & 7 deletions archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,37 @@ import (
"github.com/fujiwara/lambroll/wildcard"
)

type ArchiveOption struct {
Src string `help:"function zip archive or src dir" default:"."`
Dest string `help:"destination file path" default:"archive.zip"`

ExcludeFileOption
}

// Archive archives zip
func (app *App) Archive(ctx context.Context, opt DeployOption) error {
excludes, err := expandExcludeFile(opt.ExcludeFile)
if err != nil {
return fmt.Errorf("failed to parse exclude file: %w", err)
func (app *App) Archive(ctx context.Context, opt *ArchiveOption) error {
if err := opt.Expand(); err != nil {
return err
}
opt.excludes = append(opt.excludes, excludes...)

zipfile, _, err := createZipArchive(opt.Src, opt.excludes)
if err != nil {
return err
}
defer zipfile.Close()
_, err = io.Copy(os.Stdout, zipfile)
var w io.WriteCloser
if opt.Dest == "-" {
log.Printf("[info] writing zip archive to stdout")
w = os.Stdout
} else {
log.Printf("[info] writing zip archive to %s", opt.Dest)
w, err = os.Create(opt.Dest)
if err != nil {
return fmt.Errorf("failed to create %s: %w", opt.Dest, err)
}
defer w.Close()
}
_, err = io.Copy(w, zipfile)
return err
}

Expand Down Expand Up @@ -132,7 +149,6 @@ func addToZip(z *zip.Writer, path, relpath string, info os.FileInfo) error {
func (app *App) uploadFunctionToS3(ctx context.Context, f *os.File, bucket, key string) (string, error) {
svc := s3.NewFromConfig(app.awsConfig)
log.Printf("[debug] PutObjcet to s3://%s/%s", bucket, key)
// TODO multipart upload
res, err := svc.PutObject(ctx, &s3.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
Expand Down
23 changes: 10 additions & 13 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@ type CLIOptions struct {
List *ListOption `cmd:"list" help:"list functions"`
Rollback *RollbackOption `cmd:"rollback" help:"rollback function"`
Invoke *InvokeOption `cmd:"invoke" help:"invoke function"`
Archive *DeployOption `cmd:"archive" help:"archive function"`
Archive *ArchiveOption `cmd:"archive" help:"archive function"`
Logs *LogsOption `cmd:"logs" help:"show logs of function"`
Diff *DiffOption `cmd:"diff" help:"show diff of function"`
Render *RenderOption `cmd:"render" help:"render function.json"`
Versions *VersionsOption `cmd:"versions" help:"show versions of function"`

Version struct{} `cmd:"version" help:"show version"`
Expand Down Expand Up @@ -109,25 +108,23 @@ func dispatchCLI(ctx context.Context, sub string, usage func(), opts *CLIOptions
}
switch sub {
case "init":
return app.Init(ctx, *opts.Init)
return app.Init(ctx, opts.Init)
case "list":
return app.List(ctx, *opts.List)
return app.List(ctx, opts.List)
case "deploy":
return app.Deploy(ctx, *opts.Deploy)
return app.Deploy(ctx, opts.Deploy)
case "invoke":
return app.Invoke(ctx, *opts.Invoke)
return app.Invoke(ctx, opts.Invoke)
case "logs":
return app.Logs(ctx, *opts.Logs)
return app.Logs(ctx, opts.Logs)
case "versions":
return app.Versions(ctx, *opts.Versions)
return app.Versions(ctx, opts.Versions)
case "archive":
return app.Archive(ctx, *opts.Archive)
return app.Archive(ctx, opts.Archive)
case "rollback":
return app.Rollback(ctx, *opts.Rollback)
case "render":
return app.Render(ctx, *opts.Render)
return app.Rollback(ctx, opts.Rollback)
case "diff":
return app.Diff(ctx, *opts.Diff)
return app.Diff(ctx, opts.Diff)
default:
usage()
}
Expand Down
4 changes: 2 additions & 2 deletions create.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func prepareZipfile(src string, excludes []string) (*os.File, os.FileInfo, error
return nil, nil, fmt.Errorf("src %s is not found", src)
}

func (app *App) prepareFunctionCodeForDeploy(ctx context.Context, opt DeployOption, fn *Function) error {
func (app *App) prepareFunctionCodeForDeploy(ctx context.Context, opt *DeployOption, fn *Function) error {
if fn.PackageType == types.PackageTypeImage {
if fn.Code == nil || fn.Code.ImageUri == nil {
return fmt.Errorf("PackageType=Image requires Code.ImageUri in function definition")
Expand Down Expand Up @@ -91,7 +91,7 @@ func (app *App) prepareFunctionCodeForDeploy(ctx context.Context, opt DeployOpti
return nil
}

func (app *App) create(ctx context.Context, opt DeployOption, fn *Function) error {
func (app *App) create(ctx context.Context, opt *DeployOption, fn *Function) error {
err := app.prepareFunctionCodeForDeploy(ctx, opt, fn)
if err != nil {
return fmt.Errorf("failed to prepare function code: %w", err)
Expand Down
11 changes: 4 additions & 7 deletions deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@ import (
// DeployOption represens an option for Deploy()
type DeployOption struct {
Src string `help:"function zip archive or src dir" default:"."`
ExcludeFile string `help:"exclude file" default:".lambdaignore"`
Publish bool `help:"publish function" default:"true"`
AliasName string `help:"alias name for publish" default:"current"`
AliasToLatest bool `help:"set alias to unpublished $LATEST version" default:"false"`
DryRun bool `help:"dry run" default:"false"`
SkipArchive bool `help:"skip to create zip archive. requires Code.S3Bucket and Code.S3Key in function definition" default:"false"`
KeepVersions int `help:"Number of latest versions to keep. Older versions will be deleted. (Optional value: default 0)." default:"0"`

excludes []string
ExcludeFileOption
}

func (opt DeployOption) label() string {
Expand Down Expand Up @@ -68,12 +67,10 @@ func (opt *DeployOption) String() string {
}

// Deploy deployes a new lambda function code
func (app *App) Deploy(ctx context.Context, opt DeployOption) error {
excludes, err := expandExcludeFile(opt.ExcludeFile)
if err != nil {
return fmt.Errorf("failed to parse exclude-file: %w", err)
func (app *App) Deploy(ctx context.Context, opt *DeployOption) error {
if err := opt.Expand(); err != nil {
return err
}
opt.excludes = append(opt.excludes, excludes...)
log.Printf("[debug] %s", opt.String())

fn, err := app.loadFunction(app.functionFilePath)
Expand Down
15 changes: 6 additions & 9 deletions diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,17 @@ import (

// DiffOption represents options for Diff()
type DiffOption struct {
Src string `help:"function zip archive or src dir" default:"."`
CodeSha256 bool `help:"diff of code sha256" default:"false"`
ExcludeFile string `help:"exclude file" default:".lambrollignore"`
Src string `help:"function zip archive or src dir" default:"."`
CodeSha256 bool `help:"diff of code sha256" default:"false"`

excludes []string
ExcludeFileOption
}

// Diff prints diff of function.json compared with latest function
func (app *App) Diff(ctx context.Context, opt DiffOption) error {
excludes, err := expandExcludeFile(opt.ExcludeFile)
if err != nil {
return fmt.Errorf("failed to parse exclude-file: %w", err)
func (app *App) Diff(ctx context.Context, opt *DiffOption) error {
if err := opt.Expand(); err != nil {
return err
}
opt.excludes = append(opt.excludes, excludes...)

newFunc, err := app.loadFunction(app.functionFilePath)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion init.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type InitOption struct {
}

// Init initializes function.json
func (app *App) Init(ctx context.Context, opt InitOption) error {
func (app *App) Init(ctx context.Context, opt *InitOption) error {
res, err := app.lambda.GetFunction(ctx, &lambda.GetFunctionInput{
FunctionName: opt.FunctionName,
})
Expand Down
2 changes: 1 addition & 1 deletion invoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type InvokeOption struct {
}

// Invoke invokes function
func (app *App) Invoke(ctx context.Context, opt InvokeOption) error {
func (app *App) Invoke(ctx context.Context, opt *InvokeOption) error {
fn, err := app.loadFunction(app.functionFilePath)
if err != nil {
return fmt.Errorf("failed to load function: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion list.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type ListOption struct {
}

// List lists lambda functions
func (app *App) List(ctx context.Context, opt ListOption) error {
func (app *App) List(ctx context.Context, opt *ListOption) error {
var marker *string
for {
res, err := app.lambda.ListFunctions(ctx, &lambda.ListFunctionsInput{
Expand Down
2 changes: 1 addition & 1 deletion logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type LogsOption struct {
FilterPattern *string `help:"The filter pattern to use"`
}

func (app *App) Logs(ctx context.Context, opt LogsOption) error {
func (app *App) Logs(ctx context.Context, opt *LogsOption) error {
fn, err := app.loadFunction(app.functionFilePath)
if err != nil {
return fmt.Errorf("failed to load function: %w", err)
Expand Down
17 changes: 17 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
package lambroll

import "fmt"

// Option represents common option.

type ExcludeFileOption struct {
ExcludeFile string `help:"exclude file" default:".lambdaignore"`

excludes []string
}

func (opt *ExcludeFileOption) Expand() error {
excludes, err := expandExcludeFile(opt.ExcludeFile)
if err != nil {
return fmt.Errorf("failed to parse exclude-file: %w", err)
}
opt.excludes = append(opt.excludes, excludes...)
return nil
}
2 changes: 1 addition & 1 deletion rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (opt RollbackOption) label() string {
}

// Rollback rollbacks function
func (app *App) Rollback(ctx context.Context, opt RollbackOption) error {
func (app *App) Rollback(ctx context.Context, opt *RollbackOption) error {
fn, err := app.loadFunction(app.functionFilePath)
if err != nil {
return fmt.Errorf("failed to load function: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/aws/aws-sdk-go-v2/service/lambda/types"
)

func (app *App) updateTags(ctx context.Context, fn *Function, opt DeployOption) error {
func (app *App) updateTags(ctx context.Context, fn *Function, opt *DeployOption) error {
if fn.Tags == nil {
log.Println("[debug] Tags not defined in function.json skip updating tags")
return nil
Expand Down
2 changes: 1 addition & 1 deletion versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (v versionsOutput) TSV() string {
}

// Versions manages the versions of a Lambda function
func (app *App) Versions(ctx context.Context, opt VersionsOption) error {
func (app *App) Versions(ctx context.Context, opt *VersionsOption) error {
newFunc, err := app.loadFunction(app.functionFilePath)
if err != nil {
return fmt.Errorf("failed to load function: %w", err)
Expand Down