about summary refs log tree commit diff stats
path: root/lisp/gdrive.el
blob: 41a3660e264f9bd7ceac9d56d655fc581f94b34d (plain)
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
;;; gdrive.el --- Gdrive integration                 -*- lexical-binding: t; -*-

;; Copyright (C) 2022  Case Duckworth

;; Author: Case Duckworth <case@bob>
;; Keywords: convenience, data, docs

;; 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 3 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, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; [[https://github.com/prasmussen/gdrive][gdrive]] is a Go program to interface with Google Drive.  This library connects
;; that to Emacs.

;;; Code:

(require 'cl-lib)

(defgroup gdrive nil
  "Customizations for Emacs's gdrive module."
  :group 'applications
  :prefix "gdrive-")

(defcustom gdrive-bin (executable-find "gdrive")
  "Where gdrive binary is located."
  :type 'string)

(defcustom gdrive-buffer "*gdrive*"
  "Default buffer for gdrive output."
  :type 'string)

;;; Global flags

;;;; -c, --config <configDir>
;;;;; Application path, default: /Users/<user>/.gdrive
(defcustom gdrive-config-dir nil
  "Application path.")

;;;; --refresh-token <refreshToken>
;;;;; Oauth refresh token used to get access token (for advanced users)
(defcustom gdrive-refresh-token nil
  "Oauth refresh token used to get access token.
(For advanced users).")

;;;; --access-token <accessToken>
;;;;; Oauth access token, only recommended for short-lived requests because of
;;;;; short lifetime (for advanced users)
(defcustom gdrive-access-token nil
  "Oauth access token.
Only recommended for short-lived requests because of short
lifetime (for advanced users).")

;;;; --service-account <accountFile>
;;;;; Oauth service account filename, used for server to server communication
;;;;; without user interaction (file is relative to config dir)
(defcustom gdrive-service-account nil
  "Oauth service account filename.
Used for server to server communication without user
interaction (file is relative to config dir).")

(defun gdrive--global-arguments ()
  "Build global arguments for gdrive."
  (append
   (when gdrive-config-dir (list "--config" gdrive-config-dir))
   (when gdrive-refresh-token (list "--refresh-token" gdrive-refresh-token))
   (when gdrive-access-token (list "--access-token" gdrive-access-token))
   (when gdrive-service-account (list "--service-account" gdrive-service-account))))

;;; List files
;; gdrive [global] list [options]
;;;; -m, --max <maxFiles>
;;;; Max files to list, default: 30
;;;; -q, --query <query>
;;;;; Default query: "trashed = false and 'me' in owners". See https://developers.google.com/drive/search-parameters
;;;; --order <sortOrder>
;;;;; Sort order. See https://godoc.org/google.golang.org/api/drive/v3#FilesListCall.OrderBy
;;;; --name-width <nameWidth>
;;;;; Width of name column, default: 40, minimum: 9, use 0 for full width
;; NOTE: gdrive-list will pass 0 for this argument.
;;;; --absolute		Show absolute path to file (will only show path from first parent)
;;;; --no-header	Dont print the header
;; NOTE: gdrive-list will always pass this argument.
;;;; --bytes		Size in bytes
(cl-defun gdrive-list (&key max query order absolute no-header bytes)
  "Run the \"gdrive list\" command.
MAX is the max files to list; it defaults to 30.  QUERY is the
query to pass; the default is \"trashed = false and 'me' in
owners\"."
  (gdrive--run (append (gdrive--global-arguments)
                       (list "list")
                       (when max (list "--max" max))
                       (when query (list "--query" query))
                       (when order (list "--order" order))
                       (list "--name-width" "0")
                       (when absolute (list "--absolute"))
                       (when no-header (list "--no-header"))
                       (when bytes (list "--bytes")))))


(defmacro gdrive-query)


(defun gdrive--build-command-name (command)
  "INTERNAL: Build a string name for COMMAND."
  (concat "gdrive-" (car command)))

(defun gdrive--run (command &optional buffer)
  "Run 'gdrive COMMAND', collecting results in BUFFER.
COMMAND, if not a list, will be made a list and appended to
`gdrive-bin'.
BUFFER defaults to `gdrive-buffer'."
  (let ((command (if (listp command) command (list command)))
        (buffer (or buffer gdrive-buffer)))
    (make-process :name (gdrive--build-command-name command)
                  :buffer buffer
                  :command (cons gdrive-bin command))))

(provide 'gdrive)
;;; gdrive.el ends here