Fork me on GitHub
#tools-deps
<
2020-03-03
>
geraldodev12:03:59

how we can programatically get the list of loaded aliases ?

vlaaad12:03:50

(.getAliases *ns*)

vlaaad12:03:34

oh, sorry, this is tools-deps, I thought you are asking about namespaces

borkdude12:03:26

This would be the public API for that I think (I know, this isn't about tools.deps):

user=> (require '[clojure.string :as str])
nil
user=> (ns-aliases *ns*)
{str #object[clojure.lang.Namespace 0x301d8120 "clojure.string"]}

vlaaad12:03:08

ah, right, forgot about that one 😄

vlaaad12:03:53

back to original question @U0516053R :

prova@99-65642 ~/Downloads> clj -A:prof
Clojure 1.10.1
user=> (require '[clojure.java.shell :refer (sh)])
user=> (sh "ps" "-p" (-> (java.lang.ProcessHandle/current) .parent .get .pid str))
{:exit 0, :out "  PID TTY           TIME CMD\n70965 ttys003    0:00.01 rlwrap -r -q \\\" -b (){}[],^%#@\";:' clojure -A:prof\n", :err ""}

vlaaad12:03:38

I don’t think there is a good and reliable way to figure it out, because clj launches a new process with classpath, so no alias information is really available to the jvm

vlaaad12:03:57

with that said, you can lookup your PID, lookup parent PID, run ps (linux and mac only) to find information about parent PID and extract some info from that: as you see, :out contains invocation string

vlaaad12:03:43

note: ProcessHandle is available since JDK 9, previous JDKs will require some reflection and googling to get pids..

Alex Miller (Clojure team)13:03:03

Can you back up to what you are trying to do?

geraldodev17:03:46

I'd like to take code action based on the existence of dev alias when connecting to database.

borkdude17:03:30

maybe use an environment variable for that?

Alex Miller (Clojure team)17:03:11

yeah, I would add something extra that is explicit for that case, don't rely on all that process stuff above

Alex Miller (Clojure team)17:03:36

system property, include dev resources in classpath, etc

dominicm18:03:58

I use system props because they go into jvm-opts and they compose