/
list.go
120 lines (99 loc) · 2.57 KB
/
list.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
// Package list outputs a list of Lambda function information.
package list
import (
"fmt"
"github.com/apex/log"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/tj/cobra"
"github.com/latticehr/apex/cmd/apex/root"
"github.com/latticehr/apex/colors"
)
// tfvars output format.
var tfvars bool
// example output.
const example = `
List all functions
$ apex list
List functions based on glob
$ apex list api_*
Output list as Terraform variables (.tfvars)
$ apex list --tfvars`
// Command config.
var Command = &cobra.Command{
Use: "list [<name>...]",
Short: "Output functions list",
Example: example,
RunE: run,
}
// Initialize.
func init() {
root.Register(Command)
f := Command.Flags()
f.BoolVar(&tfvars, "tfvars", false, "Output as Terraform variables")
}
// Run command.
func run(c *cobra.Command, args []string) error {
if err := root.Project.LoadFunctions(args...); err != nil {
return err
}
if tfvars {
outputTFvars()
} else {
outputList()
}
return nil
}
// outputTFvars format.
func outputTFvars() {
for _, fn := range root.Project.Functions {
config, err := fn.GetConfig()
if err != nil {
log.Debugf("can't fetch function config: %s", err.Error())
continue
}
fmt.Printf("apex_function_%s=%q\n", fn.Name, *config.Configuration.FunctionArn)
}
}
// outputList format.
func outputList() {
fmt.Println()
for _, fn := range root.Project.Functions {
awsFn, err := fn.GetConfigCurrent()
if awserr, ok := err.(awserr.Error); ok && awserr.Code() == "ResourceNotFoundException" {
fmt.Printf(" \033[%dm%s\033[0m (not deployed) \n", colors.Blue, fn.Name)
} else {
fmt.Printf(" \033[%dm%s\033[0m\n", colors.Blue, fn.Name)
}
if fn.Description != "" {
fmt.Printf(" description: %v\n", fn.Description)
}
fmt.Printf(" runtime: %v\n", fn.Runtime)
fmt.Printf(" memory: %vmb\n", fn.Memory)
fmt.Printf(" timeout: %vs\n", fn.Timeout)
fmt.Printf(" role: %v\n", fn.Role)
fmt.Printf(" handler: %v\n", fn.Handler)
if awsFn != nil && awsFn.Configuration != nil && awsFn.Configuration.FunctionArn != nil {
fmt.Printf(" arn: %v\n", *awsFn.Configuration.FunctionArn)
}
if err != nil {
fmt.Println()
continue // ignore
}
aliaslist, err := fn.GetAliases()
if err != nil {
continue
}
var aliases string
for index, alias := range aliaslist.Aliases {
if index > 0 {
aliases += ", "
}
aliases += fmt.Sprintf("%s@v%s", *alias.Name, *alias.FunctionVersion)
}
if aliases == "" {
aliases = "<none>"
}
fmt.Printf(" aliases: %s\n", aliases)
fmt.Println()
}
}