bin.jsView |
---|
4 | 4 … | |
5 | 5 … | |
6 | 6 … | var path = require('path') |
7 | 7 … | var proc = require('child_process') |
8 | | -var u = require('./lib/util') |
9 | 8 … | |
10 | | -var prog = 'git ssb' |
11 | | - |
12 | 9 … | main() |
13 | 10 … | |
14 | 11 … | function main() { |
15 | 12 … | switch (path.basename(process.argv[1])) { |
23 | 20 … | var config = require('ssb-config/inject')(appName) |
24 | 21 … | |
25 | 22 … | var cmd = config._.shift() |
26 | 23 … | if (config.help) |
27 | | - return help(cmd) |
| 24 … | + return require('./lib/help')(cmd) |
28 | 25 … | if (config.version) |
29 | 26 … | return version() |
30 | 27 … | |
31 | 28 … | switch (cmd) { |
32 | 29 … | case 'create': |
33 | | - return createRepo(config, config._[0], config._[1]) |
| 30 … | + return require('./lib/create')(config, config._[0], config._[1]) |
34 | 31 … | case 'fork': |
35 | | - return forkRepo(config) |
| 32 … | + return require('./lib/fork')(config) |
36 | 33 … | case 'forks': |
37 | 34 … | return require('./lib/forks')(config) |
38 | 35 … | case 'issues': |
39 | 36 … | return require('./lib/issues')(config) |
40 | 37 … | case 'prs': |
41 | 38 … | case 'pull-requests': |
42 | 39 … | return require('./lib/pull-requests')(config) |
43 | 40 … | case 'name': |
44 | | - return nameRepo(config) |
| 41 … | + return require('./lib/name')(config) |
45 | 42 … | case 'pull-request': |
46 | 43 … | return require('./lib/pull-request')(config) |
47 | 44 … | case 'web': |
48 | 45 … | return require('git-ssb-web/server') |
49 | 46 … | case 'help': |
50 | | - return help(config._[0]) |
| 47 … | + return require('./lib/help')(config._[0]) |
51 | 48 … | case 'version': |
52 | 49 … | return version() |
53 | 50 … | case undefined: |
54 | | - return usage(0) |
| 51 … | + return require('./lib/help')() |
55 | 52 … | default: |
56 | | - err(1, 'No such command \'' + cmd + '\'') |
| 53 … | + throw 'No such command \'' + cmd + '\'' |
57 | 54 … | } |
58 | 55 … | } |
59 | 56 … | |
60 | | -function usage(code) { |
61 | | - out( |
62 | | - 'Usage: git ssb [--version] [--help] [command]', |
63 | | - '', |
64 | | - 'Commands:', |
65 | | - ' create Create a git repo on SSB', |
66 | | - ' fork Fork a git repo on SSB', |
67 | | - ' forks List forks of a repo', |
68 | | - ' issues List issues for a repo', |
69 | | - ' prs List pull requests for a repo', |
70 | | - ' name Name a repo', |
71 | | - ' pull-request Create a pull-request', |
72 | | - ' web Serve a web server for repos', |
73 | | - ' help Get help about a command') |
74 | | - process.exit(code) |
75 | | -} |
76 | | - |
77 | 57 … | function version() { |
78 | 58 … | var pkg = require('./package') |
79 | 59 … | console.log(pkg.name, pkg.version) |
80 | 60 … | } |
81 | | - |
82 | | -function help(cmd) { |
83 | | - switch (cmd) { |
84 | | - case 'help': |
85 | | - return out( |
86 | | - 'Usage: ' + prog + ' help <command>', |
87 | | - '', |
88 | | - ' Get help about a git-ssb command', |
89 | | - '', |
90 | | - 'Options:', |
91 | | - ' command Command to get help with') |
92 | | - case 'create': |
93 | | - return out( |
94 | | - 'Usage: ' + prog + ' create <remote> [<name>]', |
95 | | - '', |
96 | | - ' Create a new git-ssb repo and add it as a git remote', |
97 | | - '', |
98 | | - 'Arguments:', |
99 | | - ' remote Name of the remote to add. e.g. \'origin\' or \'ssb\'', |
100 | | - ' name Name to give the repo, if any') |
101 | | - case 'fork': |
102 | | - return out( |
103 | | - 'Usage: ' + prog + ' fork [<upstream>] <remote_name>', |
104 | | - '', |
105 | | - ' Create a new git-ssb repo as a fork of another repo', |
106 | | - ' and add it as a git remote', |
107 | | - '', |
108 | | - 'Arguments:', |
109 | | - ' upstream id, url, or git remote name of the repo to fork.', |
110 | | - ' default: \'origin\' or \'ssb\'', |
111 | | - ' remote_name Name for the new remote') |
112 | | - case 'forks': |
113 | | - return out( |
114 | | - 'Usage: ' + prog + ' forks [<repo>]', |
115 | | - '', |
116 | | - ' List repos that are forks of the given repo', |
117 | | - '', |
118 | | - 'Arguments:', |
119 | | - ' repo id, url, or git remote name of the base repo.', |
120 | | - ' default: \'origin\' or \'ssb\'') |
121 | | - case 'issues': |
122 | | - return out( |
123 | | - 'Usage: ' + prog + ' issues [--all|--open|--closed] [<repo>]', |
124 | | - '', |
125 | | - ' List issues about a repo', |
126 | | - '', |
127 | | - 'Arguments:', |
128 | | - ' repo id, url, or git remote name of the repo.', |
129 | | - ' default: \'origin\' or \'ssb\'', |
130 | | - 'Options:', |
131 | | - ' --open Show only open issues (default)', |
132 | | - ' --closed Show only closed issues', |
133 | | - ' --all Show issues of all state') |
134 | | - case 'pull-requests': |
135 | | - case 'prs': |
136 | | - return out( |
137 | | - 'Usage: ' + prog + ' prs [--all|--open|--closed] [<repo>]', |
138 | | - '', |
139 | | - ' List pull requests for a repo', |
140 | | - '', |
141 | | - 'Arguments:', |
142 | | - ' repo id, url, or git remote name of the base repo.', |
143 | | - ' default: \'origin\' or \'ssb\'', |
144 | | - 'Options:', |
145 | | - ' --open Show only open pull requests (default)', |
146 | | - ' --closed Show only closed pull-requests', |
147 | | - ' --all Show pull requests of all state') |
148 | | - case 'name': |
149 | | - return out( |
150 | | - 'Usage: ' + prog + ' name [<repo>] <name>', |
151 | | - '', |
152 | | - ' Publish a name for a git-ssb repo', |
153 | | - '', |
154 | | - 'Arguments:', |
155 | | - ' repo id, url, or git remote name of the base repo.', |
156 | | - ' default: \'origin\' or \'ssb\'', |
157 | | - ' name the name to give the repo') |
158 | | - case 'pull-request': |
159 | | - return out( |
160 | | - 'Usage: ' + prog + ' pull-request [-b <base>] [-h <head>],', |
161 | | - ' [-m <message> | -F <file>]', |
162 | | - '', |
163 | | - ' Create a pull request. This requests that changes from <head>', |
164 | | - ' be merged into <base>.', |
165 | | - '', |
166 | | - 'Arguments:', |
167 | | - ' head the head repo/branch, in format "[<repo>:]<branch>"', |
168 | | - ' Defaults to \'origin\' or \'ssb\', and the current branch.', |
169 | | - ' base the base repo/branch, in format "[<repo>:]<branch>"', |
170 | | - ' where <repo> may be a repo id or git remote name.', |
171 | | - ' Defaults to the upstream of <head>, or <head>,', |
172 | | - ' and its default branch (usually \'master\')', |
173 | | - ' message the text for the pull-request message', |
174 | | - ' file name of file from which to read pull-request text') |
175 | | - case 'web': |
176 | | - return out( |
177 | | - 'Usage: ' + prog + ' web [<host:port>] [<options>]', |
178 | | - '', |
179 | | - ' Host a git ssb web server', |
180 | | - '', |
181 | | - 'Options:', |
182 | | - ' host Host to bind to. default: localhost', |
183 | | - ' port Port to bind to. default: 7718', |
184 | | - ' --public Make the instance read-only') |
185 | | - case undefined: |
186 | | - usage(0) |
187 | | - default: |
188 | | - err(1, 'No help for command \'' + cmd + '\'') |
189 | | - } |
190 | | -} |
191 | | - |
192 | | -function out() { |
193 | | - console.log([].slice.call(arguments).join('\n')) |
194 | | -} |
195 | | - |
196 | | -function err(code) { |
197 | | - var args = [].slice.call(arguments, 1) |
198 | | - console.error.apply(console, [prog + ':'].concat(args)) |
199 | | - process.exit(code) |
200 | | -} |
201 | | - |
202 | | -function hasRemote(name) { |
203 | | - var child = proc.spawnSync('git', ['remote'], {encoding: 'utf8'}) |
204 | | - var remotes = child.stdout.split(/\n/) |
205 | | - return !!~remotes.indexOf(name) |
206 | | -} |
207 | | - |
208 | | -function createRepo(config, remoteName, name, upstream) { |
209 | | - if (config._.length == 0) return help('create') |
210 | | - if (!remoteName) err(1, 'Missing remote name') |
211 | | - if (hasRemote(remoteName)) |
212 | | - err(1, 'Remote \'' + remoteName + '\' already exists') |
213 | | - u.getSbot(config, function (err, sbot) { |
214 | | - if (err) throw err |
215 | | - var ssbGit = require('ssb-git-repo') |
216 | | - ssbGit.createRepo(sbot, { |
217 | | - upstream: upstream, |
218 | | - name: name |
219 | | - }, function (err, repo) { |
220 | | - if (err) throw err |
221 | | - var url = 'ssb://' + repo.id |
222 | | - console.log('Created repo:', url, name ? '(' + name + ')' : '') |
223 | | - proc.spawnSync('git', ['remote', 'add', remoteName, url]) |
224 | | - console.log('Added remote:', remoteName) |
225 | | - repo.close() |
226 | | - sbot.close() |
227 | | - }) |
228 | | - }) |
229 | | -} |
230 | | - |
231 | | -function forkRepo(argv) { |
232 | | - var repo |
233 | | - if (argv._.length == 1) repo = u.getDefaultRemote() |
234 | | - else if (argv._.length == 2) repo = u.getRemote(argv._.shift()) |
235 | | - else return help('fork') |
236 | | - if (!repo) err(1, 'unable to find git-ssb upstream repo') |
237 | | - var name = argv._[0] |
238 | | - if (!name) err(1, 'missing remote name') |
239 | | - |
240 | | - createRepo(argv, name, null, repo) |
241 | | -} |
242 | | - |
243 | | -function nameRepo(argv) { |
244 | | - var repo |
245 | | - if (argv._.length == 1) repo = u.getDefaultRemote() |
246 | | - else if (argv._.length == 2) repo = u.getRemote(argv._.shift()) |
247 | | - else return help('name') |
248 | | - if (!repo) err(1, 'unable to find git-ssb repo') |
249 | | - var name = argv._[0] |
250 | | - if (!name) err(1, 'missing name') |
251 | | - |
252 | | - u.getSbot(argv, function (err, sbot) { |
253 | | - if (err) throw err |
254 | | - var schemas = require('ssb-msg-schemas') |
255 | | - sbot.publish(schemas.name(repo, name), function (err, msg) { |
256 | | - if (err) throw err |
257 | | - console.log(msg.key) |
258 | | - sbot.close() |
259 | | - }) |
260 | | - }) |
261 | | -} |