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

Commit d8f0c37

Browse files
Merge branch 'fixes/octorun-output-refactoring' into stanley/0.31-rc
2 parents 977b32e + 687e162 commit d8f0c37

File tree

14 files changed

+49
-125
lines changed

14 files changed

+49
-125
lines changed

octorun/src/authentication.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ var endOfLine = require('os').EOL;
22
var config = require("./configuration");
33
var octokitWrapper = require("./octokit");
44

5-
var lockedRegex = new RegExp("number of login attempts exceeded", "gi");
65
var twoFactorRegex = new RegExp("must specify two-factor authentication otp code", "gi");
7-
var badCredentialsRegex = new RegExp("bad credentials", "gi");
86

97
var scopes = ["user", "repo", "gist", "write:public_key"];
108

@@ -48,12 +46,6 @@ var handleAuthentication = function (username, password, onSuccess, onFailure, t
4846
else if (twoFactorRegex.test(err.message)) {
4947
onSuccess(password, "2fa");
5048
}
51-
else if (lockedRegex.test(err.message)) {
52-
onFailure("locked")
53-
}
54-
else if (badCredentialsRegex.test(err.message)) {
55-
onFailure("badcredentials")
56-
}
5749
else {
5850
onFailure(err)
5951
}

octorun/src/bin/app-login.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,12 @@ var handleAuthentication = function (username, password, twoFactor) {
1313
authentication.handleAuthentication(username, password, function (token, status) {
1414
if (status) {
1515
output.custom(status, token);
16-
process.exit();
1716
}
1817
else {
1918
output.success(token);
20-
process.exit();
2119
}
2220
}, function (error) {
2321
output.error(error);
24-
process.exit();
2522
}, twoFactor);
2623
}
2724

@@ -41,7 +38,6 @@ if (commander.twoFactor) {
4138
}
4239
catch (error) {
4340
output.error(error);
44-
process.exit();
4541
}
4642
}
4743
else {
@@ -108,7 +104,6 @@ else {
108104
}
109105
catch (error) {
110106
output.error(error);
111-
process.exit();
112107
}
113108
});
114109
}

octorun/src/bin/app-organizations.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,18 @@ try {
1414
apiWrapper.getOrgs(function (error, result) {
1515
if (error) {
1616
output.error(error);
17-
process.exit();
1817
}
1918
else {
20-
results = [];
19+
var results = [];
2120
for (var i = 0; i < result.length; i++) {
2221
results.push(result[i].name);
2322
results.push(result[i].login);
2423
}
2524

2625
output.success(results);
27-
process.exit();
2826
}
2927
});
3028
}
3129
catch (error) {
3230
output.error(error);
33-
process.exit();
3431
}

octorun/src/bin/app-publish.js

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

67
commander
78
.version(package.version)
@@ -13,11 +14,8 @@ commander
1314

1415
if(!commander.repository)
1516
{
16-
process.stdout.write("repository required");
17-
process.stdout.write(endOfLine);
1817
commander.help();
1918
process.exit(-1);
20-
return;
2119
}
2220

2321
var private = false;
@@ -31,46 +29,13 @@ try {
3129
apiWrapper.publish(commander.repository, commander.description, private, commander.organization,
3230
function (error, result) {
3331
if (error) {
34-
process.stdout.write("error");
35-
process.stdout.write(endOfLine);
36-
37-
process.stdout.write("");
38-
process.stdout.write(endOfLine);
39-
40-
process.stdout.write("");
41-
process.stdout.write(endOfLine);
42-
43-
if (error) {
44-
process.stdout.write(error.toString());
45-
process.stdout.write(endOfLine);
46-
}
47-
48-
process.exit();
32+
output.error(error);
4933
}
5034
else {
51-
process.stdout.write("success");
52-
process.stdout.write(endOfLine);
53-
54-
process.stdout.write(commander.repository);
55-
process.stdout.write(endOfLine);
56-
57-
process.stdout.write(result);
58-
process.stdout.write(endOfLine);
59-
process.exit();
35+
output.success(commander.repository);
6036
}
6137
});
6238
}
6339
catch (error) {
64-
process.stdout.write("error");
65-
process.stdout.write(endOfLine);
66-
67-
process.stdout.write("");
68-
process.stdout.write(endOfLine);
69-
70-
if (error) {
71-
process.stdout.write(error.toString());
72-
process.stdout.write(endOfLine);
73-
}
74-
75-
process.exit();
40+
output.error(error);
7641
}

octorun/src/bin/app-usage.js

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var package = require('../../package.json')
33
var endOfLine = require('os').EOL;
44
var fs = require('fs');
55
var util = require('util');
6+
var output = require('../output');
67

78
commander
89
.version(package.version)
@@ -50,38 +51,20 @@ if (fileContents && host) {
5051

5152
res.on('data', function (d) {
5253
if (success) {
53-
process.stdout.write("success");
54-
process.stdout.write(endOfLine);
55-
process.stdout.write(d);
56-
process.stdout.write(endOfLine);
54+
output.custom("success", d, true);
5755
}
5856
else {
59-
process.stdout.write("error");
60-
process.stdout.write(endOfLine);
61-
62-
process.stdout.write("");
63-
process.stdout.write(endOfLine);
64-
65-
process.stdout.write(d);
66-
process.stdout.write(endOfLine);
57+
output.custom("error", "", true);
6758
}
6859
});
6960

7061
res.on('end', function (d) {
71-
process.exit(success ? 0 : -1);
62+
process.exit();
7263
});
7364
});
7465

7566
req.on('error', function (error) {
76-
process.stdout.write("Error");
77-
process.stdout.write(endOfLine);
78-
79-
if (error) {
80-
process.stdout.write(error.toString());
81-
process.stdout.write(endOfLine);
82-
}
83-
84-
process.exit(-1);
67+
output.error(error);
8568
});
8669

8770
req.write(fileContents);

octorun/src/output.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var endOfLine = require('os').EOL;
22

3-
var outputResult = function (status, results, errors) {
3+
var outputResult = function (status, results, errors, preventExit) {
44
process.stdout.write(status);
55
process.stdout.write(endOfLine);
66

@@ -44,19 +44,27 @@ var outputResult = function (status, results, errors) {
4444
process.stdout.write(endOfLine);
4545
}
4646
}
47+
else if (errors.toString) {
48+
process.stdout.write(errors.toString());
49+
process.stdout.write(endOfLine);
50+
}
4751
else {
4852
process.stdout.write(errors);
4953
process.stdout.write(endOfLine);
5054
}
5155
}
56+
57+
if(!preventExit) {
58+
process.exit();
59+
}
5260
}
5361

5462
var outputSuccess = function (results) {
5563
outputResult("success", results);
5664
}
5765

58-
var outputCustom = function (status, results) {
59-
outputResult(status, results);
66+
var outputCustom = function (status, results, preventExit) {
67+
outputResult(status, results, undefined, preventExit);
6068
}
6169

6270
var outputError = function (errors) {

octorun/version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
b91b7b60
1+
196e9867

src/GitHub.Api/Application/ApiClient.cs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,7 @@ private async Task<GitHubRepository> CreateRepositoryInternal(string repositoryN
247247
};
248248
}
249249

250-
if (ret.Output.Any())
251-
{
252-
throw new ApiClientException(string.Join(Environment.NewLine, ret.Output));
253-
}
254-
255-
throw new ApiClientException("Publish failed");
250+
throw new ApiClientException(ret.GetApiErrorMessage() ?? "Publish failed");
256251
}
257252
catch (Exception ex)
258253
{
@@ -294,12 +289,7 @@ private async Task GetOrganizationInternal(Action<Organization[]> onSuccess, Act
294289
return;
295290
}
296291

297-
if (ret.Output.Any())
298-
{
299-
throw new ApiClientException(string.Join(Environment.NewLine, ret.Output));
300-
}
301-
302-
throw new ApiClientException("Error getting organizations");
292+
throw new ApiClientException(ret.GetApiErrorMessage() ?? "Error getting organizations");
303293
}
304294
catch (Exception ex)
305295
{
@@ -332,12 +322,7 @@ private async Task<GitHubUser> GetCurrentUserInternal()
332322
};
333323
}
334324

335-
if (ret.Output.Any())
336-
{
337-
throw new ApiClientException(string.Join(Environment.NewLine, ret.Output));
338-
}
339-
340-
throw new ApiClientException("Error validating current user");
325+
throw new ApiClientException(ret.GetApiErrorMessage() ?? "Error validating current user");
341326
}
342327
catch (KeychainEmptyException)
343328
{

src/GitHub.Api/Authentication/Keychain.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,6 @@ private void UpdateConnections(Connection[] conns)
323323
public Connection[] Connections => connections.Values.ToArray();
324324
public IList<UriString> Hosts => connections.Keys.ToArray();
325325
public bool HasKeys => connections.Any();
326-
public bool NeedsLoad => HasKeys && !string.IsNullOrEmpty(FindOrCreateAdapter(connections.First().Value.Host).Credential.Token);
326+
public bool NeedsLoad => HasKeys && !string.IsNullOrEmpty(FindOrCreateAdapter(connections.First().Value.Host).Credential?.Token);
327327
}
328328
}

src/GitHub.Api/Authentication/LoginManager.cs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -193,22 +193,7 @@ private async Task<LoginResultData> TryLogin(
193193
return new LoginResultData(resultCodes, message, host, ret.Output[0]);
194194
}
195195

196-
if (ret.IsBadCredentials)
197-
{
198-
return new LoginResultData(LoginResultCodes.Failed, "Bad credentials.", host, ret.Output[0]);
199-
}
200-
201-
if (ret.IsLocked)
202-
{
203-
return new LoginResultData(LoginResultCodes.LockedOut, "Account locked.", host, ret.Output[0]);
204-
}
205-
206-
if (ret.Output.Any())
207-
{
208-
return new LoginResultData(LoginResultCodes.Failed, "Failed.", host, ret.Output[0]);
209-
}
210-
211-
return new LoginResultData(LoginResultCodes.Failed, "Failed.", host);
196+
return new LoginResultData(LoginResultCodes.Failed, ret.GetApiErrorMessage() ?? "Failed.", host);
212197
}
213198
}
214199

0 commit comments

Comments
 (0)