-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathservant-cli.el
172 lines (124 loc) · 4.52 KB
/
servant-cli.el
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
;;; servant-cli.el --- Servant: CLI frontend -*- lexical-binding: t; -*-
;; Copyright (C) 2013 Johan Andersson
;; Copyright (C) 2013 Sebastian Wiesner <[email protected]>
;; Author: Johan Andersson <[email protected]>
;; Sebastian Wiesner <[email protected]>
;; Maintainer: Johan Andersson <[email protected]>
;; Sebastian Wiesner <[email protected]>
;; Keywords: elpa, server
;; URL: http://github.com/rejeep/servant.el
;; This file is NOT part of GNU Emacs.
;;; License:
;; 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 <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Command line interface for Servant.
;;; Code:
(require 'f)
(require 'servant (f-join (f-parent (f-this-file)) "servant"))
(require 'commander)
(require 'ansi)
(require 'shut-up)
(require 'web-server)
(require 'eieio)
(require 'cl)
(shut-up-silence-emacs)
(defvar servant-pid-file nil
"User overwritten PID file path.")
(defvar servant-port 9191
"Server port.")
(defvar servant-root-path default-directory
"Run commands with this as root.")
(defvar servant-packages-path nil
"Path to packages directory.")
(defun servant-path ()
"Path to main Servant directory."
(f-expand "servant" servant-root-path))
(defun servant-tmp-path ()
"Path to tmp directory."
(f-expand "tmp" (servant-path)))
(defun servant-packages-path ()
"Path to package directory."
(or servant-packages-path (f-expand "packages" (servant-path))))
(defun servant-index-file ()
"Path to index (archive content) file."
(f-expand "archive-contents" (servant-packages-path)))
(defun servant-pid-file ()
"Path to server PID file."
(or servant-pid-file (f-expand "servant.pid" (servant-tmp-path))))
;;;; Options
(defun servant/pid (pid-file)
"Set path to PID file to PID-FILE.
Default is servant/tmp/servant.pid."
(setq servant-pid-file pid-file))
(defun servant/port (port)
"Set server PORT, defaulting to 9191."
(setq servant-port (string-to-number port)))
(defun servant/path (path)
"Set PATH as root path when running command."
(setq servant-root-path path))
(defun servant/packages-path (packages-path)
"Set path to packages directory to PACKAGES-PATH.
If PACKAGES-PATH is relative, it will be relative
`default-directory' or the `--path' option is specified."
(setq servant-packages-path
(if (f-absolute? packages-path)
packages-path
(f-expand packages-path servant-root-path))))
(defun servant/debug ()
"Enable debug information."
(setq debug-on-error t))
;;;; Commands
(defun servant/help ()
"Show Servant usage information."
(commander-print-usage-and-exit))
(defun servant/init ()
"Initialize the project for Servant."
(when (f-dir? (servant-path))
(error (ansi-red "Directory `servant` already exists.")))
(f-mkdir (servant-path))
(f-mkdir (servant-tmp-path))
(f-mkdir (servant-packages-path))
(message "create %s" (ansi-green "servant"))
(message "create %s" (ansi-green "servant/tmp"))
(message "create %s" (ansi-green "servant/packages")))
(defun servant/start ()
"Start server."
(servant-start)
(while t
(sit-for 1)))
(defun servant/stop ()
"Stop server."
(servant-stop))
(defun servant/index ()
"Generate index (archive-contents) file for all packages."
(f-write (servant--create-index-string (servant-packages-path))
'utf-8 (servant-index-file)))
;;;; Commander schema
(commander
(name "servant")
(description "Serve ELPA packages")
(config ".servant")
(default servant/help)
(option "-h, --help" servant/help)
(option "-p <port>, --port <port>" servant/port)
(option "-P <file>, --pid <file>" servant/pid)
(option "--debug" servant/debug)
(option "--index" servant/index)
(option "--path <path>" servant/path)
(option "--packages-path <packages-path>" servant/packages-path)
(command "init" servant/init)
(command "index" servant/index)
(command "help" servant/help)
(command "start" servant/start)
(command "stop" servant/stop))
(provide 'servant-cli)
;;; servant-cli.el ends here