diff mbox series

[bug#50314,2/2] system: Add hint for action typo.

Message ID 20210901095756.13752-2-zimon.toutoune@gmail.com
State Accepted
Headers show
Series Add hint typo for importers and system actions | expand

Checks

Context Check Description
cbaines/applying patch fail View Laminar job
cbaines/issue success View issue

Commit Message

Simon Tournier Sept. 1, 2021, 9:57 a.m. UTC
* guix/scripts/system.scm (actions): New variable.
(define-command): Add hint for action typo.
---
 guix/scripts/system.scm | 31 ++++++++++++++++++++-----------
 1 file changed, 20 insertions(+), 11 deletions(-)
diff mbox series

Patch

diff --git a/guix/scripts/system.scm b/guix/scripts/system.scm
index 83bbefd3dc..65eb98e4b2 100644
--- a/guix/scripts/system.scm
+++ b/guix/scripts/system.scm
@@ -9,6 +9,7 @@ 
 ;;; Copyright © 2020 Julien Lepiller <julien@lepiller.eu>
 ;;; Copyright © 2020 Efraim Flashner <efraim@flashner.co.il>
 ;;; Copyright © 2021 Brice Waegeneire <brice@waegenei.re>
+;;; Copyright © 2021 Simon Tournier <zimon.toutoune@gmail.com>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -1152,6 +1153,13 @@  Some ACTIONS support additional ARGS.\n"))
 ;;; Entry point.
 ;;;
 
+(define actions '("build" "container" "vm" "vm-image" "image" "disk-image"
+                  "reconfigure" "init"
+                  "extension-graph" "shepherd-graph"
+                  "list-generations" "describe"
+                  "delete-generations" "roll-back"
+                  "switch-generation" "search" "docker-image"))
+
 (define (process-action action args opts)
   "Process ACTION, a sub-command, with the arguments are listed in ARGS.
 ACTION must be one of the sub-commands that takes an operating system
@@ -1335,17 +1343,18 @@  argument list and OPTS is the option alist."
 
   (define (parse-sub-command arg result)
     ;; Parse sub-command ARG and augment RESULT accordingly.
-    (if (assoc-ref result 'action)
-        (alist-cons 'argument arg result)
-        (let ((action (string->symbol arg)))
-          (case action
-            ((build container vm vm-image image disk-image reconfigure init
-              extension-graph shepherd-graph
-              list-generations describe
-              delete-generations roll-back
-              switch-generation search docker-image)
-             (alist-cons 'action action result))
-            (else (leave (G_ "~a: unknown action~%") action))))))
+    (cond ((assoc-ref result 'action)
+           (alist-cons 'argument arg result))
+          ((member arg actions)
+           (let ((action (string->symbol arg)))
+             (alist-cons 'action action result)))
+          (else
+           (let ((hint (string-closest arg actions #:threshold 3)))
+             (report-error (G_ "~a: unknown action~%") arg)
+             (when hint
+               (display-hint
+                (format #f (G_ "Did you mean @code{~a}?~%") hint)))
+             (exit 1)))))
 
   (define (match-pair car)
     ;; Return a procedure that matches a pair with CAR.