summaryrefslogtreecommitdiff
path: root/shroud/ui.scm
blob: cbc06830ca48105f7d5dcc5855834c3fb686a053 (plain)
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
;;; Shroud
;;; Copyright © 2015 David Thompson <davet@gnu.org>
;;;
;;; Shroud 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.
;;;
;;; Shroud 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 Shroud.  If not, see <http://www.gnu.org/licenses/>.

(define-module (shroud ui)
  #:use-module (ice-9 format)
  #:use-module (ice-9 match)
  #:use-module (srfi srfi-37)
  #:use-module (shroud config)
  #:use-module (shroud secret)
  #:export (program-name
            show-version-and-exit
            leave
            make-user-module
            load*
            shroud-main))

(define program-name (make-parameter "shroud"))

(define %commands
  '("add" "show" "list-all" "remove" "clear"))

(define (show-help)
  (format #t "Usage: shroud COMMAND ARGS...
Run COMMAND with ARGS.~%~%")
  (format #t "COMMAND may be one of the sub-commands listed below:~%~%")
  (format #t "~{   ~a~%~}" %commands))

(define (show-usage)
  (format #t "Try `shroud --help' for more information.~%")
  (exit 1))

(define (show-version-and-exit)
  (format #t "~a ~a
Copyright (C) 2015 David Thompson <davet@gnu.org>
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.~%"
          (program-name) %shroud-version)
  (exit 0))

(define (leave format-string . args)
  "Display error message and exist."
  (apply format (current-error-port) format-string args)
  (newline)
  (exit 1))

(define (make-user-module modules)
  "Return a new user module with the additional MODULES loaded."
  ;; Module in which the machine description file is loaded.
  (let ((module (make-fresh-user-module)))
    (for-each (lambda (iface)
                (module-use! module (resolve-interface iface)))
              modules)
    module))

(define (report-load-error file args)
  "Report the failure to load FILE, a user-provided Scheme file, and exit.
ARGS is the list of arguments received by the 'throw' handler."
  (match args
    (('system-error . _)
     (let ((err (system-error-errno args)))
       (leave "failed to load '~a': ~a~%" file (strerror err))))
    (('syntax-error proc message properties form . rest)
     (let ((file (assq-ref properties 'filename))
           (line (assq-ref properties 'line))
           (col  (assq-ref properties 'column)))
       (format (current-error-port) "~a:~a:~a: error: ~a~%"
               file (and line (1+ line)) col message))
     (exit 1))
    ((error args ...)
     (format (current-error-port) "failed to load '~a':~%" file)
     (apply display-error #f (current-error-port) args)
     (exit 1))))

(define (load* file user-module)
  "Load the user provided Scheme source code FILE."
  (catch #t
    (lambda ()
      (set! %fresh-auto-compile #t)

      (save-module-excursion
       (lambda ()
         (set-current-module user-module)
         (primitive-load file))))
    (lambda args
      (report-load-error file args))))

(define %default-config
  `((database-file . ,(string-append (getenv "HOME") "/.shroud-db"))
    (gpg-binary    . "gpg")))

(define (load-config)
  "Load and evaluate user configuration file."
  (append (load* (string-append (getenv "HOME") "/.shroud")
                 (make-user-module '((shroud config))))
          %default-config))

(define (command-proc command)
  "Return the procedure for COMMAND."
  (let* ((module
             (catch 'misc-error
               (lambda ()
                 (resolve-interface `(shroud ui ,command)))
               (lambda -
                 (format (current-error-port) "~a: invalid subcommand~%" command)
                 (show-usage)))))
    (module-ref module (symbol-append 'shroud- command))))

(define (option? str)
  "Return #t if STR is an option string."
  (string-prefix? "-" str))

(define (make-program-name command)
  "Return a program name string for COMMAND."
  (string-append "shroud " (symbol->string command)))

(define (shroud-main . args)
  (match args
    (() (show-usage))
    ((or ("-h") ("--help"))
     (show-help))
    (("--version")
     (show-version-and-exit))
    (((? option? opt) _ ...)
     (format (current-error-port) "shroud: unrecognized option '~a'~%" opt))
    (((= string->symbol command) . args)
     (let* ((config  (load-config))
            (db-file (assq-ref config 'database-file))
            (db      (load-secrets db-file))
            (proc    (command-proc command))
            (result  (parameterize ((program-name (make-program-name command)))
                       (apply proc config db args))))
       (unless (eq? db result)
         (save-secrets result db-file))))))