From 1ce1552763ceae4219e4f99bcbb8df3d29c55ef8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20N=C3=A1jera?= Date: Sun, 28 Aug 2022 19:59:20 +0200 Subject: shepherd manager ensure service is set --- elisp/shepherd.el | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) (limited to 'elisp') diff --git a/elisp/shepherd.el b/elisp/shepherd.el index 6cf2e1b..22acbb1 100644 --- a/elisp/shepherd.el +++ b/elisp/shepherd.el @@ -20,14 +20,13 @@ ;;; Code: (require 'subr-x) -(require 'dash) (require 'helm) (defun shepherd-command (&rest cmds) "Call herd with CMDS." - (with-output-to-string - (with-current-buffer standard-output - (apply #'call-process "herd" nil t nil cmds)))) + (with-temp-buffer + (apply #'call-process "herd" nil t nil cmds) + (string-trim (buffer-string)))) (defun shepherd-services () "List all enabled shepherd services." @@ -37,12 +36,17 @@ (let (matches) (while (search-forward-regexp (rx (group (or "+" "-")) space (group (+ any))) nil t) (let ((status (if (string= (match-string 1) "+") - (propertize "started" 'face 'font-lock-function-name-face) - (propertize "stopped" 'face 'font-lock-variable-name-face))) + (propertize "start" 'face 'font-lock-function-name-face) + (propertize "stop" 'face 'font-lock-variable-name-face))) (service (match-string 2))) (push (cons service status) matches))) (sort matches (lambda (a b) (string< (car a) (car b))))))) +(defun shepherd-set (service status) + "Ensure SERVICE is in STATUS." + (unless (string= status (cdr (assoc service (shepherd-services)))) + (shepherd-command (symbol-name status) service))) + (defun shepherd () "Manage shepherd services." (interactive) @@ -50,17 +54,15 @@ :prompt "Shepherd service:" :sources (helm-build-sync-source "Services" :candidates - (--map (cons (format "%s\t%s" (car it) (cdr it)) - (car it)) - (shepherd-services)) + (cl-loop for it in (shepherd-services) + collect (cons (format "%s\t%s" (cdr it) (car it)) + (car it))) :action (mapcar (lambda (action) (let ((name (symbol-name action))) `(,name . (lambda (service) - (message - (string-trim - (shepherd-command ,name service))))))) + (message (shepherd-command ,name service)))))) '(status start stop restart))))) (provide 'shepherd) -- cgit v1.2.3