-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.rkt
74 lines (65 loc) · 2.63 KB
/
utils.rkt
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
#lang racket
;; utils.rkt - Utility functions.
;; Ask and I shall provide
(provide
(all-defined-out))
;; —————————————————————————————————
;; import and implementation section
(define (create-bogu-files)
(define path (build-path (find-system-path 'home-dir) ".bogu" "boguignore"))
(unless (file-exists? path)
(make-directory* (path-only path))
(call-with-output-file path void)))
(define (expand-home-path path)
(if (string-prefix? path "~")
(string-replace path "~" (path->string (find-system-path 'home-dir)))
path))
;; Check if we have gcp service credentials file
(define (gcp-service-credentials-file? file-path)
(define file-contents (call-with-input-file file-path port->string))
(cond
[(and
(string-contains? file-contents "type")
(string-contains? file-contents "project_id")
(string-contains? file-contents "private_key_id")
(string-contains? file-contents "private_key")
(string-contains? file-contents "client_email")
(string-contains? file-contents "client_id")
(string-contains? file-contents "auth_uri")
(string-contains? file-contents "token_uri")
(string-contains? file-contents "auth_provider_x509_cert_url")
(string-contains? file-contents "client_x509_cert_url")
(string-contains? file-contents "universe_domain")) #t]
[else #f]))
;; Check if we have gcp oauth file
(define (gcp-oauth-file? file-path)
(define file-contents (call-with-input-file file-path port->string))
(cond
[(and
(string-contains? file-contents "project_id")
(string-contains? file-contents "auth_uri")
(string-contains? file-contents "token_uri")
(string-contains? file-contents "auth_provider_x509_cert_url")
(string-contains? file-contents "client_secret")
(string-contains? file-contents "client_id")) #t]
[else #f]))
;; Checks Shannon entropy of a string
(define (shannon str)
(define len (string-length str))
(define freq (make-hash))
(for ([i (in-range len)])
(hash-update! freq (string-ref str i) add1 0))
(define entropy 0.0)
(for ([count (in-hash-values freq)])
(define p (/ count len))
(define term (* p (log p 2)))
(set! entropy (+ entropy term)))
(set! entropy (- 0 entropy))
entropy)
(define (symbolic-link)
(cond [(file-exists? "/usr/local/bin/bogu")
(delete-file "/usr/local/bin/bogu")])
(define pwd (path->string (current-directory)))
(define destination "/usr/local/bin/bogu")
(make-file-or-directory-link (string-append pwd "bogu") destination)
(displayln "Symbolic link created in /usr/local/bin"))