summaryrefslogtreecommitdiff
path: root/neocities/cli.scm
blob: c71991560f87b20e54577b737785330038606e3f (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
(define-module (neocities cli)
  #:use-module (neocities api)
  #:use-module (srfi srfi-11)
  #:use-module (ice-9 format)
  #:export (
    neocities-run
))

(define %user (getenv "NEOCITIES_USER"))
(define %pass (getenv "NEOCITIES_PASS"))
(define %key  (getenv "NEOCITIES_KEY"))
(define %host (or (getenv "NEOCITIES_HOST") "neocities.org"))

(define %auth #f)

(define (make-auth)
  (cond
    (%key (make-neocities-auth-api-key %key))
    ((and %user %pass) (make-neocities-auth-basic %user %pass))
    (else (begin
            (format (current-error-port) "ERROR: Missing authentication method~&")
            (exit 1)))))

;; TODO MAKE MACRO: if-neocities-success?
;(define-syntax (syntax-rules))

(define (neocities-cmd-list args)
  (when (< 1 (length args))
    (format (current-error-port) "USAGE: neocities list [DIRECTORY]~&")
    (exit 1))
  (let-values (((response body) (neocities-list (make-neocities-api %host (make-auth))
                                                (and (not (null? args))
                                                     (car args)))))
    (if (neocities-success? body)
        (format #t
                "Updated at~32tDir~36tFilename~86tSize~91tSHA-1~&~:{~A~33t~@[d~]~36t~A~84t~@[~6d~]~91t~@[~A~]~&~}"
                (map
                  (lambda (file) (list
                                   (assoc-ref file "updated_at")
                                   (assoc-ref file "is_directory")
                                   (assoc-ref file "path")
                                   (assoc-ref file "size")
                                   (assoc-ref file "sha1_hash")))
                  (vector->list (assoc-ref body "files"))))
      (format (current-error-port) "~A~&" (assoc-ref body "message")))))

(define (neocities-cmd-key args)
  (when (not (= 0 (length args)))
    (format (current-error-port) "USAGE: neocities key~&")
    (exit 1))
  (let-values (((response body) (neocities-key (make-neocities-api %host (make-auth)))))
    (if (neocities-success? body)
      (format #t "~A~&" (assoc-ref body "api_key"))
      (format (current-error-port) "~A~&" (assoc-ref body "message")))))

(define (neocities-cmd-info args)
  (when (not (= 0 (length args)))
    (format (current-error-port) "USAGE: neocities info~&")
    (exit 1))
  (let-values (((response body) (neocities-info (make-neocities-api %host (make-auth)))))
    (if (neocities-success? body)
      (format #t "~:{~a: ~a~&~}"
        (map (lambda (el) (list (car el) (cdr el)))
             (let ((info (assoc-ref body "info")))
               (assoc-set! info "tags"
                 (string-join (vector->list (assoc-ref info "tags")) ", ")))))
      (format (current-error-port) "~A~&" (assoc-ref body "message")))))

(define (neocities-cmd-delete args)
  (when (= 0 (length args))
    (format (current-error-port) "USAGE: neocities delete FILE [...]~&")
    (exit 1))
  (let-values (((response body) (neocities-delete (make-neocities-api %host (make-auth))
                                                  args)))
    (if (neocities-success? body)
      (format #t "~A~&" (assoc-ref body "message"))
      (format (current-error-port) "~A~&" (assoc-ref body "message")))))

(define (neocities-cmd-upload args)
  (define (pair-args args)
    (map (lambda (i)
           (cons (list-ref args (* 2 i)) (list-ref args (+ 1 (* 2 i)))))
         (iota (/ (length args) 2))))

  (when (or (> 1 (length args)) (not (= 0 (modulo (length args) 2))))
    (format (current-error-port) "USAGE: neocities upload LOCAL_FILE DESTINATION [...]~&")
    (exit 1))
  (let-values (((response body) (neocities-upload (make-neocities-api %host (make-auth))
                                                  (pair-args args))))
    (if (neocities-success? body)
      (format #t "~A~&" (assoc-ref body "message"))
      (format (current-error-port) "~A~&" (assoc-ref body "message")))))

(define (command-not-known command)
  (format (current-error-port) "Command not known ~A~&" command)
  (exit 1))

;; TODO: generate docs automatically from here?
(define neocities-commands
  `((list   . ,neocities-cmd-list)
    (upload . ,neocities-cmd-upload)
    (delete . ,neocities-cmd-delete)
    (info   . ,neocities-cmd-info)
    (key    . ,neocities-cmd-key)))

(define (neocities-run command args)
  ((or (assoc-ref neocities-commands command)
       (command-not-known command))
   args))