diff options
-rw-r--r-- | lisp/gdrive.el | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/lisp/gdrive.el b/lisp/gdrive.el new file mode 100644 index 0000000..41a3660 --- /dev/null +++ b/lisp/gdrive.el | |||
@@ -0,0 +1,130 @@ | |||
1 | ;;; gdrive.el --- Gdrive integration -*- lexical-binding: t; -*- | ||
2 | |||
3 | ;; Copyright (C) 2022 Case Duckworth | ||
4 | |||
5 | ;; Author: Case Duckworth <case@bob> | ||
6 | ;; Keywords: convenience, data, docs | ||
7 | |||
8 | ;; This program is free software; you can redistribute it and/or modify | ||
9 | ;; it under the terms of the GNU General Public License as published by | ||
10 | ;; the Free Software Foundation, either version 3 of the License, or | ||
11 | ;; (at your option) any later version. | ||
12 | |||
13 | ;; This program is distributed in the hope that it will be useful, | ||
14 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
16 | ;; GNU General Public License for more details. | ||
17 | |||
18 | ;; You should have received a copy of the GNU General Public License | ||
19 | ;; along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
20 | |||
21 | ;;; Commentary: | ||
22 | |||
23 | ;; [[https://github.com/prasmussen/gdrive][gdrive]] is a Go program to interface with Google Drive. This library connects | ||
24 | ;; that to Emacs. | ||
25 | |||
26 | ;;; Code: | ||
27 | |||
28 | (require 'cl-lib) | ||
29 | |||
30 | (defgroup gdrive nil | ||
31 | "Customizations for Emacs's gdrive module." | ||
32 | :group 'applications | ||
33 | :prefix "gdrive-") | ||
34 | |||
35 | (defcustom gdrive-bin (executable-find "gdrive") | ||
36 | "Where gdrive binary is located." | ||
37 | :type 'string) | ||
38 | |||
39 | (defcustom gdrive-buffer "*gdrive*" | ||
40 | "Default buffer for gdrive output." | ||
41 | :type 'string) | ||
42 | |||
43 | ;;; Global flags | ||
44 | |||
45 | ;;;; -c, --config <configDir> | ||
46 | ;;;;; Application path, default: /Users/<user>/.gdrive | ||
47 | (defcustom gdrive-config-dir nil | ||
48 | "Application path.") | ||
49 | |||
50 | ;;;; --refresh-token <refreshToken> | ||
51 | ;;;;; Oauth refresh token used to get access token (for advanced users) | ||
52 | (defcustom gdrive-refresh-token nil | ||
53 | "Oauth refresh token used to get access token. | ||
54 | (For advanced users).") | ||
55 | |||
56 | ;;;; --access-token <accessToken> | ||
57 | ;;;;; Oauth access token, only recommended for short-lived requests because of | ||
58 | ;;;;; short lifetime (for advanced users) | ||
59 | (defcustom gdrive-access-token nil | ||
60 | "Oauth access token. | ||
61 | Only recommended for short-lived requests because of short | ||
62 | lifetime (for advanced users).") | ||
63 | |||
64 | ;;;; --service-account <accountFile> | ||
65 | ;;;;; Oauth service account filename, used for server to server communication | ||
66 | ;;;;; without user interaction (file is relative to config dir) | ||
67 | (defcustom gdrive-service-account nil | ||
68 | "Oauth service account filename. | ||
69 | Used for server to server communication without user | ||
70 | interaction (file is relative to config dir).") | ||
71 | |||
72 | (defun gdrive--global-arguments () | ||
73 | "Build global arguments for gdrive." | ||
74 | (append | ||
75 | (when gdrive-config-dir (list "--config" gdrive-config-dir)) | ||
76 | (when gdrive-refresh-token (list "--refresh-token" gdrive-refresh-token)) | ||
77 | (when gdrive-access-token (list "--access-token" gdrive-access-token)) | ||
78 | (when gdrive-service-account (list "--service-account" gdrive-service-account)))) | ||
79 | |||
80 | ;;; List files | ||
81 | ;; gdrive [global] list [options] | ||
82 | ;;;; -m, --max <maxFiles> | ||
83 | ;;;; Max files to list, default: 30 | ||
84 | ;;;; -q, --query <query> | ||
85 | ;;;;; Default query: "trashed = false and 'me' in owners". See https://developers.google.com/drive/search-parameters | ||
86 | ;;;; --order <sortOrder> | ||
87 | ;;;;; Sort order. See https://godoc.org/google.golang.org/api/drive/v3#FilesListCall.OrderBy | ||
88 | ;;;; --name-width <nameWidth> | ||
89 | ;;;;; Width of name column, default: 40, minimum: 9, use 0 for full width | ||
90 | ;; NOTE: gdrive-list will pass 0 for this argument. | ||
91 | ;;;; --absolute Show absolute path to file (will only show path from first parent) | ||
92 | ;;;; --no-header Dont print the header | ||
93 | ;; NOTE: gdrive-list will always pass this argument. | ||
94 | ;;;; --bytes Size in bytes | ||
95 | (cl-defun gdrive-list (&key max query order absolute no-header bytes) | ||
96 | "Run the \"gdrive list\" command. | ||
97 | MAX is the max files to list; it defaults to 30. QUERY is the | ||
98 | query to pass; the default is \"trashed = false and 'me' in | ||
99 | owners\"." | ||
100 | (gdrive--run (append (gdrive--global-arguments) | ||
101 | (list "list") | ||
102 | (when max (list "--max" max)) | ||
103 | (when query (list "--query" query)) | ||
104 | (when order (list "--order" order)) | ||
105 | (list "--name-width" "0") | ||
106 | (when absolute (list "--absolute")) | ||
107 | (when no-header (list "--no-header")) | ||
108 | (when bytes (list "--bytes"))))) | ||
109 | |||
110 | |||
111 | (defmacro gdrive-query) | ||
112 | |||
113 | |||
114 | (defun gdrive--build-command-name (command) | ||
115 | "INTERNAL: Build a string name for COMMAND." | ||
116 | (concat "gdrive-" (car command))) | ||
117 | |||
118 | (defun gdrive--run (command &optional buffer) | ||
119 | "Run 'gdrive COMMAND', collecting results in BUFFER. | ||
120 | COMMAND, if not a list, will be made a list and appended to | ||
121 | `gdrive-bin'. | ||
122 | BUFFER defaults to `gdrive-buffer'." | ||
123 | (let ((command (if (listp command) command (list command))) | ||
124 | (buffer (or buffer gdrive-buffer))) | ||
125 | (make-process :name (gdrive--build-command-name command) | ||
126 | :buffer buffer | ||
127 | :command (cons gdrive-bin command)))) | ||
128 | |||
129 | (provide 'gdrive) | ||
130 | ;;; gdrive.el ends here | ||