Skip to content

Commit

Permalink
Merge branch 'main' into aws-sdk-go-v2
Browse files Browse the repository at this point in the history
backports Add runtime to output of versions command. #309
  • Loading branch information
fujiwara committed Aug 31, 2023
2 parents d17e1dd + 38b3fc1 commit ae37451
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 5 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:

steps:
- name: Set up Go
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go }}
id: go
Expand All @@ -37,6 +37,8 @@ jobs:
echo $PATH
make clean
make test
env:
TZ: Asia/Tokyo

- name: create test
run: |
Expand Down
3 changes: 3 additions & 0 deletions export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ var (
LoadZipArchive = loadZipArchive
MergeTags = mergeTags
)

type VersionsOutput = versionsOutput
type VersionsOutputs = versionsOutputs
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/fujiwara/logutils v1.1.0
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/hashicorp/go-envparse v0.0.0-20200406174449-d9cfd743a15e
github.com/kayac/go-config v0.6.0
Expand Down
16 changes: 12 additions & 4 deletions versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type versionsOutput struct {
Version string `json:"Version"`
Aliases []string `json:"Aliases,omitempty"`
LastModified time.Time `json:"LastModified"`
Runtime string `json:"Runtime"`
}

type versionsOutputs []versionsOutput
Expand All @@ -49,20 +50,26 @@ func (vo versionsOutputs) TSV() string {
func (vo versionsOutputs) Table() string {
buf := new(strings.Builder)
w := tablewriter.NewWriter(buf)
w.SetHeader([]string{"Version", "Last Modified", "Aliases"})
w.SetHeader([]string{"Version", "Last Modified", "Aliases", "Runtime"})
for _, v := range vo {
w.Append([]string{v.Version, v.LastModified.Local().Format(time.RFC3339), strings.Join(v.Aliases, ",")})
w.Append([]string{
v.Version,
v.LastModified.Local().Format(time.RFC3339),
strings.Join(v.Aliases, ","),
v.Runtime,
})
}
w.Render()
return buf.String()
}

func (v versionsOutput) TSV() string {
return fmt.Sprintf("%s\t%s\t%s\n",
return strings.Join([]string{
v.Version,
v.LastModified.Local().Format(time.RFC3339),
strings.Join(v.Aliases, ","),
)
v.Runtime,
}, "\t") + "\n"
}

// Versions manages the versions of a Lambda function
Expand Down Expand Up @@ -130,6 +137,7 @@ func (app *App) Versions(opt VersionsOption) error {
Version: *v.Version,
Aliases: aliases[*v.Version],
LastModified: lm,
Runtime: string(v.Runtime),
})
}

Expand Down
61 changes: 61 additions & 0 deletions versions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package lambroll_test

import (
"encoding/json"
"testing"
"time"

"github.com/fujiwara/lambroll"
"github.com/google/go-cmp/cmp"
)

// TestFixedTime is a fixed time for testing (JST)
var TestFixedTime = time.Date(2023, 8, 30, 12, 34, 56, 0, time.FixedZone("Asia/Tokyo", 9*60*60))

var TestVersionsOutputs = lambroll.VersionsOutputs{
{Version: "1", LastModified: TestFixedTime, Runtime: "go1.x"},
{Version: "2", LastModified: TestFixedTime, Runtime: "python3.8", Aliases: []string{"current", "latest"}},
}

func TestVersionsJSON(t *testing.T) {
jsonOutput := TestVersionsOutputs.JSON()

var parsedOutputs lambroll.VersionsOutputs
err := json.Unmarshal([]byte(jsonOutput), &parsedOutputs)

if err != nil {
t.Errorf("Failed to unmarshal JSON: %s", err)
}

if d := cmp.Diff(parsedOutputs, TestVersionsOutputs); d != "" {
t.Errorf("JSON mismatch: diff:%s", d)
}
}

func TestVersionsTSV(t *testing.T) {
t.Setenv("TZ", "UTC+9")
expectedTSV := "1\t2023-08-30T12:34:56+09:00\t\tgo1.x\n" +
"2\t2023-08-30T12:34:56+09:00\tcurrent,latest\tpython3.8\n"

if d := cmp.Diff(TestVersionsOutputs.TSV(), expectedTSV); d != "" {
t.Errorf("TSV mismatch: diff:%s", d)
}
}

func TestVersionsTable(t *testing.T) {
t.Setenv("TZ", "UTC+9")
tableOutput := TestVersionsOutputs.Table()
expectedOutput := `
+---------+---------------------------+----------------+-----------+
| VERSION | LAST MODIFIED | ALIASES | RUNTIME |
+---------+---------------------------+----------------+-----------+
| 1 | 2023-08-30T12:34:56+09:00 | | go1.x |
| 2 | 2023-08-30T12:34:56+09:00 | current,latest | python3.8 |
+---------+---------------------------+----------------+-----------+
`
expectedOutput = expectedOutput[1:] // remove first newline

if d := cmp.Diff(tableOutput, expectedOutput); d != "" {
t.Errorf("Table mismatch: diff:%s", d)
}
}

0 comments on commit ae37451

Please sign in to comment.