clojure-control icon indicating copy to clipboard operation
clojure-control copied to clipboard

`ssh` execute command in path

Open nha opened this issue 10 years ago • 1 comments

I guess my post is more a question than a real issue, but here it is (worst case is, you get to see how a user of your lib for a few hours try to do) : I am trying to define a task to get the git branch of a repository.

Here is what I have so far :

(deftask :git-branch []
  (ssh (run
         (cd (:path @config))
         (run "pwd") ;; this println nicely /var/dev/my-repo => ok
         (run "ls -a") ;; this lists the right content                => ok
         (run "git rev-parse --abbrev-ref HEAD") ;; master => ok

         ;; strangely this is listed before
         ;; and it fails with :
         ;; stderr  fatal: Not a git repository (or any of the parent directories): .git
         (let [rt (ssh "git rev-parse --abbrev-ref HEAD")]
           (println "status " (:status rt))
           (println "stdout " (:stdout rt))
           (println "stderr " (:stderr rt)))

         ;; I am in my home repository
         ;; how can use `ssh` to get a result
         ;; while being in a repository ?
         (let [rt (ssh "pwd")]
             (println "status " (:status rt))
             (println "stdout " (:stdout rt))
             (println "stderr " (:stderr rt))))))

I am confused by the rules of the paths. I expected all calls inside cd to have the same path. Also, how can I get the result of "git rev-parse --abbrev-ref HEAD" in the right folder ?

nha avatar Aug 31 '15 14:08 nha

Alright I got it working, but I'm not sure why I had to nest those ssh and run macros. Also, I expected the tasks to be able to return values like normal functions, and that's not what I get.

(deftask :git-status []
  (ssh (run
         (let [st (ssh (cd (:path @config)
                           (run "git status")))]
           (println "status " (:status st))
           (println "stdout " (:stdout st))
           (println "stderr " (:stderr st))
           st  ;; this doesn't work
           ))))

The last line, was to return a value (in this case : {:stdout "master\n", :stderr "", :status 0}). The result I get instead :

  (println "RETURNS " (call :git-branch))
  ;; RETURNS  {:stdout , :stderr zsh:1: command not found: :branch
  ;; , :status 127}

For now I am using an atom to store the results, but this is less than ideal.

nha avatar Sep 01 '15 09:09 nha