-
Notifications
You must be signed in to change notification settings - Fork 0
/
info.go
90 lines (79 loc) · 2.25 KB
/
info.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
package main
import (
"context"
"fmt"
"strconv"
"strings"
awsconfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/lambda"
"github.com/spf13/cobra"
)
var infoCmd *cobra.Command
func init() {
var ver string
infoCmd = &cobra.Command{
Use: "info function-name",
Short: "Print out info about a function",
Args: cobra.ExactArgs(1),
RunE: func(c *cobra.Command, args []string) error {
fnName := args[0]
inf, err := info(fnName, ver)
if err != nil {
return err
}
return formatOutput(inf)
},
}
addVersionFlag(infoCmd.Flags(), &ver)
}
// info returns information about a function.
func info(fnName string, fnVer string) (map[string]string, error) {
inf := map[string]string{
"name": fnName,
"url": "",
}
ctx := context.Background()
acfg, err := awsconfig.LoadDefaultConfig(ctx)
if err != nil {
return inf, fmt.Errorf("failed to load aws config: %s", err)
}
lambdaCl := lambda.NewFromConfig(acfg)
// We kind of re-implement the login of versionFlag here, but it's necessary
// because there are minor differences and we also need to get more details
// out of the function alias.
if fnVer == latestPseudoVersion {
vers, err := versions(fnName)
if err != nil {
return inf, fmt.Errorf("failed to get versions: %s", err)
}
fnVer = strconv.Itoa(vers[len(vers)-1].Version)
} else if _, err := strconv.Atoi(fnVer); err != nil { // not a number
fu, err := lambdaCl.GetFunctionUrlConfig(ctx, &lambda.GetFunctionUrlConfigInput{
FunctionName: &fnName,
Qualifier: &fnVer,
})
if err != nil {
if !strings.Contains(err.Error(), "ResourceNotFoundException") {
return inf, fmt.Errorf("failed to get function url: %s", err)
}
} else {
inf["url"] = *fu.FunctionUrl
}
}
gfo, err := lambdaCl.GetFunction(ctx, &lambda.GetFunctionInput{
FunctionName: &fnName,
Qualifier: &fnVer,
})
if err != nil {
return inf, err
}
if gfo.Code.ImageUri == nil {
return inf, fmt.Errorf("function %s is not an docker image function", fnName)
}
inf["version"] = *gfo.Configuration.Version
inf["image"] = *gfo.Code.ImageUri
inf["resolved_image"] = *gfo.Code.ResolvedImageUri
inf["role"] = *gfo.Configuration.Role
inf["timestamp"] = *gfo.Configuration.LastModified
return inf, nil
}