1 | 'use strict';
|
2 | let git = require('../lib/git');
|
3 | let h = require('heroku-cli-util');
|
4 |
|
5 | function includes (array, item) {
|
6 | return array.indexOf(item) !== -1;
|
7 | }
|
8 |
|
9 | module.exports = {
|
10 | topic: 'git',
|
11 | command: 'remote',
|
12 | needsAuth: true,
|
13 | variableArgs: true,
|
14 | flags: [
|
15 | {name: 'app', char: 'a', hasValue: true},
|
16 | {name: 'remote', char: 'r', hasValue: true},
|
17 | {name: 'ssh-git'},
|
18 | ],
|
19 | run: h.command(function* (context, heroku) {
|
20 | let appName = context.flags.app || context.args.shift();
|
21 | if (!appName) {
|
22 | return h.error('Specify an app with --app');
|
23 | }
|
24 | let app = yield heroku.apps(appName).info();
|
25 | let remote = context.flags.remote || (yield git.remoteFromGitConfig()) || 'heroku';
|
26 | let remotes = yield git.exec(['remote']);
|
27 | let url = context.flags['ssh-git'] ? git.sshGitUrl(app.name) : git.httpGitUrl(app.name);
|
28 | if (includes(remotes.split('\n'), remote)) {
|
29 | yield git.exec(['remote', 'set-url', remote, url].concat(context.args));
|
30 | } else {
|
31 | yield git.exec(['remote', 'add', remote, url].concat(context.args));
|
32 | }
|
33 | console.log(`set git remote ${h.color.cyan(remote)} to ${h.color.cyan(url)}`);
|
34 | })
|
35 | };
|