From 8e72aa3be2c71846346f9131c074daf44a73459a Mon Sep 17 00:00:00 2001 From: Ekaitz Zarraga Date: Sat, 30 Sep 2023 21:12:12 +0200 Subject: neocities: add cli tool: key subcommand works --- Makefile.am | 1 + hall.scm | 1 + neocities/cli.scm | 58 +++++++++++++++++++++++++++++++ scripts/neocities.in | 97 ++++++++++++++++++++++++++++++++++++++-------------- 4 files changed, 132 insertions(+), 25 deletions(-) create mode 100644 neocities/cli.scm diff --git a/Makefile.am b/Makefile.am index 09462cc..8dff344 100644 --- a/Makefile.am +++ b/Makefile.am @@ -34,6 +34,7 @@ SUFFIXES = .scm .go SOURCES = neocities/api.scm \ neocities/requests.scm \ + neocities/cli.scm \ neocities/mime.scm TESTS = tests/requests.scm diff --git a/hall.scm b/hall.scm index 4916347..8be7006 100644 --- a/hall.scm +++ b/hall.scm @@ -18,6 +18,7 @@ "neocities" ((scheme-file "api") (scheme-file "requests") + (scheme-file "cli") (scheme-file "mime"))))) (tests ((directory "tests" ((scheme-file "requests"))))) diff --git a/neocities/cli.scm b/neocities/cli.scm new file mode 100644 index 0000000..706e715 --- /dev/null +++ b/neocities/cli.scm @@ -0,0 +1,58 @@ +(define-module (neocities cli) + #:use-module (neocities api) + #: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 + (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))))) + +(define %api (make-neocities-api %host %auth)) + + +(define (neocities-cmd-list args) + (when (not (= 1 (length args))) + (format (current-error-port) "USAGE: neocities list DIRECTORY~&") + (exit 1)) + (neocities-list %api (car args))) + +(define (neocities-cmd-key args) + (when (not (= 0 (length args))) + (format (current-error-port) "USAGE: neocities key~&") + (exit 1)) + (let ((key-pair (assoc "api_key" (neocities-key %api)))) + (format #t "~a~&" (cdr key-pair)))) + +(define (neocities-cmd-upload args) #f) +(define (neocities-cmd-delete args) #f) +(define (neocities-cmd-info args) #f) + +(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 (lookup-command command) + (let ((value (assoc command neocities-commands))) + (and value (cdr value)))) + +(define (neocities-run command args) + ((or (lookup-command command) (command-not-known command)) args)) diff --git a/scripts/neocities.in b/scripts/neocities.in index f431e13..c2be195 100644 --- a/scripts/neocities.in +++ b/scripts/neocities.in @@ -31,32 +31,79 @@ ;; ;;; Code: -(use-modules (neocities api)) - - -; Commands -; -; AUTH -; export NEOCITIES_USER -; export NEOCITIES_PASS -; or -; export NEOCITIES_KEY -; or -; --prompt -; -; TARGET -; --host HOSTNAME -; -; COMMANDS -; list -d DIRECTORY -; upload LOCAL -to REMOTE LOCAL -to REMOTE ?? -; delete file ... -; info -; key +(use-modules (neocities cli) + (ice-9 match)) + +(define (help) + (format #t " +~A + +USAGE: + + ENVIRONMENT VARIABLES: + Export them: + export NEOCITIES_USER=my_username + ... + Or use them in the command line call + NEOCITIES_USER=my_username ... neocities + + Authentication: + Your credentials + export NEOCITIES_USER=my_username + export NEOCITIES_PASS=my_password + Or your API key: + export NEOCITIES_KEY=my_key + + Target: + export NEOCITIES_HOST # defaults to neocities.org + + USAGE: + neocities --help/--version + neocities COMMAND + + Commands: + neocities list DIRECTORY + neocities upload LOCAL -to REMOTE LOCAL -to REMOTE ?? + neocities delete FILE [...] + neocities info + neocities key +~%" (car (command-line)))) + +(define (version) + (format #t " +~A + +VERSION: @PACKAGE_VERSION@ + + Copyright (C) 2023 Ekaitz Zarraga + + Author: Ekaitz Zarraga + + guile-neocities 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. + + guile-neocities 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. +~%" (car (command-line)))) + +(define (subcommand? arg) + (not (string-prefix? "-" arg))) (define* (main #:optional (args (command-line))) "Entry point for the commandline application. ARGS should be a normal command-line list, e.g. '(\"neocities\" \"upload\" \"...\")." - (display "It runs") - (newline) -) + (match args + ((program (or "--version" "-v")) + (begin (version) (exit 0))) + ((or (program) (program (or "--help" "-h"))) + (begin (help) (exit 1))) + ((program (? subcommand? subcommand) . args*) + (neocities-run (string->symbol subcommand) args*)) + ((program invalid-subcommand . args*) + (begin + (format (current-error-port) "Invalid subcommand: ~a~&" invalid-subcommand ) + (help) (exit 1))))) -- cgit v1.2.3