Skip to content

Commit 0727b56

Browse files
committed
Made *ssh-agent* unbound by default and added asserts in order to improve error messages and debugging
1 parent 08b3e8a commit 0727b56

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@
100100
<artifactId>jsch</artifactId>
101101
<version>0.1.42</version>
102102
</dependency>
103+
<dependency>
104+
<groupId>swank-clojure</groupId>
105+
<artifactId>swank-clojure</artifactId>
106+
<version>1.2.1</version>
107+
<optional>true</optional>
108+
</dependency>
103109
</dependencies>
104110
<profiles>
105111
<profile>

src/clj_ssh/ssh.clj

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ More advance usage is possible.
3131
3232
## Installation
3333
34-
Via maven and the clojars (http://clojars.org), or
34+
Via maven and the clojars (http://clojars.org/clj-ssh), or
3535
Leiningen (http://github.com/technomancy/leiningen).
3636
3737
## License
3838
3939
Licensed under EPL (http://www.eclipse.org/legal/epl-v10.html)"
4040
(:use
41-
[clojure.contrib.def :only [defvar]])
41+
[clojure.contrib.def :only [defvar defunbound]])
4242
(:require
4343
clj-ssh.keychain
4444
[clojure.contrib.logging :as logging])
@@ -58,14 +58,26 @@ Licensed under EPL (http://www.eclipse.org/legal/epl-v10.html)"
5858

5959

6060

61-
(defvar *ssh-agent* nil "SSH agent used to manage identities.")
61+
(defunbound *ssh-agent* "SSH agent used to manage identities.")
6262
(defvar *default-session-options* {} "Default SSH options")
6363

64+
6465
(defmacro when-feature
6566
[feature-name & body]
66-
(if (find-var (symbol "clojure.core" (name feature-name)))
67+
(when (find-var (symbol "clojure.core" (name feature-name)))
6768
`(do ~@body)))
6869

70+
(defmacro when-not-feature
71+
[feature-name & body]
72+
(when-not (find-var (symbol "clojure.core" (name feature-name)))
73+
`(do ~@body)))
74+
75+
(when-not-feature
76+
bound?
77+
(defn bound?
78+
[& vars#]
79+
(every? #(.isBound #^clojure.lang.Var %) vars#)))
80+
6981
;; Enable java logging of jsch when in clojure 1.2
7082
(when-feature deftype
7183
(def ssh-log-levels
@@ -152,6 +164,7 @@ Licensed under EPL (http://www.eclipse.org/legal/epl-v10.html)"
152164
(add-identity agent private-key nil)
153165
(add-identity *ssh-agent* agent private-key)))
154166
([#^JSch agent private-key #^String passphrase]
167+
(assert agent)
155168
(.addIdentity
156169
agent
157170
(if (instance? Identity private-key)
@@ -160,6 +173,7 @@ Licensed under EPL (http://www.eclipse.org/legal/epl-v10.html)"
160173
(and passphrase (.getBytes passphrase))))
161174
([#^JSch agent #^String name #^bytes private-key #^bytes public-key
162175
#^bytes passphrase]
176+
(assert agent)
163177
(.addIdentity
164178
agent name private-key public-key passphrase)))
165179

@@ -367,7 +381,7 @@ keys. All other option key pairs will be passed as SSH config options."
367381

368382
(defn default-session [host username port password]
369383
(doto (session-impl
370-
(or *ssh-agent* (create-ssh-agent))
384+
(or (and (bound? #'*ssh-agent*) *ssh-agent*) (create-ssh-agent))
371385
host username port password
372386
*default-session-options*)
373387
(.connect)))

0 commit comments

Comments
 (0)