Finished writing the repair procedures. the second time? i think for real this time?

cleanup
m455 2020-06-15 11:07:48 -04:00
parent da77be104d
commit 17528dd23e
1 changed files with 81 additions and 64 deletions

View File

@ -13,34 +13,37 @@
;; ------------------------------------------------ ;; ------------------------------------------------
(define messages (define messages
(hash (hash
'not-found "> '~a' wasn't found." 'not-found "> '~a' wasn't found."
'repair-prompt "> You will need it to use nicethings.\n> Do you want to create it? [y/n]\n> " 'not-found-prompt "> You will need it to use nicethings.\n> Do you want to create it? [y/n]\n> "
'fake-file-found "> The directory '~a' was found.\n> Please move this file somewhere else before using nicethings." 'wrong-permissions "> '~a''s permissions are incorrect."
'try (list "> For usage help, try running the following command:" 'wrong-permissions-prompt "> You will need the permissions to be fixed before using nicethings.\n> Do you want to fix them? [y/n]\n> "
"nicethings --help") 'fake-file-found "> The directory '~a' was found.\n> Please move this file somewhere else before using nicethings."
'cancel-creation "> Cancelled nicethings file creation." 'try (list "> For usage help, try running the following command:"
'file-created "> '~a' was successfully created." "nicethings --help")
'not-an-option "> Error: '~a' is not an option." 'cancel "> Cancelled."
'add-expected-arg (list "> Error: Found 'add', but no arguments were found." 'file-created "> '~a' was successfully created."
"> The 'add' command expects one quoted argument after it." 'permissions-fixed "> '~a''s permissions were successfully fixed."
"> Example:" 'not-an-option "> Error: '~a' is not an option."
"nicethings add \"You are beautiful\".") 'add-expected-arg (list "> Error: Found 'add', but no arguments were found."
'rm-expected-arg (list "> Error: Found 'rm', but no arguments were found." "> The 'add' command expects one quoted argument after it."
"> The 'rm' command expects one number as an argument after it." "> Example:"
"> Example:" "nicethings add \"You are beautiful\".")
"nicethings rm 2" 'rm-expected-arg (list "> Error: Found 'rm', but no arguments were found."
"> Note: You may need to use the 'ls' command to see which number correlates to which message.") "> The 'rm' command expects one number as an argument after it."
'ls-expected-no-args (list "> Error: Found 'ls', but also found other arguments." "> Example:"
"> The 'ls' command expects no arguments after it." "nicethings rm 2"
"> Example:" "> Note: You may need to use the 'ls' command to see which number correlates to which message.")
"nicethings ls") 'ls-expected-no-args (list "> Error: Found 'ls', but also found other arguments."
;; I don't currently use this message yet: "> The 'ls' command expects no arguments after it."
'rm-expected-number (list "> Error: Found '~a' after 'rm'." "> Example:"
"> The 'rm' command expects one number as an argument after it." "nicethings ls")
"> Example:" ;; I don't currently use this message yet:
"nicethings rm 2" 'rm-expected-number (list "> Error: Found '~a' after 'rm'."
"> Note: You may need to use the 'ls' command to see which number correlates to which message.") "> The 'rm' command expects one number as an argument after it."
'added "> Added '~a' to the list.")) "> Example:"
"nicethings rm 2"
"> Note: You may need to use the 'ls' command to see which number correlates to which message.")
'added "> Added '~a' to the list."))
;; ------------------------------------------------ ;; ------------------------------------------------
;; helpers ;; helpers
@ -54,6 +57,12 @@
(define (messages-ref key) (define (messages-ref key)
(hash-ref messages key)) (hash-ref messages key))
(define (file-has-420-permissions? file)
(equal? 420 (file-or-directory-permissions file 'bits)))
(define (append-nicethings-file home-directory)
(build-path home-directory nicethings-string))
;; ------------------------------------------------ ;; ------------------------------------------------
;; repair ;; repair
;; ------------------------------------------------ ;; ------------------------------------------------
@ -62,30 +71,50 @@
(exit)) (exit))
(define (repair/cancel) (define (repair/cancel)
(displayln (messages-ref 'cancel-creation)) (displayln (messages-ref 'cancel))
(exit)) (exit))
;; Check for a "fake" '.nicethings' file, (define (repair/fix-permissions)
;; which is a directory named '.nicethings' (file-or-directory-permissions nicethings-path-local 420)
(define (repair/start) (displayln-format (messages-ref 'permissions-fixed) nicethings-path-local)
(if (directory-exists? nicethings-path-local) (exit))
(begin (displayln-format (messages-ref 'fake-file-found) nicethings-path-local)
(exit))
(begin (close-output-port (open-output-file nicethings-path-local))
(displayln-format (messages-ref 'file-created) nicethings-path-local))))
(define (repair/prompt) (define (repair/wrong-permissions)
(display (messages-ref 'repair-prompt)) (display (messages-ref 'wrong-permissions-prompt))
(let ([user-input (read-line)]) (let ([user-input (read-line)])
(case (string->symbol user-input) (case (string->symbol user-input)
['y (repair/start)] ['y (repair/fix-permissions)]
['n (repair/cancel)]
[else (repair/not-an-option user-input)])))
(define (repair/create-file)
(close-output-port (open-output-file nicethings-path-local))
(displayln-format (messages-ref 'file-created) nicethings-path-local)
(exit))
(define (repair/not-found)
(display (messages-ref 'not-found-prompt))
(let ([user-input (read-line)])
(case (string->symbol user-input)
['y (repair/create-file)]
['n (repair/cancel)] ['n (repair/cancel)]
[else (repair/not-an-option user-input)]))) [else (repair/not-an-option user-input)])))
(define (repair) (define (repair)
(when (not (file-exists? nicethings-path-local)) (cond
(displayln-format (messages-ref 'not-found) nicethings-path-local) ;; Check for a "fake" '.nicethings' file, which is a directory named '.nicethings'
(repair/prompt))) [(directory-exists? nicethings-path-local)
(begin (displayln-format (messages-ref 'fake-file-found) nicethings-path-local)
(exit))]
;; Check for a missing '.nicethings' file
[(not (file-exists? nicethings-path-local))
(begin (displayln-format (messages-ref 'not-found) nicethings-path-local)
(repair/not-found))]
;; Check for incorrect permissions on '.nicethings' file
[(not (file-has-420-permissions? nicethings-path-local))
(begin (displayln-format (messages-ref 'wrong-permissions) nicethings-path-local)
(repair/wrong-permissions))]
[else 'do-nothing]))
;; ------------------------------------------------ ;; ------------------------------------------------
;; add message ;; add message
@ -106,29 +135,17 @@
;; ------------------------------------------------ ;; ------------------------------------------------
;; random message ;; random message
;; ------------------------------------------------ ;; ------------------------------------------------
;; The +1 includes the last element in the list
(define (random-message) (define (random-message)
(repair) (repair)
;; TODO: Don't use local path here. turn into global (let* ([root (find-system-path 'sys-dir)] ;; /
;; Algo: [root-home (build-path root "home")] ;; /home
;; gather all directories in /home/ -> list [listof-homes (directory-list root-home #:build? #t)] ;; #:build #t builds the full path
;; filter file-exists? /home/directory/.nicethings -> list [paths-to-nicethings (map append-nicethings-file listof-homes)] ;; '("/home/username/.nicethings")
;; NOTES: [directories-with-nicethings (filter file-exists? paths-to-nicethings)]
;; /home [directories-with-420 (filter file-has-420-permissions? directories-with-nicethings)])
;;(define root-home (build-path (find-system-path 'sys-dir) "home")) (for ([i directories-with-420])
;; '(/home/username _ ...) (displayln i))
;;(map path->complete-path (directory-list root-home)) ))
;;;;;;;;;;;;;;
;; NOTES2:
;;(define root-home (build-path (find-system-path 'sys-dir) "home"))
;;(map (lambda (x) (build-path root-home x)) (directory-list root-home))
;;;;;;;;;;;;
(let* ([root-home (build-path (find-system-path 'sys-dir) "home")]
[paths-to-nicethings (map (lambda (home-directory) (build-path root-home home-directory nicethings-string))
(directory-list root-home))])
(filter (lambda (x) (file-exists? x)) paths-to-nicethings)))
;; [listof-paths (map (lambda (path) (filter file-exists? path)) paths-to-nicethings)])
;; (when (not (zero? list-length)) ;; (when (not (zero? list-length))
;; (let* ([random-number (random list-length)] ;; (let* ([random-number (random list-length)]