This repository has been archived by the owner on Aug 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.clj
93 lines (82 loc) · 3.72 KB
/
core.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
(ns com.agilecreativity.gh_utils.core
(:gen-class)
(:require [aero.core :refer [read-config]]
[clojure.string :as string]
[clojure.tools.cli :as cli]
[com.agilecreativity.gh_utils.git_helper :as hlp :refer :all]
[com.agilecreativity.gh_utils.option :as opt :refer :all]
[me.raynes.fs :as fs]
[tentacles.repos :as t-repos]))
(defn- expand-and-normalize
"Allow the ~ to be expanded."
[filename]
(-> filename
fs/expand-home
fs/normalized))
(defn- default-options [& options]
"Define the sensible default options when creating new Github repository"
(let [opts (merge {:public false
:has-issue false
:has-wiki false
:has-download false}
(first options))]
opts))
(defn- check-and-confirm-result
"Check and confirm if we can successfully create a new Github repository."
[result]
(if (:status result)
(println "Problem creating new repository, errors : " (get-in result [:body :errors]))
(let [url (:html_url result)
origin-prefix "git remote add origin "
https-url (str origin-prefix url ".git")
ssh-url
(clojure.string/replace-first https-url
#"https://github.com/"
"git@github.com:")]
(println (str "You have succesfully created new repository at : " url)))))
(defn create-new-repo!
"Create new repository using the given options"
[options]
(let [config-options (:config options)
reponame (:repo options)]
(try
(if-let [config (read-config (expand-and-normalize config-options))]
(let [username (:username config)
password (:password config)
auth (str username ":" password)]
(let [github-prefix "https://github.com/"
homepage (str github-prefix (clojure.string/join "/" (list username reponame)))
result (t-repos/create-repo reponame (default-options {:auth auth
:description (str reponame " by " username)
:homepage homepage}))]
(check-and-confirm-result result)
;; Make sure that we are running from the right directory
(let [base-dir (fs/file ".")
{:keys [init-commit
remote-label
push]} options]
;; Don't run git-init commit if project already containing .git directory
(if-not (fs/exists? ".git")
(if init-commit (hlp/git-init-commit base-dir)))
;; We always add remote tracking
(hlp/git-add-remote username reponame base-dir remote-label)
;; Run git push only if the user specify '--push' option.
(if (fs/exists? ".git")
(if push (hlp/git-push-remote base-dir remote-label ""))
;; No valid git project, so let the the user know
(println "FYI: As you don't have the valid git project, you may like to run git init && git push manually."))))))
(exit 0 "Done")
(catch Exception e
;; Handle any problem/exception that we may have
(exit 1 (println (str "Error loading configuration file: " (.getMessage e))))))))
(defn -main [& args]
(let [{:keys [options arguments errors summary]}
(cli/parse-opts args opt/options)]
(cond
(:help options)
(exit 0 (usage summary))
(:config options)
(if (:repo options)
(create-new-repo! options)
(exit 0 (usage summary)))
:else (exit 0 (usage summary)))))