From 8cb58cc43cd329fbae75cedd1e889d977f0f9226 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20N=C3=A1jera?= Date: Wed, 20 Apr 2022 16:19:29 +0200 Subject: manage shepherd services --- config/doom/config.org | 5 ++++ elisp/shepherd.el | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 elisp/shepherd.el diff --git a/config/doom/config.org b/config/doom/config.org index 4a057db..b926dfa 100644 --- a/config/doom/config.org +++ b/config/doom/config.org @@ -456,3 +456,8 @@ Emacs mode for managing ledger text files #+begin_src emacs-lisp (use-package! json-rpc) #+end_src +#+begin_src emacs-lisp +(use-package! shepherd + :load-path "~/dev/dotfiles/elisp/" + :commands (shepherd)) +#+end_src diff --git a/elisp/shepherd.el b/elisp/shepherd.el new file mode 100644 index 0000000..4ba5ed0 --- /dev/null +++ b/elisp/shepherd.el @@ -0,0 +1,67 @@ +;;; shepherd.el --- Manage shepherd services -*- lexical-binding: t; -*- +;; +;; Copyright (C) 2022 Óscar Nájera +;; +;; Author: Óscar Nájera +;; Maintainer: Óscar Nájera +;; Created: April 20, 2022 +;; Modified: April 20, 2022 +;; Version: 0.0.1 +;; Keywords: abbrev bib c calendar comm convenience data docs emulations extensions faces files frames games hardware help hypermedia i18n internal languages lisp local maint mail matching mouse multimedia news outlines processes terminals tex tools unix vc wp +;; Homepage: https://github.com/titan/shepherd +;; Package-Requires: ((emacs "25.1")) +;; +;; This file is not part of GNU Emacs. +;; +;;; Commentary: +;; +;; Manage shepherd services +;; +;;; 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)))) + +(defun shepherd-services () + "List all enabled shepherd services." + (with-temp-buffer + (insert (shepherd-command "status")) + (goto-char (point-min)) + (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))) + (service (match-string 2))) + (push (cons service status) matches))) + (sort matches (lambda (a b) (string< (car a) (car b))))))) + +(defun shepherd () + "Manage shepherd services." + (interactive) + (helm + :prompt "Shepherd service:" + :sources (helm-build-sync-source "Services" + :candidates + (--map (cons (format "%s\t%s" (car it) (cdr it)) + (car it)) + (shepherd-services)) + :action + (mapcar (lambda (action) + (let ((name (symbol-name action))) + `(,name . + (lambda (service) + (message + (string-trim + (shepherd-command ,name service))))))) + '(status start stop))))) + +(provide 'shepherd) +;;; shepherd.el ends here -- cgit v1.2.3