-
Notifications
You must be signed in to change notification settings - Fork 0
/
versions.go
156 lines (136 loc) · 4.04 KB
/
versions.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"context"
"errors"
"fmt"
"sort"
"strconv"
awsconfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/lambda"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
const latestPseudoVersion = "latest"
// resolveVersion resolves the given version spec to an actual version. If a
// numerical version is provided, it will be returned as is. Otherwise, function
// aliase names are looked up. "latest" is a special case referring to the
// latest version of the function. "latest" is NOT the same as lambda's
// "$LATEST".
func resolveVersion(fnName string, verSpec string) (int, error) {
if verSpec == "" {
return 0, errors.New("version spec must not be empty")
}
if v, err := strconv.Atoi(verSpec); err == nil {
return v, nil
}
if verSpec == latestPseudoVersion {
vers, err := versions(fnName)
if err != nil {
return 0, fmt.Errorf("failed lookup latest version: %s", err)
}
return vers[len(vers)-1].Version, nil
}
lookupVer := &verSpec
ctx := context.Background()
acfg, err := awsconfig.LoadDefaultConfig(ctx)
if err != nil {
return 0, fmt.Errorf("failed to load aws config: %s", err)
}
lambdaCl := lambda.NewFromConfig(acfg)
alias, err := lambdaCl.GetAlias(ctx, &lambda.GetAliasInput{
FunctionName: &fnName,
Name: lookupVer,
})
if err != nil {
return 0, fmt.Errorf("failed to get alias: %s", err)
}
vint, err := strconv.Atoi(*alias.FunctionVersion)
if err != nil {
return 0, fmt.Errorf("failed to parse version: %s", err)
}
return vint, nil
}
// addVersionFlag adds a version flag to the given flag set. This is used in
// various commands that take version information in order to provide
// consistency. The version flag defaults to the active alias.
func addVersionFlag(c *pflag.FlagSet, ver *string) {
c.StringVarP(ver, "version", "v", activeAlias, "the version/alias of the function (use 'latest' for latest version)")
}
var versionsCmd = &cobra.Command{
Use: "versions",
Aliases: []string{"ver", "version"},
Short: "List versions of a function",
Args: cobra.ExactArgs(1),
RunE: func(c *cobra.Command, args []string) error {
fnName := args[0]
vers, err := versions(fnName)
if err != nil {
return err
}
return formatOutput(vers)
},
}
// fnVersion represents a version of a function.
type fnVersion struct {
Version int `json:"version"`
Aliases []string `json:"aliases"`
Description string `json:"description"`
}
// versions returns a list of all versions of the given function.
func versions(fnName string) ([]fnVersion, error) {
vs := []fnVersion{}
ctx := context.Background()
acfg, err := awsconfig.LoadDefaultConfig(ctx)
if err != nil {
return nil, fmt.Errorf("failed to load aws config: %s", err)
}
lambdaCl := lambda.NewFromConfig(acfg)
// Get all aliases and map them from function version to alias name.
aliases := map[string][]string{}
ap := lambda.NewListAliasesPaginator(lambdaCl, &lambda.ListAliasesInput{
FunctionName: &fnName,
})
for ap.HasMorePages() {
page, err := ap.NextPage(ctx)
if err != nil {
return nil, fmt.Errorf("failed to list aliases: %s", err)
}
for _, a := range page.Aliases {
fa, fv := *a.Name, *a.FunctionVersion
aliases[fv] = append(aliases[fv], fa)
}
}
for _, a := range aliases {
sort.StringSlice(a).Sort()
}
p := lambda.NewListVersionsByFunctionPaginator(lambdaCl, &lambda.ListVersionsByFunctionInput{
FunctionName: &fnName,
})
for p.HasMorePages() {
page, err := p.NextPage(ctx)
if err != nil {
return nil, fmt.Errorf("failed to list versions: %s", err)
}
for _, v := range page.Versions {
if *v.Version != "$LATEST" {
intVer, err := strconv.Atoi(*v.Version)
if err != nil {
return nil, fmt.Errorf("failed to convert version to int: %s", err)
}
al := aliases[*v.Version]
if al == nil {
al = []string{}
}
vs = append(vs, fnVersion{
Version: intVer,
Aliases: al,
Description: *v.Description,
})
}
}
}
sort.Slice(vs, func(i, j int) bool {
return vs[i].Version < vs[j].Version
})
return vs, nil
}