Skip to content

Commit

Permalink
Merge pull request #3 from AlexWheeler/add-custom-sftp-capability
Browse files Browse the repository at this point in the history
add jsch/ssh for sftp capabilities
  • Loading branch information
AlexWheeler authored Nov 14, 2016
2 parents 6d56f09 + 2350b48 commit f0f93c5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 16 deletions.
1 change: 1 addition & 0 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
[org.clojure/java.jdbc "0.6.2-alpha3"]
[com.microsoft/sqljdbc4 "3.0"]
[clj-ssh "0.5.14"]
[com.jcraft/jsch "0.1.53"]
]
:main airlift.core
:aot [airlift.core]
Expand Down
18 changes: 18 additions & 0 deletions src/airlift/channel.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(ns airlift.channel
(:require [airlift.sftp :as session])
(:import
[com.jcraft.jsch JSch]
[java.io FileInputStream
File]))

(defn channel [creds]
(let [chan (.openChannel (session/session creds) "sftp")]
(.connect chan)
chan))

(defn upload [channel file_path]
(let [file (File. file_path)]
(.setWritable file true)
(let [stream (FileInputStream. file)]
(.put channel stream file_path)
(.close stream))))
24 changes: 18 additions & 6 deletions src/airlift/core.clj
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
(ns airlift.core
(:gen-class)
(:require [airlift.db :as db]))
(:require [airlift.db :as db]
[airlift.channel :as ch]
[airlift.csv :as csv]))

(defn export-tables
[]
(let [config (read-string (slurp "config.edn"))]
(println config)))

(def creds {:host "" :username "" :password ""})

;;(defn -main [& args]
;; (let [chan (ch/channel creds)]
;; (ch/upload chan "hello.txt")))

(defn -main [& args]
(let [conn (db/init "table_name" "username" "password")]
(let [res (db/query conn "SELECT * FROM TABLE")]
(let [columns (db/columns res)]
(while (.next res)
(println (map #(.getString res %) columns)))))))
(let [conn (db/init "" "" "")
chan (ch/channel creds)]
(doseq [table []]
(let [res (db/query conn (str "SELECT * FROM " table))]
(let [columns (db/columns res) data (atom [])]
(while (.next res)
(swap! data conj (map #(.getString res %) columns)))
(csv/write @data (str table ".csv"))
(ch/upload chan (str table ".csv")))))))
21 changes: 11 additions & 10 deletions src/airlift/sftp.clj
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
(ns airlift.sftp
(:require
[clj-ssh.ssh :as ssh]))
[clj-ssh.ssh :as ssh])
(:import
[com.jcraft.jsch JSch]))

(defn init-session [{:keys [username password host]}]
"returns session given map with keys username, password, host"
(ssh/session (ssh/ssh-agent {}) host {:username username :password password :strict-host-key-checking :no}))
(defn config []
(doto (java.util.Properties.)
(.put "StrictHostKeyChecking" "no")))

(defn put [session in-file out-file]
"Given session, in-file path and out-file path uploads file"
(ssh/with-connection session
(let [channel (ssh/ssh-sftp session)]
(ssh/with-channel-connection channel
(ssh/sftp channel {} :put in-file out-file)))))
(defn session [{:keys [host username password]}]
(doto (.getSession (JSch.) username host 22)
(.setPassword password)
(.setConfig (config))
(.connect)))

0 comments on commit f0f93c5

Please sign in to comment.