[bug#33448,2/3] describe: Add json format.

Message ID 20181121141715.16417-2-go.wigust@gmail.com
State Accepted
Headers show
Series [bug#33448,1/3] describe: Use a procedure to format output. | expand

Checks

Context Check Description
cbaines/applying patch success Successfully applied
cbaines/applying patch fail Apply failed

Commit Message

Oleg Pykhalov Nov. 21, 2018, 2:17 p.m. UTC
* guix/channels.scm (channel->json): New procedure.
* guix/scripts/describe.scm (%options): Add 'json' option.
(display-checkout-info, display-profile-info): Use 'channel->json'.
* doc/guix.texi (Invoking guix describe): Document this.
---
 doc/guix.texi             |  5 ++++-
 guix/channels.scm         | 12 ++++++++++++
 guix/scripts/describe.scm | 13 ++++++++++---
 3 files changed, 26 insertions(+), 4 deletions(-)

Comments

Ludovic Courtès Nov. 21, 2018, 9:33 p.m. UTC | #1
Oleg Pykhalov <go.wigust@gmail.com> skribis:

> * guix/channels.scm (channel->json): New procedure.
> * guix/scripts/describe.scm (%options): Add 'json' option.
> (display-checkout-info, display-profile-info): Use 'channel->json'.
> * doc/guix.texi (Invoking guix describe): Document this.

[...]

> +(define channel->json
> +  (match-lambda
> +    (($ <channel> name url branch commit location)
> +     (scm->json-string
> +      (list
> +       (cons 'name name)
> +       (cons 'url url)
> +       (cons 'commit commit))))))

Since this is not a core feature of channels and add a dependency on
(json), what about moving this procedure to (guix scripts describe)?

(‘channel->sexp’ could got to (guix scripts describe) as well maybe; no
big deal.)

Otherwise LGTM!

Ludo’.
Oleg Pykhalov Nov. 22, 2018, 12:54 p.m. UTC | #2
Hi Ludovic,

Almost done with current patch series and only one question.

ludo@gnu.org (Ludovic Courtès) writes:

> Oleg Pykhalov <go.wigust@gmail.com> skribis:
>
>> * guix/channels.scm (channel->json): New procedure.
>> * guix/scripts/describe.scm (%options): Add 'json' option.
>> (display-checkout-info, display-profile-info): Use 'channel->json'.
>> * doc/guix.texi (Invoking guix describe): Document this.
>
> [...]
>
>> +(define channel->json
>> +  (match-lambda
>> +    (($ <channel> name url branch commit location)
>> +     (scm->json-string
>> +      (list
>> +       (cons 'name name)
>> +       (cons 'url url)
>> +       (cons 'commit commit))))))
>
> Since this is not a core feature of channels and add a dependency on
> (json), what about moving this procedure to (guix scripts describe)?

<channel> should be exported in (guix channels) in this case.  Is it OK?

[…]

Oleg.
Ludovic Courtès Nov. 22, 2018, 4:53 p.m. UTC | #3
Hi Oleg,

Oleg Pykhalov <go.wigust@gmail.com> skribis:

> Almost done with current patch series and only one question.
>
> ludo@gnu.org (Ludovic Courtès) writes:
>
>> Oleg Pykhalov <go.wigust@gmail.com> skribis:
>>
>>> * guix/channels.scm (channel->json): New procedure.
>>> * guix/scripts/describe.scm (%options): Add 'json' option.
>>> (display-checkout-info, display-profile-info): Use 'channel->json'.
>>> * doc/guix.texi (Invoking guix describe): Document this.
>>
>> [...]
>>
>>> +(define channel->json
>>> +  (match-lambda
>>> +    (($ <channel> name url branch commit location)
>>> +     (scm->json-string
>>> +      (list
>>> +       (cons 'name name)
>>> +       (cons 'url url)
>>> +       (cons 'commit commit))))))
>>
>> Since this is not a core feature of channels and add a dependency on
>> (json), what about moving this procedure to (guix scripts describe)?
>
> <channel> should be exported in (guix channels) in this case.  Is it OK?

No I think we should avoid exporting <channel>, but anyway we can write
this procedure as:

  (lambda (channel)
    (scm->json-string `((name . ,(channel-name channel)) …)))

Ludo’.
Oleg Pykhalov Nov. 22, 2018, 6:23 p.m. UTC | #4
ludo@gnu.org (Ludovic Courtès) writes:

[…]

>> <channel> should be exported in (guix channels) in this case.  Is it OK?
>
> No I think we should avoid exporting <channel>, but anyway we can write
> this procedure as:
>
>   (lambda (channel)
>     (scm->json-string `((name . ,(channel-name channel)) …)))

Oh, sure.  Thank you for a reminder.

Pushed as:
--8<---------------cut here---------------start------------->8---
85e9c4b91990008f2b6b07c5de6f14427d7c3a06 describe: Add recutils format.
81a40ee0cb925bc39e3044bddcfdd38ddb04f04d describe: Add json format.
8548f995494d8d6358e6a8d7bc3b3bb5a0cbecb5 describe: Use a procedure to format output.
--8<---------------cut here---------------end--------------->8---

Thanks,
Oleg.

Patch

diff --git a/doc/guix.texi b/doc/guix.texi
index 082e81bf7..3413eb30f 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -3273,7 +3273,10 @@  produce human-readable output;
 @item channels
 produce a list of channel specifications that can be passed to @command{guix
 pull -C} or installed as @file{~/.config/guix/channels.scm} (@pxref{Invoking
-guix pull}).
+guix pull});
+@item json
+@cindex JSON
+produce a list of channel specifications in JSON format.
 @end table
 
 @item --profile=@var{profile}
diff --git a/guix/channels.scm b/guix/channels.scm
index bfdbf470b..9f2c9103e 100644
--- a/guix/channels.scm
+++ b/guix/channels.scm
@@ -26,6 +26,7 @@ 
   #:use-module (guix derivations)
   #:use-module (guix store)
   #:use-module (guix i18n)
+  #:use-module (json)
   #:use-module (srfi srfi-1)
   #:use-module (srfi srfi-9)
   #:use-module (srfi srfi-11)
@@ -39,7 +40,9 @@ 
             channel-commit
             channel-location
 
+            channel->json
             channel->sexp
+
             %default-channels
 
             channel-instance?
@@ -94,6 +97,15 @@ 
        (url ,url)
        (commit ,commit)))))
 
+(define channel->json
+  (match-lambda
+    (($ <channel> name url branch commit location)
+     (scm->json-string
+      (list
+       (cons 'name name)
+       (cons 'url url)
+       (cons 'commit commit))))))
+
 (define-record-type <channel-instance>
   (channel-instance channel commit checkout)
   channel-instance?
diff --git a/guix/scripts/describe.scm b/guix/scripts/describe.scm
index 6a30d19b1..d7e276769 100644
--- a/guix/scripts/describe.scm
+++ b/guix/scripts/describe.scm
@@ -39,7 +39,7 @@ 
   ;; Specifications of the command-line options.
   (list (option '(#\f "format") #t #f
                 (lambda (opt name arg result)
-                  (unless (member arg '("human" "channels"))
+                  (unless (member arg '("human" "channels" "json"))
                     (leave (G_ "~a: unsupported output format~%") arg))
                   (alist-cons 'format (string->symbol arg) result)))
         (option '(#\p "profile") #t #f
@@ -107,7 +107,12 @@  within a Git checkout."
       ('channels
        (pretty-print (channel->sexp (channel (name 'guix)
                                              (url (dirname directory))
-                                             (commit commit))))))
+                                             (commit commit)))))
+      ('json
+       (display (channel->json (channel (name 'guix)
+                                        (url (dirname directory))
+                                        (commit commit))))
+       (newline)))
     (display-package-search-path fmt)))
 
 (define (display-profile-info profile fmt)
@@ -144,7 +149,9 @@  in the format specified by FMT."
     ('human
      (display-profile-content profile number))
     ('channels
-     (pretty-print (map channel->sexp (channels)))))
+     (pretty-print (map channel->sexp (channels))))
+    ('json
+     (format #t "[~a]~%" (string-join (map channel->json (channels)) ","))))
   (display-package-search-path fmt))