-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaws-core.el
120 lines (103 loc) · 4.41 KB
/
aws-core.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
;;; aws-core.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:
(defvar aws-core--tmp-file-path
"/tmp/awsel/")
(defun aws-core-write-buffer-to-tmp-file (filename)
"Write the current buffer to FILENAME in /tmp/awsel/."
(unless (file-exists-p aws-core--tmp-file-path)
(make-directory aws-core--tmp-file-path))
(let ((filepath (concat aws-core--tmp-file-path filename)))
(write-region (point-min) (point-max) filepath)
filepath))
(defun aws-core--tabulated-list-from-command (cmd header)
"Displays an aws service list command in a 1 column tabulated-list-view.
CMD is the aws command to get the resources to list.
HEADER configures the column header for the tabulated-list-view."
(let ((rows
(mapcar
(lambda (x)
(let ((trimmed-x (string-trim x)))
(list trimmed-x (vector trimmed-x))))
(split-string
(shell-command-to-string
(concat (aws-cmd) cmd)) "\t"))))
(setq tabulated-list-format header)
(setq tabulated-list-entries rows)
(tabulated-list-init-header)
(tabulated-list-print)
(hl-line-mode 1)))
(defun aws-core--tabulated-list-from-command-multi-column (cmd header)
"Displays an aws service list command in a multi column tabulated-list-view.
CMD is the aws command to get the resources to list.
HEADER configures the column header for the tabulated-list-view."
(let* ((rows (mapcar (lambda (x)
(let ((splitted (split-string x "\t")))
(list (car splitted) (vconcat splitted))))
(butlast
(split-string
(shell-command-to-string
(concat
(aws-cmd)
cmd)) "\n")))))
(setq tabulated-list-format header)
(setq tabulated-list-entries rows)
(tabulated-list-init-header)
(tabulated-list-print)
(hl-line-mode 1)))
(defun aws-core--describe-current-resource (cmd &optional name)
"Describe resource under cursor. CMD is the aws command to describe the resource.
If NAME is passed it's used as the currend resource to describe, otherwise the entry in tabulated-list-view is taken."
(let* ((current-resource (if name
name
(tabulated-list-get-id)))
(service-name (car (split-string cmd)))
(buffer (concat (aws--buffer-name service-name) ": " cmd " " current-resource "*"))
(cmd (concat (aws-cmd)
"--output "
aws-output
" "
cmd
" "
current-resource)))
(call-process-shell-command cmd nil buffer)
(pop-to-buffer buffer)
(with-current-buffer buffer
(aws--get-view-mode))
(goto-line 1)))
(defun aws-core--get-current-line ()
"Get the current line where point is."
(+ 1 (count-lines 1 (point))))
(defun aws-core--refresh-list-view (list-function &rest args)
"Refresh the current tabulated list view.
LIST-FUNCTION is the function to load the list and ARGS will
be passed to LIST-FUNCTION if needed."
(message "Refreshing buffer...")
(if (> (length args) 0)
(funcall list-function args)
(funcall list-function))
(forward-line (aws-core--get-current-line))
(message "Buffer refreshed"))
(provide 'aws-core)
;;; aws-core.el ends here