Skip to content

Commit

Permalink
Merge branch 'pre/v1' into v1/archive
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiwara authored Oct 13, 2023
2 parents 3c0891c + bc537fd commit d8fbce0
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 51 deletions.
70 changes: 34 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,59 +116,57 @@ $ lambroll deploy
## Usage

```console
usage: lambroll [<flags>] <command> [<args> ...]
Usage: lambroll <command>
Flags:
--help Show context-sensitive help (also try --help-long and --help-man).
--log-level=info log level (trace, debug, info, warn, error)
--function="function.json" Function file path
--color enable colored output
--profile="" AWS credential profile name
--region="" AWS region
--tfstate="" URL to terraform.tfstate
--prefixed-tfstate=PREFIX=URL ...
key value pair of the prefix for template function name and URL to terraform.tfstate
--endpoint="" AWS API Lambda Endpoint
--envfile=ENVFILE ... environment files
--ext-str=EXT-STR ... external string values for Jsonnet
--ext-code=EXT-CODE ... external code values for Jsonnet
-h, --help Show context-sensitive help.
--function=STRING Function file path
--log-level="info" log level (trace, debug, info, warn, error)
--color enable colored output
--region=REGION AWS region
--profile=PROFILE AWS credential profile name
--tfstate=TFSTATE URL to terraform.tfstate
--prefixed-tfstate=KEY=VALUE;...
key value pair of the prefix for template function name
and URL to terraform.tfstate
--endpoint=ENDPOINT AWS API Lambda Endpoint
--envfile=ENVFILE,... environment files
--ext-str=KEY=VALUE;... external string values for Jsonnet
--ext-code=KEY=VALUE;... external code values for Jsonnet
Commands:
help [<command>...]
Show help.
version
show version
deploy
deploy or create function
init --function-name=FUNCTION-NAME [<flags>]
init --function-name=
init function.json
list
list functions
deploy [<flags>]
deploy or create function
rollback [<flags>]
rollback
rollback function
delete [<flags>]
delete function
invoke [<flags>]
invoke
invoke function
archive [<flags>]
archive zip
archive
archive function
logs
show logs of function
diff
show diff of function
logs [<flags>]
tail logs using `aws logs tail` (aws-cli v2 required)
render
render function.json
diff [<flags>]
show display diff of function.json compared with latest function
versions
show versions of function
versions [<flags>]
manage function versions
version
show version
```

### Init
Expand Down
3 changes: 2 additions & 1 deletion cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type CLIOptions struct {
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 @@ -122,7 +123,7 @@ func dispatchCLI(ctx context.Context, sub string, usage func(), opts *CLIOptions
case "archive":
return app.Archive(ctx, opts.Archive)
case "rollback":
return app.Rollback(ctx, opts.Rollback)
return app.Rollback(ctx, *opts.Rollback)

Check failure on line 126 in cli.go

View workflow job for this annotation

GitHub Actions / Build (1.20)

cannot use *opts.Rollback (variable of type RollbackOption) as *RollbackOption value in argument to app.Rollback

Check failure on line 126 in cli.go

View workflow job for this annotation

GitHub Actions / Build (1.21)

cannot use *opts.Rollback (variable of type RollbackOption) as *RollbackOption value in argument to app.Rollback

Check failure on line 126 in cli.go

View workflow job for this annotation

GitHub Actions / Build (1.21)

cannot use *opts.Rollback (variable of type RollbackOption) as *RollbackOption value in argument to app.Rollback
case "diff":
return app.Diff(ctx, opts.Diff)
default:
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/fujiwara/tfstate-lookup v1.1.2
github.com/go-test/deep v1.0.7
github.com/google/go-cmp v0.5.9
github.com/google/go-jsonnet v0.17.0
github.com/google/go-jsonnet v0.20.0
github.com/hashicorp/go-envparse v0.0.0-20200406174449-d9cfd743a15e
github.com/kayac/go-config v0.6.0
github.com/kylelemons/godebug v1.1.0
Expand Down Expand Up @@ -97,4 +97,5 @@ require (
google.golang.org/protobuf v1.26.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
sigs.k8s.io/yaml v1.1.0 // indirect
)
15 changes: 4 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5y
github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
github.com/envoyproxy/go-control-plane v0.9.9-0.20210217033140-668b12f5399d/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/fujiwara/logutils v1.1.0 h1:JAYmqW40d/ZjzouB01sfZiaTxwNe4hwmB6lLajZqm1s=
Expand Down Expand Up @@ -224,8 +223,8 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-jsonnet v0.17.0 h1:/9NIEfhK1NQRKl3sP2536b2+x5HnZMdql7x3yK/l8JY=
github.com/google/go-jsonnet v0.17.0/go.mod h1:sOcuej3UW1vpPTZOr8L7RQimqai1a57bt5j22LzGZCw=
github.com/google/go-jsonnet v0.20.0 h1:WG4TTSARuV7bSm4PMB4ohjxe33IHT5WVTrJSU33uT4g=
github.com/google/go-jsonnet v0.20.0/go.mod h1:VbgWF9JX7ztlv770x/TolZNGGFfiHEVx9G6ca2eUmeA=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/google/martian v2.1.0+incompatible h1:/CP5g8u/VJHijgedC/Legn3BAbAaWPgecwXBIDzw5no=
Expand Down Expand Up @@ -294,11 +293,8 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
Expand All @@ -321,7 +317,6 @@ github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/shogo82148/go-retry v1.1.0 h1:eNBmDFO0ekhF7GEx8Ie5fUmoqH/O1pSIcdivdBPLMcw=
github.com/shogo82148/go-retry v1.1.0/go.mod h1:NCS+03WD76BaTewJNwgyj3ukqcwNTodfy1SWHmbLZaU=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down Expand Up @@ -450,7 +445,6 @@ golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -459,7 +453,6 @@ golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191001151750-bb3f8db39f24/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200113162924-86b910548bc1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down Expand Up @@ -674,12 +667,10 @@ google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/l
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
Expand All @@ -696,3 +687,5 @@ honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs=
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
17 changes: 15 additions & 2 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
type InitOption struct {
FunctionName *string `help:"Function name for init" required:"true" default:""`
DownloadZip bool `help:"Download function.zip" default:"false"`
Jsonnet bool `default:"false" help:"render function.json as jsonnet"`
}

// Init initializes function.json
Expand Down Expand Up @@ -87,9 +88,21 @@ func (app *App) Init(ctx context.Context, opt *InitOption) error {
return err
}

log.Printf("[info] creating %s", DefaultFunctionFilenames[0])
var name string
if opt.Jsonnet {
name = DefaultFunctionFilenames[1]
} else {
name = DefaultFunctionFilenames[0]
}
log.Printf("[info] creating %s", name)
b, _ := marshalJSON(fn)
return app.saveFile(DefaultFunctionFilenames[0], b, os.FileMode(0644))
if opt.Jsonnet {
b, err = jsonToJsonnet(b, name)
if err != nil {
return err
}
}
return app.saveFile(name, b, os.FileMode(0644))
}

func download(url, path string) error {
Expand Down
33 changes: 33 additions & 0 deletions render.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package lambroll

import (
"context"
"fmt"
"os"
)

type RenderOption struct {
Jsonnet bool `default:"false" help:"render function.json as jsonnet"`
}

// Invoke invokes function
func (app *App) Render(ctx context.Context, opt RenderOption) error {
fn, err := app.loadFunction(app.functionFilePath)
if err != nil {
return fmt.Errorf("failed to load function: %w", err)
}
b, err := marshalJSON(fn)
if err != nil {
return fmt.Errorf("failed to marshal function: %w", err)
}
if opt.Jsonnet {
b, err = jsonToJsonnet(b, app.functionFilePath)
if err != nil {
return fmt.Errorf("failed to render function.json as jsonnet: %w", err)
}
}
if _, err := os.Stdout.Write(b); err != nil {
return fmt.Errorf("failed to write function.json: %w", err)
}
return nil
}
9 changes: 9 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"

"github.com/Songmu/prompter"
"github.com/google/go-jsonnet/formatter"
)

func (app *App) saveFile(path string, b []byte, mode os.FileMode) error {
Expand Down Expand Up @@ -68,3 +69,11 @@ func FindFunctionFile(preffered string) (string, error) {
}
return "", fmt.Errorf("function file (%s) not found", strings.Join(DefaultFunctionFilenames, " or "))
}

func jsonToJsonnet(src []byte, filepath string) ([]byte, error) {
s, err := formatter.Format(filepath, string(src), formatter.DefaultOptions())
if err != nil {
return nil, fmt.Errorf("failed to format jsonnet: %w", err)
}
return []byte(s), nil
}

0 comments on commit d8fbce0

Please sign in to comment.