-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
status.go
96 lines (89 loc) · 2.8 KB
/
status.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package lambroll
import (
"context"
"errors"
"fmt"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/lambda"
"github.com/aws/aws-sdk-go-v2/service/lambda/types"
"github.com/olekukonko/tablewriter"
)
// StatusOption represents options for Status()
type StatusOption struct {
Qualifier *string `help:"compare with"`
Output string `help:"output format" default:"table" enum:"table,json"`
}
type StatusOutput struct {
FunctionName string `json:"FunctionName"`
FunctionArn string `json:"FunctionArn"`
Version string `json:"Version"`
Runtime string `json:"Runtime,omitempty"`
PackageType string `json:"PackageType"`
State string `json:"State"`
LastUpdateState string `json:"LastUpdateState"`
FunctionURL string `json:"FunctionURL,omitempty"`
}
func (o *StatusOutput) String() string {
buf := new(strings.Builder)
w := tablewriter.NewWriter(buf)
w.Append([]string{"FunctionName", o.FunctionName})
w.Append([]string{"FunctionArn", o.FunctionArn})
w.Append([]string{"Version", o.Version})
if o.Runtime != "" {
w.Append([]string{"Runtime", o.Runtime})
}
w.Append([]string{"PackageType", o.PackageType})
w.Append([]string{"State", o.State})
w.Append([]string{"LastUpdateState", o.LastUpdateState})
if o.FunctionURL != "" {
w.Append([]string{"FunctionURL", o.FunctionURL})
}
w.Render()
return buf.String()
}
// Status prints status of function
func (app *App) Status(ctx context.Context, opt *StatusOption) error {
fn, err := app.loadFunction(app.functionFilePath)
if err != nil {
return fmt.Errorf("failed to load function: %w", err)
}
name := *fn.FunctionName
res, err := app.lambda.GetFunction(ctx, &lambda.GetFunctionInput{
FunctionName: &name,
Qualifier: opt.Qualifier,
})
if err != nil {
return fmt.Errorf("failed to GetFunction %s: %w", name, err)
}
out := &StatusOutput{
FunctionName: aws.ToString(res.Configuration.FunctionName),
FunctionArn: aws.ToString(res.Configuration.FunctionArn),
Version: aws.ToString(res.Configuration.Version),
Runtime: string(res.Configuration.Runtime),
PackageType: string(res.Configuration.PackageType),
State: string(res.Configuration.State),
LastUpdateState: string(res.Configuration.LastUpdateStatus),
}
if res, err := app.lambda.GetFunctionUrlConfig(ctx, &lambda.GetFunctionUrlConfigInput{
FunctionName: &name,
Qualifier: opt.Qualifier,
}); err != nil {
var nfe *types.ResourceNotFoundException
if errors.As(err, &nfe) {
// do nothing
} else {
return fmt.Errorf("failed to GetFunctionUrlConfig %s: %w", name, err)
}
} else {
out.FunctionURL = aws.ToString(res.FunctionUrl)
}
switch opt.Output {
case "table":
fmt.Print(out.String())
case "json":
b, _ := marshalJSON(out)
fmt.Print(string(b))
}
return nil
}