Rewrote nicethings

* Added an implicit `init` procedure instead of doing it automatically
* Removed the repair procedures and replaced them with warnings and help messages if permissions are wrong or files are missing
* Simplified 'condition' checking, which just checks the permissions and existence of files or directories with the name `.nicethings`
cleanup
m455 2020-07-15 11:52:32 -04:00
parent 465f622eaa
commit 67978c4915
1 changed files with 219 additions and 182 deletions

View File

@ -6,165 +6,179 @@
racket/string) racket/string)
;; ------------------------------------------------ ;; ------------------------------------------------
;; values ;; Constants
;; ------------------------------------------------ ;; ------------------------------------------------
(define help-command "help") (define help-command-1 "help")
(define help-command-2 "--help")
(define help-command-3 "-h")
(define init-command "init")
(define ls-command "ls") (define ls-command "ls")
(define rm-command "rm") (define rm-command "rm")
(define add-command "add") (define add-command "add")
(define program-name "nicethings") (define program-name "nicethings")
(define program-name-dotted (string-append "." program-name)) (define program-file (string-append "." program-name))
;; /home/username/ (define program-path (build-path (find-system-path 'home-dir) program-file))
(define home-directory-path (find-system-path 'home-dir)) (define program-permissions 420) ;; 644/-rw-r--r-- permissions
(define program-path (build-path home-directory-path program-name-dotted)) (define newline "\n")
(define correct-permissions 420) ;; -rw-r--r-- permissions (define double-newline "\n\n")
(define message-prefix "> ")
;; ------------------------------------------------ ;; ------------------------------------------------
;; messages ;; Messages
;; ------------------------------------------------ ;; ------------------------------------------------
(define messages (define messages
(hash (hash
'error-add (list (format "Error: No arguments were found after the '~a' command." add-command) 'error-too-many-arguments
(format "Suggestion: Try using a quoted argument after the '~a' command." ls-command) (format (string-append "Error: Too many arguments." newline
(format "Example: ~a add \"Go for a walk\"" program-name)) "Try running '~a ~a' for more information.")
program-name
help-command-1)
'item-not-found (list "Error: Item not found." 'error-incorrect-usage
(format "Suggestion: Try using the '~a' command to see which number " ls-command) (format (string-append "Error: Incorrect usage." newline
" correlates to which message in your list." "Try running '~a ~a' for more information.")
(format "Example: ~a ~a" program-name ls-command)) program-name
help-command-1)
'error-rm (list (format "Error: No arguments were found after the '~a' command." rm-command) 'error-file-already-exists
(format "Suggestion: Try using a number after the '~a' command." rm-command) (format "Error: The file '~a' already exists." program-path)
(format "Example: ~a ~a 2" program-name rm-command)
""
(format "Note: You may need to run the '~a' command to see which " ls-command)
" number correlates to which item in your list."
(format "Example: ~a ~a" program-name ls-command))
'error-usage (list "Error: Incorrect usage." 'error-fake-file-exists
(format "Suggestions: Try running the '~a' command." help-command) (format (string-append "Error: The directory '~a' exists." newline
(format "Example: ~a ~a" program-name help-command)) "Try moving, renaming, or deleting this directory.")
program-path)
'error-ls (list (format "Error: Found arguments after the '~a' command." ls-command) 'error-file-doesnt-exist
(format "Suggestions: Try using '~a' without any arguments." ls-command) (format (string-append "Error: '~a' doesn't exist." newline
(format "Example: ~a ~a" program-name ls-command)) "Try running '~a ~a'.")
program-path
program-name
init-command)
'error-fake-file (list (format "Error: A '~a' directory was found in your home directory." program-name-dotted) 'error-item-not-found
(format "Suggestion 1: Move the '~a' directory out of your " program-name-dotted) "Error: Item not found."
(format " home directory before using ~a." program-name)
(format "Suggestion 2: Rename the '~a' directory before using ~a." program-name-dotted program-name))
'error-permissions-prompt (list (format "Error: '~a''s permissions are incorrect." program-path)
"Would you like to fix them? [y/n]")
'cancel-creation (list (format "Cancelled the creation of ~a." program-path)) 'warning-permissions
(format (string-append "Warning: The permissions on your '~a' file are incorrect." newline
"Other users on this host may be able to read your file." newline
"Try running 'chmod 600 ~a' to fix this." newline)
program-path
program-path)
'permissions-fixed (list (format "'~a''s permissions were successfully fixed." program-path)) 'init-cancelled
(format "Cancelled the creation of '~a'." program-path)
'file-created (list (format "'~a' was successfully created." program-path)) 'init-prompt
(format (string-append "~a will create '~a'." newline
"Is this okay? [y/n]")
program-name
program-path)
'not-found-prompt (list (format "'~a' wasn't found." program-path) 'file-created
"Would you like to create it? [y/n]") (format "Successfully created ~a." program-path)
'error-option (list "Error: '~a' is not an option.") 'empty-list
"There is nothing in your list."))
'empty-list (list "There is nothing in your list.") (define formatees
(hash
'error-not-an-option
(string-append "Error: '~a' is not an option." newline
"Please choose 'y' or 'n'.")
'added (list "Added '~a' to your list.") 'error-not-a-number
(string-append "Error: '~a' is not a number.")
'removed (list "Removed '~a' from your list."))) 'added
"Added '~a' to your list."
'removed
"Removed '~a' from your list."))
;; ------------------------------------------------ ;; ------------------------------------------------
;; helpers ;; helpers
;; ------------------------------------------------ ;; ------------------------------------------------
(define (messages-ref key) (define (messages-ref key)
(map (lambda (element) (string-append message-prefix element)) (hash-ref messages key))
(hash-ref messages key)))
(define-syntax-rule (displayln-message-format key string ...) (define (formatees-ref key)
(let ([string-to-format (string-join (messages-ref key) "\n")]) (hash-ref formatees key))
(displayln (format string-to-format string ...))))
(define (displayln-message-list key) (define (displayln-messages-ref key)
(let ([listof-strings (messages-ref key)]) (let ([message (messages-ref key)])
(for ([string listof-strings]) (displayln message)))
(displayln string))))
(define (displayln-for . strings) (define (displayln-formatees-ref key string)
(for ([string strings]) (let* ([formatee (formatees-ref key)]
(displayln string))) [formatee-formatted (format formatee string)])
(displayln formatee-formatted)))
(define (file-has-correct-permissions? file) (define (create-file string)
(equal? correct-permissions (file-or-directory-permissions file 'bits))) (close-output-port
(open-output-file string)))
(define (has-program-permissions? string)
(equal? program-permissions (file-or-directory-permissions string 'bits)))
;; ------------------------------------------------ ;; ------------------------------------------------
;; repair ;; Check conditions
;; ------------------------------------------------ ;; ------------------------------------------------
(define (repair/not-an-option user-input) (define (check-conditions)
(displayln-message-format 'error-option user-input)
(exit))
(define (repair/cancel)
(displayln-message-list 'cancel-creation)
(exit))
(define (repair/fake-file)
(displayln-message-list 'error-fake-file)
(exit))
(define (repair/create-exit-mode key)
(match key
[(or 'ls 'rm) (exit)]
['add 'do-nothing]))
(define (repair/fix-permissions key)
(file-or-directory-permissions program-path correct-permissions)
(displayln-message-list 'permissions-fixed)
(repair/create-exit-mode key))
(define (repair/wrong-permissions key)
(displayln-message-list 'error-permissions-prompt)
(display message-prefix)
(let ([user-input (read-line)])
(case (string->symbol user-input)
['y (repair/fix-permissions key)]
['n (repair/cancel)]
[else (repair/not-an-option user-input)])))
(define (repair/create-file key)
(close-output-port (open-output-file program-path))
(file-or-directory-permissions program-path correct-permissions)
(displayln-message-list 'file-created)
(repair/create-exit-mode key))
(define (repair/not-found key)
(displayln-message-list 'not-found-prompt)
(display message-prefix)
(let ([user-input (read-line)])
(case (string->symbol user-input)
['y (repair/create-file key)]
['n (repair/cancel)]
[else (repair/not-an-option user-input)])))
(define (repair key)
(cond (cond
[(directory-exists? program-path) [(directory-exists? program-path)
(repair/fake-file)] (begin (displayln-messages-ref 'error-fake-file-exists)
(exit))]
[(not (file-exists? program-path)) [(not (file-exists? program-path))
(repair/not-found key)] (begin (displayln-messages-ref 'error-file-doesnt-exist)
(exit))]
[(not (file-has-correct-permissions? program-path)) [(not (has-program-permissions? program-path))
(repair/wrong-permissions key)] (displayln-messages-ref 'warning-permissions)]
[else 'do-nothing])) [else 'do-nothing]))
;; ------------------------------------------------
;; init
;; ------------------------------------------------
(define (init/create-file)
(create-file program-path)
(file-or-directory-permissions program-path
program-permissions)
(displayln-messages-ref 'file-created))
(define (init/cancel)
(displayln-messages-ref 'init-cancelled)
(exit))
(define (init/prompt)
(displayln-messages-ref 'init-prompt)
(display "> ")
(let* ([input (read-line)]
[input-symbol (string->symbol input)])
(case input-symbol
['y (init/create-file)]
['n (init/cancel)]
[else (displayln-formatees-ref 'error-not-an-option input)])))
(define (init)
(cond
[(file-exists? program-path)
(displayln-messages-ref 'error-file-already-exists)]
[(directory-exists? program-path)
(displayln-messages-ref 'error-fake-file-exists)]
[else (init/prompt)]))
;; ------------------------------------------------ ;; ------------------------------------------------
;; ls ;; ls
;; ------------------------------------------------ ;; ------------------------------------------------
(define (ls/display-list listof-items) (define (ls/display-list listof-items)
;; add1 starts the listof-numbers at 1 instead of 0 ;; The add1 in the first binding starts the
;; listof-numbers at 1 instead of 0 to make the
;; list numbering more human-friendly
(let* ([listof-numbers (map add1 (range (length listof-items)))] (let* ([listof-numbers (map add1 (range (length listof-items)))]
[listof-number-strings (map number->string listof-numbers)] [listof-number-strings (map number->string listof-numbers)]
[combine-lists (lambda (a b) (string-append a ". " b))] [combine-lists (lambda (a b) (string-append a ". " b))]
@ -175,84 +189,84 @@
(displayln item)))) (displayln item))))
(define (ls) (define (ls)
(repair 'ls) (check-conditions)
(let ([listof-items (file->lines program-path)]) (let ([listof-items (file->lines program-path)])
(if (null? listof-items) (if (null? listof-items)
(displayln-message-list 'empty-list) (displayln-messages-ref 'empty-list)
(ls/display-list listof-items)))) (ls/display-list listof-items))))
;; ------------------------------------------------ ;; ------------------------------------------------
;; rm ;; rm
;; ------------------------------------------------ ;; ------------------------------------------------
(define (rm/remove-item listof-items item-number-sub1) (define (rm/remove-item listof-items item-number)
(let* ([item-to-remove (list-ref listof-items item-number-sub1)] (let* ([item-to-remove (list-ref listof-items item-number)]
[list-without-item (remove item-to-remove listof-items)]) [list-without-item (remove item-to-remove listof-items)])
(display-lines-to-file list-without-item (display-lines-to-file list-without-item
program-path program-path
#:exists 'truncate) #:exists 'truncate)
(displayln-message-format 'removed item-to-remove))) (displayln-formatees-ref 'removed item-to-remove)))
(define (rm string) (define (rm/process-string string)
(repair 'rm)
(let* ([listof-items (file->lines program-path)] (let* ([listof-items (file->lines program-path)]
;; subtract 1 because the index starts at
;; 0 under the hood, but the numbers presented from 'ls'
;; start at 1.
[item-number (string->number string)] [item-number (string->number string)]
;; Subtract 1 from the user's original number,
;; because we want to convert the number from
;; human numbers (1 2 3) to index numbers
;; (0 1 2)
[item-number-sub1 (sub1 item-number)] [item-number-sub1 (sub1 item-number)]
[list-length (length listof-items)]) [list-length (length listof-items)])
(if (and (not (null? listof-items)) (if (and (not (null? listof-items))
(number? item-number) (number? item-number)
(positive? item-number) (positive? item-number)
;; 1 less than length, because we want to ;; Don't allow numbers that are equal to
;; remove the index number, which is one less ;; or greater than list-length, because
;; than the item the user chose ;; the list index starts at 0
;;
;; Example: ;; Example:
;; We have a list length of 3: ;; Length of (1 2 3) = 3
;; '(1 2 3) ;;
;; `list-ref` in rm/remove-item above ;; To reference the highest number, we
;; uses an index that starts at 0, so ;; use (list-ref (1 2 3) 2)
;; the index of the numbers above are:
;; '(0 1 2)
;; The 2 is the last index number in a
;; list of length 3, which is what we
;; want, because if you try to remove
;; an index larger than 2, such as the
;; list length 3, then that would be
;; an error
(< item-number-sub1 list-length)) (< item-number-sub1 list-length))
(rm/remove-item listof-items item-number-sub1) (rm/remove-item listof-items item-number-sub1)
(displayln-message-list 'item-not-found)))) (displayln-messages-ref 'error-item-not-found))))
(define (rm string)
(check-conditions)
(if (string->number string)
(rm/process-string string)
(displayln-formatees-ref 'error-not-a-number string)))
;; ------------------------------------------------ ;; ------------------------------------------------
;; add ;; add
;; ------------------------------------------------ ;; ------------------------------------------------
;; The string-cleaned and -remade is incase there
;; are multiple newline characters. This ensures
;; there is only one newline character.
(define (add string) (define (add string)
(repair 'add) (check-conditions)
;; The removing and adding of the '\n' is to
;; ensure only one '\n' exists at the end of the
;; item to be added.
(let* ([string-no-newline (string-replace string "\n" "")] (let* ([string-no-newline (string-replace string "\n" "")]
[string-newline (string-append string-no-newline "\n")]) [string-newline (string-append string-no-newline "\n")])
(display-to-file string-newline (display-to-file string-newline
program-path program-path
#:exists 'append) #:exists 'append)
(displayln-message-format 'added string-no-newline))) (displayln-formatees-ref 'added string-no-newline)))
;; ------------------------------------------------ ;; ------------------------------------------------
;; random message ;; random message
;; ------------------------------------------------ ;; ------------------------------------------------
(define (random-message/append-nicethings-file home-directory) (define (random-message/append-program-file home-directory)
(build-path home-directory program-name-dotted)) (build-path home-directory program-file))
(define (random-message) (define (random-message)
(let* ([root (find-system-path 'sys-dir)] ;; / (let* ([root (find-system-path 'sys-dir)] ;; /
[root-home (build-path root "home")] ;; /home [root-home (build-path root "home")] ;; /home
[listof-homes (directory-list root-home #:build? #t)] ;; `#:build? #t` Builds full paths for all items listed in /home/
[paths-to-nicethings (map random-message/append-nicethings-file listof-homes)] [listof-home-directories (directory-list root-home #:build? #t)]
[directories-with-nicethings (filter file-exists? paths-to-nicethings)] [listof-nicethings-paths (map random-message/append-program-file listof-home-directories)]
[directories-with-420 (filter file-has-correct-permissions? directories-with-nicethings)] [directories-with-nicethings (filter file-exists? listof-nicethings-paths)]
[listof-nicethings (apply append (map file->lines directories-with-420))] [directories-with-644 (filter has-program-permissions? directories-with-nicethings)]
[listof-nicethings (apply append (map file->lines directories-with-644))]
[list-length (length listof-nicethings)]) [list-length (length listof-nicethings)])
(when (not (zero? list-length)) (when (not (zero? list-length))
(let* ([random-number (random list-length)] (let* ([random-number (random list-length)]
@ -263,40 +277,63 @@
;; help ;; help
;; ------------------------------------------------ ;; ------------------------------------------------
(define (help) (define (help)
(displayln-for (displayln
"Usage:" (string-append
"Usage:" newline
(format " ~a [<command>] [<args>]" program-name) (format " ~a [<command>] [<args>]" program-name)
"" double-newline
"Commands:"
(format " ~a - Adds an item to your list." add-command) "Commands:" newline
(format " ~a - Prints a numbered list of your items." ls-command) " No command - Displays a random nicething from a random user." newline
(format " ~a - Removes an item from your list." rm-command) (format " ~a - Creates a file in ~a, which allows you to contribute to the town-wide list of nicethings."
"" init-command
"Examples:" program-path) newline
(format " ~a ~a \"You are beautiful\"" program-name add-command) (format " ~a - Adds a nicething to your list." add-command) newline
(format " ~a ~a" program-name ls-command) (format " ~a - Prints a numbered list of the nicethings you've added." ls-command) newline
(format " ~a ~a 2" program-name rm-command))) (format " ~a - Removes a nicething from your list." rm-command)
double-newline
"Examples:" newline
(format " ~a" program-name) newline
(format " ~a ~a" program-name init-command) newline
(format " ~a ~a \"You are wonderful\"" program-name add-command) newline
(format " ~a ~a" program-name ls-command) newline
(format " ~a ~a 2" program-name rm-command))))
(define (process-args vectorof-args) (define (process-args vectorof-args)
(define (args-ref number) (define (args-ref number)
(vector-ref vectorof-args number)) (vector-ref vectorof-args number))
(match vectorof-args (match vectorof-args
;; Proper usage [(vector a)
[(or '#("-h") (cond
'#("--help") [(ormap (lambda (x) (equal? a x))
'#("help")) (help)] (list help-command-1
[(vector "add" _) (add (args-ref 1))] help-command-2
[(vector "rm" _) (rm (args-ref 1))] help-command-3))
[(vector "ls") (ls)] (help)]
[(vector) (random-message)]
;; Improper usage [(equal? a ls-command)
;; This is checked so we can give the user hints on how to (ls)]
;; use the software if they have part of the command
;; correct [(equal? a init-command)
[(vector "ls" _) (displayln-message-list 'error-ls)] (init)]
[(vector "add") (displayln-message-list 'error-add)]
[(vector "rm") (displayln-message-list 'error-rm)] [else (displayln-messages-ref 'error-incorrect-usage)])]
[(vector _ ...) (displayln-message-list 'error-usage)]))
[(vector a _)
(cond
[(equal? a add-command)
(add (args-ref 1))]
[(equal? a rm-command)
(rm (args-ref 1))]
[else (displayln-messages-ref 'error-incorrect-usage)])]
[(vector _ _ _ ...)
(displayln-messages-ref 'error-too-many-arguments)]
[_ (random-message)]))
(define (main vectorof-args) (define (main vectorof-args)
(process-args vectorof-args)) (process-args vectorof-args))