forked from imjerrybao/apex
/
list.go
107 lines (85 loc) · 2.22 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
// 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/spf13/cobra"
"github.com/apex/apex/cmd/apex/root"
"github.com/apex/apex/colors"
"github.com/apex/apex/stats"
)
// 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 {
stats.Track("List", map[string]interface{}{
"tfvars": tfvars,
})
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 {
config, 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 err != nil {
fmt.Println()
continue // ignore
}
fmt.Printf(" current version: %s\n", *config.Configuration.Version)
fmt.Println()
}
}