diff options
author | Anonymized <anonymous@example.com> | 2018-01-11 19:02:01 +0100 |
---|---|---|
committer | Anonymized <anonymous@example.com> | 2018-01-11 19:02:01 +0100 |
commit | d4b8b0c1e12d82174e2cdb299045adab18d3ea4c (patch) | |
tree | baaba92680cfdf144aefa689c2033ea79cea6bc3 | |
parent | 8e3019f5365aa6e2c2162bb4323331c61a3b4f54 (diff) | |
download | gnunet-guile2-d4b8b0c1e12d82174e2cdb299045adab18d3ea4c.tar.gz gnunet-guile2-d4b8b0c1e12d82174e2cdb299045adab18d3ea4c.zip |
add ./gnunet-guile program with download subcommand and getopt-long support
Check the help menu to know how it works.
This is missing integration with autotools.
-rw-r--r-- | Makefile.am | 3 | ||||
-rw-r--r-- | download.scm | 21 | ||||
-rwxr-xr-x | gnunet-guile | 72 | ||||
-rw-r--r-- | gnunet/subcommand.scm | 74 |
4 files changed, 148 insertions, 22 deletions
diff --git a/Makefile.am b/Makefile.am index ef40ab6..a681768 100644 --- a/Makefile.am +++ b/Makefile.am | |||
@@ -22,7 +22,8 @@ SUBDIRS = doc | |||
22 | docdir = $(datadir)/doc/gnunet-guile/ | 22 | docdir = $(datadir)/doc/gnunet-guile/ |
23 | 23 | ||
24 | MODULES = \ | 24 | MODULES = \ |
25 | gnunet.scm | 25 | gnunet.scm \ |
26 | gnunet/subcommand.scm | ||
26 | 27 | ||
27 | GOBJECTS = $(MODULES:%.scm=%.go) gnunet/config.go | 28 | GOBJECTS = $(MODULES:%.scm=%.go) gnunet/config.go |
28 | 29 | ||
diff --git a/download.scm b/download.scm deleted file mode 100644 index 6e136ea..0000000 --- a/download.scm +++ /dev/null | |||
@@ -1,21 +0,0 @@ | |||
1 | (define-module (download)) | ||
2 | |||
3 | (use-modules ((gnunet))) | ||
4 | |||
5 | |||
6 | (define %configuration (configuration-create)) | ||
7 | (configuration-load! %configuration "etc/p2.conf") | ||
8 | |||
9 | (define %fs-handle #f) | ||
10 | (define %fs-download #f) | ||
11 | |||
12 | (define (progress info) | ||
13 | (pk (fs-progress-info-status info))) | ||
14 | |||
15 | (scheduler-run | ||
16 | (lambda () | ||
17 | (set! %fs-handle (fs-start %configuration "guile-gnunet" progress)) | ||
18 | (set! %fs-download (fs-download-start %fs-handle | ||
19 | (string->uri (cadr (program-arguments))) | ||
20 | 0 | ||
21 | #:filename "gnunet.out")))) | ||
diff --git a/gnunet-guile b/gnunet-guile new file mode 100755 index 0000000..750cd04 --- /dev/null +++ b/gnunet-guile | |||
@@ -0,0 +1,72 @@ | |||
1 | #!/bin/sh | ||
2 | # -*- mode: Scheme; indent-tabs-mode: nil; fill-column: 80; -*- | ||
3 | exec guile -e '(@ (gnunet-guile) main)' -s "$0" "$@" | ||
4 | !# | ||
5 | ;;;; Copyright © 2018 Amirouche Boubekki <amirouche@hypermove.net> | ||
6 | ;;;; | ||
7 | ;;;; This program is free software: you can redistribute it and/or modify | ||
8 | ;;;; it under the terms of the GNU General Public License as published by | ||
9 | ;;;; the Free Software Foundation, either version 3 of the License, or | ||
10 | ;;;; (at your option) any later version. | ||
11 | ;;;; | ||
12 | ;;;; This program is distributed in the hope that it will be useful, | ||
13 | ;;;; but 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 this program. If not, see <http://www.gnu.org/licenses/>. | ||
19 | (define-module (gnunet-guile)) | ||
20 | |||
21 | (use-modules ((ice-9 getopt-long))) | ||
22 | (use-modules ((ice-9 match))) | ||
23 | |||
24 | (use-modules ((gnunet))) | ||
25 | (use-modules ((gnunet subcommand))) | ||
26 | |||
27 | |||
28 | ;;; download | ||
29 | |||
30 | (define %fs-handle #f) | ||
31 | (define %fs-download #f) | ||
32 | |||
33 | (define (progress info) | ||
34 | (pk (fs-progress-info-status info))) | ||
35 | |||
36 | (define %download-options | ||
37 | '((help (single-char #\h) (value #f)))) | ||
38 | |||
39 | (define (download args) | ||
40 | "gnunet download CONFIGURATION URI FILENAME | ||
41 | |||
42 | Download the URI to FILENAME using CONFIGURATION. | ||
43 | " | ||
44 | (let ((options (getopt-long (cons "gnunet-guile download" args) | ||
45 | %download-options | ||
46 | #:stop-at-first-non-option #f))) | ||
47 | (if (option-ref options 'help #f) | ||
48 | (display (procedure-documentation download)) | ||
49 | (match args | ||
50 | ((configuration uri filename) | ||
51 | (let ((configuration* (configuration-create))) | ||
52 | (configuration-load! configuration* configuration) | ||
53 | (scheduler-run | ||
54 | (lambda () | ||
55 | (set! %fs-handle (fs-start configuration* "gnunet-guile" progress)) | ||
56 | (fs-download-start %fs-handle | ||
57 | (string->uri uri) | ||
58 | 0 | ||
59 | #:filename filename))))))))) | ||
60 | |||
61 | ;;; publish | ||
62 | |||
63 | (define (publish . args) | ||
64 | (pk 'publish args)) | ||
65 | |||
66 | ;;; cli | ||
67 | |||
68 | (define %cli `((download ,download) | ||
69 | (publish ,publish))) | ||
70 | |||
71 | (define-public (main args) | ||
72 | (subcommand 'gnunet-guile %cli (cdr args))) | ||
diff --git a/gnunet/subcommand.scm b/gnunet/subcommand.scm new file mode 100644 index 0000000..d2dd9b5 --- /dev/null +++ b/gnunet/subcommand.scm | |||
@@ -0,0 +1,74 @@ | |||
1 | ;;;; -*- mode: Scheme; indent-tabs-mode: nil; fill-column: 80; -*- | ||
2 | ;;;; | ||
3 | ;;;; Copyright © 2018 Amirouche Boubekki <amirouche@hypermove.net> | ||
4 | ;;;; | ||
5 | ;;;; This program is free software: you can redistribute it and/or modify | ||
6 | ;;;; it under the terms of the GNU General Public License as published by | ||
7 | ;;;; the Free Software Foundation, either version 3 of the License, or | ||
8 | ;;;; (at your option) any later version. | ||
9 | ;;;; | ||
10 | ;;;; This program is distributed in the hope that it will be useful, | ||
11 | ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | ;;;; GNU General Public License for more details. | ||
14 | ;;;; | ||
15 | ;;;; You should have received a copy of the GNU General Public License | ||
16 | ;;;; along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | ;; | ||
18 | ;; Comment: | ||
19 | ;; | ||
20 | ;; Command line interface subcommands parsing | ||
21 | ;; | ||
22 | (define-module (gnunet subcommand)) | ||
23 | |||
24 | (use-modules ((srfi srfi-1))) | ||
25 | |||
26 | |||
27 | (define (path-display path) | ||
28 | (display " ") | ||
29 | (let loop ((path path)) | ||
30 | (unless (null? path) | ||
31 | (display (car path)) | ||
32 | (display " ") | ||
33 | (loop (cdr path)))) | ||
34 | (display #\newline)) | ||
35 | |||
36 | (define (%spec-help path) | ||
37 | (lambda (spec) | ||
38 | (if (procedure? (cadr spec)) | ||
39 | (path-display (append path (list (car spec)))) | ||
40 | (for-each (%spec-help (append path (list (car spec)))) (cdr spec))))) | ||
41 | |||
42 | (define (spec-help name spec) | ||
43 | "Display the usage message for this SPEC for the program named NAME" | ||
44 | (display "Usage:\n\n") | ||
45 | (for-each (%spec-help (list name)) spec)) | ||
46 | |||
47 | (define (%lookup spec args) | ||
48 | (if (string=? (symbol->string (car spec)) (car args)) | ||
49 | (values (cdr spec) (cdr args)) | ||
50 | (values #f #f))) | ||
51 | |||
52 | (define (lookup spec args) | ||
53 | (let loop ((spec spec)) | ||
54 | (if (null? spec) | ||
55 | (values #f #f) | ||
56 | (call-with-values (lambda () (%lookup (car spec) args)) | ||
57 | (lambda (procedure-or-spec args) | ||
58 | (cond | ||
59 | ((and (not procedure-or-spec) (not args)) | ||
60 | (loop (cdr spec))) | ||
61 | ((procedure? (car procedure-or-spec)) | ||
62 | (values (car procedure-or-spec) args)) | ||
63 | (else (lookup procedure-or-spec args)))))))) | ||
64 | |||
65 | (define-public (subcommand name spec args) | ||
66 | "Execute the subcommand of SPEC based on ARGS for the command NAME" | ||
67 | (if (or (null? args) (equal? args '("--help")) (equal? args '("-h"))) | ||
68 | (spec-help name spec) | ||
69 | (call-with-values (lambda () (lookup spec args)) | ||
70 | (lambda (procedure args) | ||
71 | (cond | ||
72 | ((and procedure args) | ||
73 | (procedure args)) | ||
74 | (else (spec-help name spec))))))) | ||