Mainly making the code more elegant. Actual problems not solved.
This commit is contained in:
parent
bc68d186cd
commit
c3004ea93a
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -14,3 +14,5 @@ pom.xml.asc
|
||||||
.hg/
|
.hg/
|
||||||
.clj-kondo/
|
.clj-kondo/
|
||||||
.lsp/
|
.lsp/
|
||||||
|
|
||||||
|
docs/
|
||||||
|
|
15
project.clj
15
project.clj
|
@ -1,7 +1,12 @@
|
||||||
(defproject real-name "0.1.0-SNAPSHOT"
|
(defproject org.clojars.simon_brooke/real-name "0.1.0-SNAPSHOT"
|
||||||
:description "FIXME: write description"
|
:codox {:metadata {:doc "**TODO**: write docs"
|
||||||
:url "http://example.com/FIXME"
|
:doc/format :markdown}
|
||||||
|
:output-path "docs"
|
||||||
|
:source-uri "https://github.com/simon-brooke/real-name/blob/master/{filepath}#L{line}"}
|
||||||
|
:dependencies [[org.clojure/clojure "1.10.3"]]
|
||||||
|
:description "Resolve real names from user names in a platform independent fashion."
|
||||||
:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
|
:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
|
||||||
:url "https://www.eclipse.org/legal/epl-2.0/"}
|
:url "https://www.eclipse.org/legal/epl-2.0/"}
|
||||||
:dependencies [[org.clojure/clojure "1.10.3"]]
|
:plugins [[lein-codox "0.10.7"]]
|
||||||
:repl-options {:init-ns cc.journeyman.real-name.core})
|
:repl-options {:init-ns cc.journeyman.real-name.core}
|
||||||
|
:url "https://github.com/simon-brooke/real-name/")
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
"Resolve real names from user names in a platform independent fashion."
|
"Resolve real names from user names in a platform independent fashion."
|
||||||
(:require [clojure.java.io :refer [reader]]
|
(:require [clojure.java.io :refer [reader]]
|
||||||
[clojure.java.shell :refer [sh]]
|
[clojure.java.shell :refer [sh]]
|
||||||
[clojure.string :refer [split trim]])
|
[clojure.string :refer [split starts-with? trim]])
|
||||||
(:gen-class))
|
(:gen-class))
|
||||||
|
|
||||||
(defn- mac-os-x
|
(defn- mac-os-x
|
||||||
|
@ -20,22 +20,42 @@
|
||||||
:os-name (System/getProperty "os.name")
|
:os-name (System/getProperty "os.name")
|
||||||
:os-version (System/getProperty "os.version")})))))
|
:os-version (System/getProperty "os.version")})))))
|
||||||
|
|
||||||
|
(defn delimited-record->map
|
||||||
|
"Split this `record` into fields delimited by this `delimiter-pattern` and
|
||||||
|
return it as a map with these `keys`."
|
||||||
|
[^String record ^java.util.regex.Pattern delimiter-pattern keys]
|
||||||
|
(apply assoc
|
||||||
|
(cons {} (interleave keys (split record delimiter-pattern)))))
|
||||||
|
|
||||||
|
(defn process-gecos
|
||||||
|
"Process this `gecos` field into a map of its sub-fields. See
|
||||||
|
https://en.wikipedia.org/wiki/Gecos_field"
|
||||||
|
[gecos]
|
||||||
|
(delimited-record->map gecos #"," [:real-name :address :work-phone :home-phone :other]))
|
||||||
|
|
||||||
|
(defn process-passwd-line
|
||||||
|
"Process this `line` from a passwd file"
|
||||||
|
[line]
|
||||||
|
(let [record (delimited-record->map line #":" [:uname :pass :uid :gid :gecos :sh])]
|
||||||
|
(when record (assoc record :gecos (process-gecos (:gecos record))))))
|
||||||
|
|
||||||
|
(defn process-passwd
|
||||||
|
"Process the password file into a map whose keys are user names and whose
|
||||||
|
values are maps of associated records from lines in the file."
|
||||||
|
[]
|
||||||
|
(reduce #(assoc %1 (:uname %2) %2)
|
||||||
|
{}
|
||||||
|
(map process-passwd-line
|
||||||
|
(remove #(starts-with? % "#")(line-seq (reader "/etc/passwd"))))))
|
||||||
|
|
||||||
(defn- unix
|
(defn- unix
|
||||||
"Generic unix, parse the GECOS field of the passwd record matching `username`."
|
"Generic unix, parse the GECOS field of the passwd record matching `username`."
|
||||||
([username]
|
([username]
|
||||||
(unix username 4))
|
(let [real-name (-> ((process-passwd) username) :gecos :real-name)]
|
||||||
([username gecos-field]
|
(if real-name
|
||||||
(let [passwd (map #(split % #":") (line-seq (reader "/etc/passwd")))
|
real-name
|
||||||
userrecord (first (filter #(= (first %) username) passwd))
|
(throw (ex-info (format "Real name for `%s` not found." username)
|
||||||
gecos (when userrecord (split (nth userrecord gecos-field) #","))]
|
|
||||||
(if gecos (trim (first gecos))
|
|
||||||
(throw (ex-info (format "Real name for `%s` not found" username)
|
|
||||||
{:username username
|
{:username username
|
||||||
:known-users (map
|
|
||||||
first
|
|
||||||
(filter
|
|
||||||
#(> (read-string (nth % 2)) 999)
|
|
||||||
(filter #(= (count %) 7) passwd)))
|
|
||||||
:os-name (System/getProperty "os.name")
|
:os-name (System/getProperty "os.name")
|
||||||
:os-version (System/getProperty "os.version")}))))))
|
:os-version (System/getProperty "os.version")}))))))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue