Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 7856043

Browse files
Merge pull request #616 from github-for-unity/fixes/octorun-js-output
Improving how data is output and read from Octorun
2 parents 33dabf1 + 42f66f6 commit 7856043

File tree

8 files changed

+210
-143
lines changed

8 files changed

+210
-143
lines changed

octorun/src/bin/app-login.js

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
var commander = require("commander");
2-
var package = require('../../package.json')
3-
var authentication = require('../authentication')
2+
var package = require('../../package.json');
3+
var authentication = require('../authentication');
44
var endOfLine = require('os').EOL;
5+
var output = require('../output');
56

67
commander
78
.version(package.version)
@@ -13,22 +14,10 @@ var encoding = 'utf-8';
1314
if (commander.twoFactor) {
1415
var handleTwoFactorAuthentication = function (username, password, token) {
1516
authentication.handleTwoFactorAuthentication(username, password, token, function (token) {
16-
process.stdout.write("success");
17-
process.stdout.write(endOfLine);
18-
process.stdout.write(token);
19-
process.stdout.write(endOfLine);
17+
output.success(token);
2018
process.exit();
2119
}, function (error) {
22-
process.stdout.write("error");
23-
process.stdout.write(endOfLine);
24-
process.stdout.write("");
25-
process.stdout.write(endOfLine);
26-
27-
if (error) {
28-
process.stdout.write(error.toString());
29-
process.stdout.write(endOfLine);
30-
}
31-
20+
output.error(error);
3221
process.exit();
3322
});
3423
}
@@ -68,27 +57,13 @@ else {
6857
var handleBasicAuthentication = function (username, password) {
6958
authentication.handleBasicAuthentication(username, password,
7059
function (token) {
71-
process.stdout.write("success");
72-
process.stdout.write(endOfLine);
73-
process.stdout.write(token);
74-
process.stdout.write(endOfLine);
60+
output.success(token);
7561
process.exit();
7662
}, function () {
77-
process.stdout.write("2fa");
78-
process.stdout.write(endOfLine);
79-
process.stdout.write(password);
80-
process.stdout.write(endOfLine);
63+
output.custom("2fa", password);
8164
process.exit();
8265
}, function (error) {
83-
process.stdout.write("error");
84-
process.stdout.write(endOfLine);
85-
process.stdout.write("");
86-
process.stdout.write(endOfLine);
87-
88-
if (error) {
89-
process.stdout.write(error.toString());
90-
process.stdout.write(endOfLine);
91-
}
66+
output.error(error);
9267
process.exit();
9368
});
9469
}

octorun/src/bin/app-organizations.js

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
var commander = require("commander");
2-
var package = require('../../package.json')
3-
var ApiWrapper = require('../api')
2+
var package = require('../../package.json');
3+
var ApiWrapper = require('../api');
44
var endOfLine = require('os').EOL;
5+
var output = require('../output');
56

67
commander
78
.version(package.version)
@@ -12,39 +13,22 @@ try {
1213
var apiWrapper = new ApiWrapper();
1314
apiWrapper.getOrgs(function (error, result) {
1415
if (error) {
15-
process.stdout.write("error");
16-
process.stdout.write(endOfLine);
17-
18-
if (error) {
19-
process.stdout.write(error.toString());
20-
process.stdout.write(endOfLine);
21-
}
22-
16+
output.error(error);
2317
process.exit();
2418
}
2519
else {
26-
process.stdout.write("success");
27-
process.stdout.write(endOfLine);
28-
20+
let results = [];
2921
for (var i = 0; i < result.length; i++) {
30-
process.stdout.write(result[i].name);
31-
process.stdout.write(endOfLine);
32-
process.stdout.write(result[i].login);
33-
process.stdout.write(endOfLine);
22+
results.push(result[i].name);
23+
results.push(result[i].login);
3424
}
3525

26+
output.success(results);
3627
process.exit();
3728
}
3829
});
3930
}
4031
catch (error) {
41-
process.stdout.write("Error");
42-
process.stdout.write(endOfLine);
43-
44-
if (error) {
45-
process.stdout.write(error.toString());
46-
process.stdout.write(endOfLine);
47-
}
48-
32+
output.error(error);
4933
process.exit();
5034
}

octorun/src/bin/app-validate.js

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
var commander = require("commander");
2-
var package = require('../../package.json')
2+
var package = require('../../package.json');
33
var endOfLine = require('os').EOL;
4-
var ApiWrapper = require('../api')
4+
var ApiWrapper = require('../api');
5+
var output = require('../output');
56

67
commander
78
.version(package.version)
@@ -12,39 +13,16 @@ try {
1213

1314
apiWrapper.verifyUser(function (error, result) {
1415
if (error) {
15-
process.stdout.write("error");
16-
process.stdout.write(endOfLine);
17-
process.stdout.write("");
18-
process.stdout.write(endOfLine);
19-
process.stdout.write("");
20-
process.stdout.write(endOfLine);
21-
22-
if (error) {
23-
process.stdout.write(error.toString());
24-
process.stdout.write(endOfLine);
25-
}
26-
16+
output.error(error)
2717
process.exit();
2818
}
2919
else {
30-
process.stdout.write("success");
31-
process.stdout.write(endOfLine);
32-
process.stdout.write(result.name);
33-
process.stdout.write(endOfLine);
34-
process.stdout.write(result.login);
35-
process.stdout.write(endOfLine);
20+
output.success([result.name, result.login])
3621
process.exit();
3722
}
3823
});
3924
}
4025
catch (error) {
41-
process.stdout.write("Error");
42-
process.stdout.write(endOfLine);
43-
44-
if (error) {
45-
process.stdout.write(error.toString());
46-
process.stdout.write(endOfLine);
47-
}
48-
26+
output.error(error)
4927
process.exit();
5028
}

octorun/src/output.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
var endOfLine = require('os').EOL;
2+
3+
var outputResult = function (status, results, errors) {
4+
process.stdout.write(status);
5+
process.stdout.write(endOfLine);
6+
7+
if (!results) {
8+
process.stdout.write("");
9+
process.stdout.write(endOfLine);
10+
}
11+
else {
12+
if (typeof results === 'string') {
13+
process.stdout.write(results);
14+
process.stdout.write(endOfLine);
15+
}
16+
else if (Array.isArray(results)) {
17+
for (var resultIndex = 0; resultIndex < results.length; resultIndex++) {
18+
var result = results[resultIndex];
19+
if (typeof result !== 'string') {
20+
throw "Unsupported result output";
21+
}
22+
23+
process.stdout.write(result);
24+
process.stdout.write(endOfLine);
25+
}
26+
}
27+
28+
throw "Unsupported result output";
29+
}
30+
31+
if (errors) {
32+
if (typeof errors === 'string') {
33+
process.stdout.write(errors);
34+
process.stdout.write(endOfLine);
35+
}
36+
else if (Array.isArray(errors)) {
37+
for (var errorIndex = 0; errorIndex < errors.length; errorIndex++) {
38+
var error = errors[errorIndex];
39+
if (typeof error !== 'string') {
40+
throw "Unsupported result output";
41+
}
42+
43+
process.stdout.write(error);
44+
process.stdout.write(endOfLine);
45+
}
46+
}
47+
else {
48+
process.stdout.write(errors);
49+
process.stdout.write(endOfLine);
50+
}
51+
}
52+
}
53+
54+
var outputSuccess = function (results) {
55+
outputResult("success", results);
56+
}
57+
58+
var outputCustom = function (status, results) {
59+
outputResult(status, results);
60+
}
61+
62+
var outputError = function (errors) {
63+
outputResult("error", null, errors);
64+
}
65+
66+
module.exports = {
67+
success: outputSuccess,
68+
custom: outputCustom,
69+
error: outputError
70+
};

script

src/GitHub.Api/Application/ApiClient.cs

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -241,24 +241,18 @@ private async Task<GitHubRepository> CreateRepositoryInternal(NewRepository newR
241241
.Configure(processManager);
242242

243243
var ret = await octorunTask.StartAwait();
244-
245-
if (ret.Count == 0)
244+
if (ret.IsSuccess && ret.Output.Length == 2)
246245
{
247-
throw new ApiClientException("Publish failed");
248-
}
249-
250-
if (ret[0] == "success")
251-
{
252-
return new GitHubRepository()
246+
return new GitHubRepository
253247
{
254-
Name = ret[1],
255-
CloneUrl = ret[2],
248+
Name = ret.Output[0],
249+
CloneUrl = ret.Output[1]
256250
};
257251
}
258252

259-
if (ret.Count > 3)
253+
if (ret.Output.Any())
260254
{
261-
throw new ApiClientException(ret[3]);
255+
throw new ApiClientException(string.Join(Environment.NewLine, ret.Output));
262256
}
263257

264258
throw new ApiClientException("Publish failed");
@@ -287,30 +281,27 @@ private async Task GetOrganizationInternal(Action<Organization[]> onSuccess, Act
287281
.Configure(processManager);
288282

289283
var ret = await octorunTask.StartAsAsync();
290-
291-
logger.Trace("Return: {0}", string.Join(";", ret.ToArray()));
292-
293-
if (ret.Count == 0)
294-
{
295-
throw new ApiClientException("Error getting organizations");
296-
}
297-
298-
if (ret[0] == "success")
284+
if (ret.IsSuccess)
299285
{
300286
var organizations = new List<Organization>();
301-
for (var i = 1; i < ret.Count; i = i + 2)
287+
for (var i = 0; i < ret.Output.Length; i = i + 2)
302288
{
303289
organizations.Add(new Organization
304290
{
305-
Name = ret[i],
306-
Login = ret[i + 1]
291+
Name = ret.Output[i],
292+
Login = ret.Output[i + 1]
307293
});
308294
}
309295

310296
onSuccess(organizations.ToArray());
311297
return;
312298
}
313299

300+
if (ret.Output.Any())
301+
{
302+
throw new ApiClientException(string.Join(Environment.NewLine, ret.Output));
303+
}
304+
314305
throw new ApiClientException("Error getting organizations");
315306
}
316307
catch (Exception ex)
@@ -337,17 +328,20 @@ private async Task<GitHubUser> GetCurrentUserInternal()
337328
.Configure(processManager);
338329

339330
var ret = await octorunTask.StartAsAsync();
340-
341-
logger.Trace("Return: {0}", string.Join(";", ret.ToArray()));
342-
343-
if (ret[0] == "success")
331+
if (ret.IsSuccess)
344332
{
345-
return new GitHubUser {
346-
Name = ret[1],
347-
Login = ret[2]
333+
return new GitHubUser
334+
{
335+
Name = ret.Output[0],
336+
Login = ret.Output[1]
348337
};
349338
}
350339

340+
if (ret.Output.Any())
341+
{
342+
throw new ApiClientException(string.Join(Environment.NewLine, ret.Output));
343+
}
344+
351345
throw new ApiClientException("Error validating current user");
352346
}
353347
catch (KeychainEmptyException)

0 commit comments

Comments
 (0)