-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaws-lambda.el
168 lines (143 loc) · 6.35 KB
/
aws-lambda.el
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
157
158
159
160
161
162
163
164
165
166
167
168
;;; aws-lambda.el --- Emacs major modes wrapping the AWS CLI
;; Copyright (C) 2022, Marcel Patzwahl
;; This file is NOT part of Emacs.
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2 of the
;; License, or (at your option) any later version.
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software
;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
;; USA
;; Version: 1.0
;; Author: Marcel Patzwahl
;; Keywords: aws cli tools
;; URL: https://github.com/snowiow/aws.el
;; License: GNU General Public License >= 3
;; Package-Requires: ((emacs "28.1"))
;;; Commentary:
;; Emacs major modes wrapping the AWS CLI
;;; Code:
(require 'transient)
(defun aws-lambda--list-functions ()
"List all Lambda Functions."
(fset 'aws--last-view 'aws-lambda)
(aws-core--tabulated-list-from-command
"lambda list-functions --output=text --query 'Functions[*].FunctionName'"
[("Functions" 100)]))
(defun aws-lambda-get-function ()
"Describe the Lambda Function under the cursor.
This function is used in the AWS Lambda Mode."
(interactive)
(aws-core--describe-current-resource "lambda get-function --function-name"))
(defun aws-lambda-get-latest-logs ()
"Get the latest logs of the Lambda Function under the cursor.
This function is used in the AWS Lambda Mode."
(interactive)
(let* ((function-name (aref (tabulated-list-get-entry) 0))
(log-group-name (concat "/aws/lambda/" function-name))
(latest-log-stream
(car
(split-string
(shell-command-to-string
(concat
(aws-cmd)
(aws-log-streams-get-latest-logs-command log-group-name "1"))) "\n"))))
(aws-log-streams-get-log-event log-group-name latest-log-stream)))
(defun aws-lambda-describe-log-streams ()
"List all log streams for the Lambda Function under the cursor.
This functions is used in the AWS Lambda Mode."
(interactive)
(let* ((function-name (aref (tabulated-list-get-entry) 0))
(log-group-name (concat "/aws/lambda/" function-name)))
(aws-log-streams log-group-name)))
(defun aws-lambda-invoke (&optional args)
"Invoke the Lambda Function under the cursor.
ARGS represent the arguments set in the transient."
(interactive (list (transient-args 'aws-lambda-invoke-popup)))
(let* ((function-name (aref (tabulated-list-get-entry) 0))
(outfile-path (aws-lambda--tmp-outfile function-name))
(subcmd
(concat
"lambda invoke --function-name "
function-name " "
(mapconcat 'identity args " ")
" " outfile-path))
(buffer (concat "*" subcmd "*"))
(cmd (concat (aws-cmd) subcmd)))
(call-process-shell-command cmd nil buffer)
(switch-to-buffer buffer)
(with-current-buffer buffer (aws-text-view-mode))))
(defun aws-lambda--tmp-outfile (function-name)
"Location where the Lambda Function FUNCTION-NAME output is stored."
(concat "/tmp/aws-el-lambda-" function-name "-output.json"))
(defun aws-lambda-view-last-execution ()
"Open the output of the last execution in a buffer."
(interactive)
(let* ((function-name (aref (tabulated-list-get-entry) 0))
(outfile-path (aws-lambda--tmp-outfile function-name)))
(switch-to-buffer (find-file-other-window outfile-path))))
;; TRANSIENTS
(transient-define-prefix aws-lambda-help-popup ()
"AWS Lambda Menu"
["Actions"
("RET" "Get Function" aws-lambda-get-function)
("e" "List Event Sources" aws-lambda-event-source-mapping-list-from-line-under-cursor)
("i" "Invoke Function" aws-lambda-invoke-popup)
("l" "Get log streams" aws-lambda-describe-log-streams)
("L" "Get latest logs" aws-lambda-get-latest-logs)
("o" "View oufile from functions last execution" aws-lambda-view-last-execution)
("P" "Set AWS Profile" aws-set-profile)
("q" "Service Overview" aws)])
(transient-define-prefix aws-lambda-invoke-popup ()
"AWS Lambda Invoke Transient"
["Arguments"
("c" "[string] Up to 3583 bytes of base64 encoded data about the invoking client to pass to the function in the context object." "--client-context=")
(aws-lambda-invoke-log-type)
("p" "[string] The JSON that you want to provide to your Lambda function as input." "--payload=")
("q" "[string] Specify a version or alias to invoke a published version of the function." "--qualifier=")
(aws-lambda-invoke-invocation-type)]
["Invoke"
("i" "invoke function" aws-lambda-invoke)])
(transient-define-argument aws-lambda-invoke-invocation-type ()
:description "Invocation Type"
:class 'transient-switches
:key "t"
:argument-format "--invocation-type=%s"
:argument-regexp "\\(RequestResponse\\|Event\\|DryRun\\)"
:choices '("RequestResponse" "Event" "DryRun"))
(transient-define-argument aws-lambda-invoke-log-type ()
:description "Set to Tail to include the execution logs in the response"
:class 'transient-switches
:key "l"
:argument-format "--log-type=%s"
:argument-regexp "\\(None\\|Tail\\)"
:choices '("None" "Tail"))
(defvar aws-lambda-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "RET") 'aws-lambda-get-function)
(define-key map (kbd "?") 'aws-lambda-help-popup)
(define-key map (kbd "e") 'aws-lambda-event-source-mapping-list-from-line-under-cursor)
(define-key map (kbd "i") 'aws-lambda-invoke-popup)
(define-key map (kbd "l") 'aws-lambda-describe-log-streams)
(define-key map (kbd "L") 'aws-lambda-get-latest-logs)
(define-key map (kbd "o") 'aws-lambda-view-last-execution)
(define-key map (kbd "P") 'aws-set-profile)
(define-key map (kbd "q") 'aws)
map))
(defun aws-lambda ()
"Open the AWS Lambda Mode."
(interactive)
(aws--pop-to-buffer (aws--buffer-name "lambda"))
(aws-lambda-mode))
(define-derived-mode aws-lambda-mode tabulated-list-mode "aws-lambda"
"AWS Lambda mode"
(setq major-mode 'aws-lambda-mode)
(use-local-map aws-lambda-mode-map)
(aws-lambda--list-functions))
(provide 'aws-lambda)
;;; aws-lambda.el ends here