@@ -142,6 +142,80 @@ format."
142142 (pythonic-unaliased-path (concat connection filename)))
143143 filename))
144144
145+
146+ ; ;; Docker Compose.
147+
148+ (defvar pythonic-docker-compose-filename " docker-compose.yml" )
149+
150+ (defvar pythonic-read-docker-compose-file-code "
151+ from __future__ import print_function
152+ import json, sys, yaml
153+ print(json.dumps(yaml.safe_load(open(sys.argv[-1], 'r'))))
154+ " )
155+
156+ (defun pythonic-get-docker-compose-project ()
157+ " Get directory where `pythonic-docker-compose-filename' is present."
158+ (let ((project (locate-dominating-file default-directory pythonic-docker-compose-filename)))
159+ (when project
160+ (f-full project))))
161+
162+ (defun pythonic-get-docker-compose-filename (project )
163+ " Get full path to the docker-compose PROJECT configuration file."
164+ (f-join project pythonic-docker-compose-filename))
165+
166+ (defun pythonic-read-docker-compose-file (filename )
167+ " Read docker-compose project configuration FILENAME."
168+ (let ((json-key-type 'string )
169+ (json-array-type 'list ))
170+ (json-read-from-string
171+ (with-output-to-string
172+ (with-current-buffer
173+ standard-output
174+ (call-process " python" nil t nil " -c" pythonic-read-docker-compose-file-code filename))))))
175+
176+ (defun pythonic-get-docker-compose-volumes (struct )
177+ " Get docker volume list from the compose STRUCT."
178+ (let (volumes)
179+ (dolist (service (cdr (assoc " services" struct)))
180+ (dolist (volume (cdr (assoc " volumes" service)))
181+ (when (s-starts-with-p " .:" volume)
182+ (push (cons (car service) (s-chop-prefix " .:" volume)) volumes))))
183+ volumes))
184+
185+ (defun pythonic-get-docker-compose-container (filename service )
186+ " Get container name from the FILENAME project for SERVICE name."
187+ (s-trim
188+ ; ; FIXME: It is possible to have many running containers for given
189+ ; ; service.
190+ (with-output-to-string
191+ (with-current-buffer
192+ standard-output
193+ (call-process " docker-compose" nil t nil
194+ " --file" filename " ps" " --quiet" service)))))
195+
196+ (defun pythonic-set-docker-compose-alias ()
197+ " Build alias string for current docker-compose project."
198+ (unless
199+ (or (tramp-tramp-file-p default-directory)
200+ (pythonic-has-alias-p default-directory))
201+ (let ((project (pythonic-get-docker-compose-project)))
202+ (when project
203+ (let* ((filename (pythonic-get-docker-compose-filename project))
204+ (struct (pythonic-read-docker-compose-file filename))
205+ (volumes (pythonic-get-docker-compose-volumes struct))
206+ (volume (if (< 1 (length volumes))
207+ (assoc (completing-read " Service: " (mapcar 'car volumes) nil t ) volumes)
208+ (car volumes)))
209+ (service (car volume))
210+ (mount (cdr volume))
211+ (container (pythonic-get-docker-compose-container filename service))
212+ ; ; FIXME: Get actual user for the connection string.
213+ (connection (format " /docker:root@%s :%s " container mount))
214+ (alias (list project connection)))
215+ (unless (s-blank-p container)
216+ (push alias pythonic-directory-aliases))
217+ alias)))))
218+
145219
146220; ;; Processes.
147221
0 commit comments