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

@ -14,12 +14,15 @@
(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> "
'wrong-permissions "> '~a''s permissions are incorrect."
'wrong-permissions-prompt "> You will need the permissions to be fixed before using nicethings.\n> Do you want to fix them? [y/n]\n> "
'fake-file-found "> The directory '~a' was found.\n> Please move this file somewhere else before using nicethings." 'fake-file-found "> The directory '~a' was found.\n> Please move this file somewhere else before using nicethings."
'try (list "> For usage help, try running the following command:" 'try (list "> For usage help, try running the following command:"
"nicethings --help") "nicethings --help")
'cancel-creation "> Cancelled nicethings file creation." 'cancel "> Cancelled."
'file-created "> '~a' was successfully created." 'file-created "> '~a' was successfully created."
'permissions-fixed "> '~a''s permissions were successfully fixed."
'not-an-option "> Error: '~a' is not an option." 'not-an-option "> Error: '~a' is not an option."
'add-expected-arg (list "> Error: Found 'add', but no arguments were found." 'add-expected-arg (list "> Error: Found 'add', but no arguments were found."
"> The 'add' command expects one quoted argument after it." "> The 'add' command expects one quoted argument after it."
@ -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)
(begin (displayln-format (messages-ref 'fake-file-found) nicethings-path-local)
(exit)) (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)]