-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·136 lines (120 loc) · 4.17 KB
/
cli.js
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env node;
/*jshint esversion: 6 */
"use strict";
/**
* Created by bryancross on 7/13/18.
*/
const RepoController = require('./lib/RepoController');
const cla = require('command-line-args');
const clu = require('command-line-usage');
const optionDefinitions = [
{name: 'templateRepoURL', alias: 't', type: String}
, {name: 'targetRepoName', alias: 'n', type: String}
, {name: 'targetRepoOwner', alias: 'o', type: String}
, {name: 'mode', alias: 'm', type: String, defaultValue: 'get'}
, {name: 'help', alias: 'h', type: Boolean}
, {name: 'compareRepoURL', alias: 'c', type: String}
];
const clSections = [
{
header: 'repo-copy',
content: 'A utility to retrieve configuration info for and/or copy a repository.'
},
{
header: 'Options',
optionList: [
{
name: 'templateURL',
alias: 't',
typeLabel: '{underline url}',
description: 'URL for the repository to copy or retrieve configuration info for.'
},
{
name: 'compareRepoURL',
alias: 'c',
typeLabel: '{underline url}',
description: 'URL to compare templateRepoURL to'
},
{
name: 'mode',
alias: 'm',
description: 'get|create|audit. \n get - return configuration data \n create - use configuration of <templateURL> to create a new repository\naudit - Return differences in configuration between template and target repos'
},
{
name: 'targetRepoName',
alias: 'n',
description: 'Name of the repo to be created, or, in the case of <audit> mode, compared against'
},
{
name: 'targetRepoOwner',
alias: 'o',
description: 'Owner of the repo to be created, or, in the case of <audit> mode, compared against'
},
{
name: 'help',
alias: 'h',
type: Boolean,
description: 'Display this message'
}
]
},
{
header: 'Examples',
content: [
{
desc: '1. Retrieve repository configuration information',
example: '$ node repo-copy -t https://github.com/myorg/myrepo'
},
{
desc: '2. Create a new repository copying the configuration of a template repository',
example: '$ node repo-copy -t https://github.com/myorg/myrepo -n myNewRepo -o myOtherOrg'
},
{
desc: '3. Retrieve a list of configuration differences between 2 repositories',
example: '$ node repo-copy -t https://github.com/sourceOrg/sourceRepo -n myCompareRepo -o myCompareOrg'
}
]
},
{
content: 'Project home: {underline https://github.com/bidnessforb/repo-copy}'
}
];
const args = cla(optionDefinitions);
if (args.help) {
console.log(clu(clSections));
process.exit(0);
}
execute();
async function execute() { //jshint ignore:line
var repo = new RepoController();
var fakeRequest = {};
var fakebody;
try {
//build a fake request
if (args.mode === 'get') {
fakebody = [{URL: args.templateRepoURL}];
}
else if (args.mode === 'audit') {
fakebody = [{URL: args.templateRepoURL}, {URL: args.compareRepoURL}];
}
else if (args.mode === 'create') {
fakebody = {
templateRepoURL: args.templateRepoURL,
newRepoOwner: args.targetRepoOwner,
newRepoName: args.targetRepoName,
tokens: []
};
}
fakeRequest.body = JSON.stringify(fakebody);
fakeRequest.headers = {};
fakeRequest.headers.authorization = process.env.GH_PAT;
repo.executeRest(fakeRequest, null, args.mode, function (data) {
console.log(JSON.stringify(data));
process.exit(0);
});
}
catch (err) {
console.log('Error: ' + err.message);
process.exit(1);
}
}