This repository was archived by the owner on Feb 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmain.m
More file actions
93 lines (78 loc) · 2.82 KB
/
main.m
File metadata and controls
93 lines (78 loc) · 2.82 KB
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
/**
@file main.m
@package xcode-github
@brief Command line interface for the xcode-github app.
@author Edward Smith
@date February 28, 2018
@copyright Copyright © 2018 Branch. All rights reserved.
*/
#import <Foundation/Foundation.h>
#import <XcodeGitHub/XcodeGitHub.h>
#include <sysexits.h>
static BNCLogLevel global_logLevel = BNCLogLevelWarning;
void LogOutputFunction(
NSDate*_Nonnull timestamp,
BNCLogLevel level,
NSString *_Nullable message
) {
if (level < global_logLevel || !message) return;
NSRange range = [message rangeOfString:@") "];
if (range.location != NSNotFound) {
message = [message substringFromIndex:range.location+2];
}
NSData *data = [message dataUsingEncoding:NSNEXTSTEPStringEncoding];
if (!data) return;
int descriptor = (level == BNCLogLevelLog) ? STDOUT_FILENO : STDERR_FILENO;
write(descriptor, data.bytes, data.length);
write(descriptor, "\n ", sizeof('\n'));
}
int main(int argc, char*const argv[]) {
int returnCode = EXIT_FAILURE;
BOOL repeatForever = NO;
start:
@autoreleasepool {
BNCLogSetOutputFunction(LogOutputFunction);
BNCLogSetDisplayLevel(BNCLogLevelWarning);
XGCommandOptions *options = [[XGCommandOptions alloc] initWithArgc:argc argv:argv];
if (options.badOptionsError) {
returnCode = EX_USAGE;
goto exit;
}
if (options.showHelp) {
NSData *data = [[XGCommandOptions helpString] dataUsingEncoding:NSUTF8StringEncoding];
write(STDOUT_FILENO, data.bytes, data.length);
returnCode = EXIT_SUCCESS;
goto exit;
}
global_logLevel = MIN(MAX(BNCLogLevelWarning - options.verbosity, BNCLogLevelAll), BNCLogLevelNone);
BNCLogSetDisplayLevel(global_logLevel);
if (options.showVersion) {
BNCLog(@"xcode-github version %@(%@).",
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"],
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]
);
returnCode = EXIT_SUCCESS;
goto exit;
}
if (options.showStatusOnly) {
if (XGShowXcodeBotStatus(options) == nil)
returnCode = EXIT_SUCCESS;
goto exit;
}
NSError *error = XGUpdateXcodeBotsWithGitHub(options);
if (error) {
returnCode = [error.userInfo[@"return_code"] intValue];
goto exit;
}
error = XGShowXcodeBotStatus(options);
if (error == nil) returnCode = EXIT_SUCCESS;
repeatForever = options.repeatForever;
}
if (repeatForever) {
sleep(60);
goto start;
}
exit:
BNCLogFlushMessages();
return returnCode;
}