search.scm 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2018 Clément Lassieur <clement@lassieur.org>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (guix scripts system search)
  20. #:use-module (guix ui)
  21. #:use-module (guix utils)
  22. #:use-module (gnu services)
  23. #:use-module (gnu services shepherd)
  24. #:use-module (srfi srfi-1)
  25. #:use-module (srfi srfi-11)
  26. #:use-module (srfi srfi-26)
  27. #:use-module (srfi srfi-34)
  28. #:use-module (ice-9 format)
  29. #:use-module (ice-9 regex)
  30. #:use-module (ice-9 match)
  31. #:export (service-type->recutils
  32. find-service-types
  33. guix-system-search))
  34. ;;; Commentary:
  35. ;;;
  36. ;;; Implement the 'guix system search' command, which searches among the
  37. ;;; available service types.
  38. ;;;
  39. ;;; Code:
  40. (define service-type-name*
  41. (compose symbol->string service-type-name))
  42. (define (service-type-default-shepherd-services type)
  43. "Return the list of Shepherd services created by default instances of TYPE,
  44. provided TYPE has a default value."
  45. (match (guard (c ((service-error? c) #f))
  46. (service type))
  47. (#f '())
  48. ((? service? service)
  49. (let* ((extension (find (lambda (extension)
  50. (eq? (service-extension-target extension)
  51. shepherd-root-service-type))
  52. (service-type-extensions type)))
  53. (compute (and extension (service-extension-compute extension))))
  54. (if compute
  55. (compute (service-value service))
  56. '())))))
  57. (define (service-type-shepherd-names type)
  58. "Return the default names of Shepherd services created for TYPE."
  59. (append-map shepherd-service-provision
  60. (service-type-default-shepherd-services type)))
  61. (define* (service-type->recutils type port
  62. #:optional (width (%text-width))
  63. #:key
  64. (extra-fields '())
  65. (hyperlinks? (supports-hyperlinks? port)))
  66. "Write to PORT a recutils record of TYPE, arranging to fit within WIDTH
  67. columns. When HYPERLINKS? is true, emit hyperlink escape sequences when
  68. appropriate."
  69. (define width*
  70. ;; The available number of columns once we've taken into account space for
  71. ;; the initial "+ " prefix.
  72. (if (> width 2) (- width 2) width))
  73. (define (extensions->recutils extensions)
  74. (let ((list (string-join (map (compose service-type-name*
  75. service-extension-target)
  76. extensions))))
  77. (string->recutils
  78. (fill-paragraph list width*
  79. (string-length "extends: ")))))
  80. ;; Note: Don't i18n field names so that people can post-process it.
  81. (format port "name: ~a~%" (service-type-name type))
  82. (format port "location: ~a~%"
  83. (or (and=> (service-type-location type)
  84. (if hyperlinks? location->hyperlink location->string))
  85. (G_ "unknown")))
  86. (format port "extends: ~a~%"
  87. (extensions->recutils (service-type-extensions type)))
  88. ;; If possible, display the list of *default* Shepherd service names. Note
  89. ;; that we may not always be able to do this (e.g., if the service type
  90. ;; lacks a default value); furthermore, it could be that the service
  91. ;; generates Shepherd services with different names if we give it different
  92. ;; parameters (this is the case, for instance, for
  93. ;; 'console-font-service-type'.)
  94. (match (service-type-shepherd-names type)
  95. (() #f)
  96. (names (format port "shepherdnames:~{ ~a~}~%" names)))
  97. (when (service-type-description type)
  98. (format port "~a~%"
  99. (string->recutils
  100. (string-trim-right
  101. (parameterize ((%text-width width*))
  102. (texi->plain-text
  103. (string-append "description: "
  104. (or (and=> (service-type-description type) P_)
  105. ""))))
  106. #\newline))))
  107. (for-each (match-lambda
  108. ((field . value)
  109. (let ((field (symbol->string field)))
  110. (format port "~a: ~a~%"
  111. field
  112. (fill-paragraph (object->string value) width*
  113. (string-length field))))))
  114. extra-fields)
  115. (newline port))
  116. (define (service-type-description-string type)
  117. "Return the rendered and localised description of TYPE, a service type."
  118. (and=> (service-type-description type)
  119. (compose texi->plain-text P_)))
  120. (define %service-type-metrics
  121. ;; Metrics used to estimate the relevance of a search result.
  122. `((,service-type-name* . 3)
  123. (,service-type-description-string . 2)
  124. (,(lambda (type)
  125. (match (and=> (service-type-location type) location-file)
  126. ((? string? file)
  127. (basename file ".scm"))
  128. (#f
  129. "")))
  130. . 1)))
  131. (define (find-service-types regexps)
  132. "Return a list of service type/score pairs: service types whose name or
  133. description matches REGEXPS sorted by relevance, and their score."
  134. (let ((matches (fold-service-types
  135. (lambda (type result)
  136. (match (relevance type regexps
  137. %service-type-metrics)
  138. ((? zero?)
  139. result)
  140. (score
  141. (cons (cons type score) result))))
  142. '())))
  143. (sort matches
  144. (lambda (m1 m2)
  145. (match m1
  146. ((type1 . score1)
  147. (match m2
  148. ((type2 . score2)
  149. (if (= score1 score2)
  150. (string>? (service-type-name* type1)
  151. (service-type-name* type2))
  152. (> score1 score2))))))))))
  153. (define (guix-system-search . args)
  154. (with-error-handling
  155. (let* ((regexps (map (cut make-regexp* <> regexp/icase) args))
  156. (matches (find-service-types regexps)))
  157. (leave-on-EPIPE
  158. (display-search-results matches (current-output-port)
  159. #:print service-type->recutils
  160. #:command "guix system search")))))