Skip to content
Browse files

Initial commit; basic apps:info and apps:create working.

  • Loading branch information...
0 parents commit 9ba711dd17a45cb37481e18c8dfac822cb76cacf @technomancy committed Nov 21, 2011
Showing with 121 additions and 0 deletions.
  1. +8 −0 .gitignore
  2. +13 −0 README.md
  3. +8 −0 project.clj
  4. +59 −0 src/leiningen/heroku.clj
  5. +33 −0 src/leiningen/heroku/apps.clj
8 .gitignore
@@ -0,0 +1,8 @@
+/pom.xml
+*jar
+/lib
+/classes
+/native
+/.lein-failures
+/checkouts
+/.lein-deps-sum
13 README.md
@@ -0,0 +1,13 @@
+# lein-heroku
+
+A Leiningen plugin for managing your Heroku apps.
+
+## Usage
+
+FIXME: write
+
+## License
+
+Copyright © 2011 Heroku Inc.
+
+Distributed under the Eclipse Public License, the same as Clojure.
8 project.clj
@@ -0,0 +1,8 @@
+(defproject lein-heroku "1.0.0-SNAPSHOT"
+ :description "Heroku plugin for Leiningen"
+ :dependencies [[lein-newnew "0.1.0"]
+ [com.heroku.api/heroku-api "0.1-SNAPSHOT"]
+ [com.heroku.api/heroku-httpclient "0.1-SNAPSHOT"]
+ [com.heroku.api/heroku-json-gson "0.1-SNAPSHOT"]
+ [clj-http "0.2.4"]]
+ :eval-in-leiningen true)
59 src/leiningen/heroku.clj
@@ -0,0 +1,59 @@
+(ns leiningen.heroku
+ (:require [clojure.java.io :as io]
+ [clj-http.core :as http]
+ [leiningen.help :as help])
+ (:import (com.heroku.api.command CommandConfig)
+ (com.heroku.api.command.login BasicAuthLogin)
+ (com.heroku.api.connection Connection HttpClientConnection)
+ (com.heroku.api HerokuAPI HerokuStack)))
+
+(try ; Leiningen 2.0 compatibility
+ (use '[leiningen.core :only [abort]])
+ (catch Exception _
+ (use '[leiningen.main :only [abort]])))
+
+(defn- read-password []
+ (.readPassword (System/console) "%s" (into-array ["Password: "])))
+
+(defn- credentials-file []
+ (io/file (System/getProperty "user.home") ".heroku" "credentials"))
+
+(defn- get-credentials []
+ (-> (credentials-file)
+ (slurp)
+ (.split "\n")))
+
+(defn login [_ & args]
+ (println "Enter your Heroku credentials.")
+ (print "Email: ") (flush)
+ (let [email (read-line)
+ password (read-password)
+ command (BasicAuthLogin. email password)]
+ ;; TODO: save to credentials-file
+ ))
+
+(defn api []
+ (when-not (.exists (credentials-file))
+ (login))
+ (let [[email key] (get-credentials)]
+ (-> (BasicAuthLogin. email key)
+ (HttpClientConnection.)
+ (HerokuAPI/with))))
+
+(defn app-api []
+ ;; TODO: depends on current directory; should we pass project args around?
+ (let [git-config (slurp (io/file ".git/config"))
+ [_ app-name] (re-find #"git@heroku.com:(.+).git" git-config)]
+ (.app (api) app-name)))
+
+(defn ^{:no-project-needed true} heroku
+ "Manage Heroku apps."
+ [command & args]
+ (let [command-ns (str "leiningen.heroku." (first (.split command ":")))
+ _ (require (symbol command-ns))
+ subtask (ns-resolve (symbol command-ns) (symbol command))]
+ (try
+ ;; TODO: help
+ (apply subtask args)
+ (catch Exception e
+ (abort (.getMessage e))))))
33 src/leiningen/heroku/apps.clj
@@ -0,0 +1,33 @@
+(ns leiningen.heroku.apps
+ (:require [leiningen.heroku :as heroku])
+ (:import (com.heroku.api.command.app AppList)
+ (com.heroku.api Heroku$Stack)))
+
+(defn create [name]
+ (-> (heroku/api) (.newapp Heroku$Stack/Cedar name)))
+
+(defn apps:create [name]
+ (let [response (create name)]
+ (println "Created app" (.getAppName response))))
+
+(defn apps:destroy [])
+
+(defn- space-key [k longest-key]
+ (apply str k ": " (repeat (- longest-key (count k)) " ")))
+
+(defn apps:info []
+ (let [api (heroku/app-api)
+ info (.info api)
+ longest-key (apply max (map count (keys info)))]
+ (println "==" (.getAppName api))
+ (doseq [[k v] info]
+ (println (space-key k longest-key) v))))
+
+(defn apps:open [])
+
+(defn apps:rename [new-name])
+
+(defn apps []
+ (println "= Heroku Applications")
+ (doseq [app (-> (heroku/api) .apps .getData)]
+ (println "*" (.get app "name"))))

0 comments on commit 9ba711d

Please sign in to comment.
Something went wrong with that request. Please try again.