-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshortlr
executable file
·103 lines (88 loc) · 2.6 KB
/
shortlr
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
#!/usr/bin/env node
/**
* Created by piyush0 on 26/05/17.
*/
const exec = require('child_process').exec;
const secret = ""; // Add your secret
const querystring = require('querystring');
const http = require('http');
if (process.argv[2] === '-l') {
const customShortcode = process.argv[4];
PostCode(process.argv[3], secret, customShortcode);
} else if (process.argv[2] === '-s') {
let remote = null;
if (process.argv[3]) {
remote = process.argv[3];
} else {
remote = 'origin';
}
exec('git config --get remote.' + remote + '.url', function (error, stdout, stderr) {
if (error) {
console.error(`exec error: ${error}`);
return;
}
let str = `${stdout}`;
PostCode(str);
});
} else {
const customShortcode = process.argv[5];
const directory = process.argv[4];
let remote = null;
let branch = null;
if (process.argv[2]) {
remote = process.argv[2];
} else {
remote = 'origin';
}
if (process.argv[3]) {
branch = process.argv[3];
} else {
branch = 'master';
}
exec('git config --get remote.' + remote + '.url', function (error, stdout, stderr) {
if (error) {
console.error(`exec error: ${error}`);
return;
}
let str = `${stdout}`;
if (directory) {
str = str.substring(0, str.length - 5) + "/" + "tree/" + branch + "/" + directory;
PostCode(str, secret, customShortcode);
}
else {
PostCode(str, secret, customShortcode);
}
console.log("Pushing code to " + remote + " " + branch);
exec('git push ' + remote + ' ' + branch, function (error, stdout, stderr) {
if (error) {
console.error(`exec error: ${error}`);
return;
}
})
});
}
function PostCode(url, secret, code) {
url = url.replace(/(\r\n|\n|\r)/gm, "");
let post_data = querystring.stringify({
'url': url,
'secret': secret,
'code': code
});
let post_options = {
host: 'cb.lk',
path: '/api/v1/shorten',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(post_data)
}
};
let post_req = http.request(post_options, function (res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('http://cb.lk/' + JSON.parse(chunk).shortcode);
});
});
post_req.write(post_data);
post_req.end();
}