Skip to content

Commit 3a1097a

Browse files
committed
got it to open a web page if you add -o
1 parent 5a8210f commit 3a1097a

File tree

3 files changed

+35
-23
lines changed

3 files changed

+35
-23
lines changed

gist-put

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,37 @@
22

33
var post_gist = require("./lib/post")
44
, program = require("commander")
5+
, exec = require('child_process').spawn
6+
, temp = require("temp")
57

68
program
79
.version("0.0.0")
810
.usage("[options] <file1, file2>")
9-
.option("-u, --public", "Make a public gist on your account (It's private by default). Needs to read your authentication details from `git credential fill`.")
11+
.option("-p, --public", "Make a public gist on your account (It's secret by default). Needs to read your authentication details from `git credential fill`.")
1012
.option("-a, --anonymous", "Make an anonymous gist")
1113
.option('-d, --description <string>', "A description for your gist", String)
12-
13-
program.on("--help", function(){
14-
//additional help?
15-
})
16-
17-
// TODO, if (program.args.length === 0){
18-
// // Open editor and then on quite make a
19-
// // gist of the file
20-
// }
14+
.option("-o, --open", "open the url in the default browser after creation")
2115

2216
program.parse(process.argv)
2317

24-
var privacy = ["anonymous", "public", "private"]
18+
// TODO,
19+
if (program.args.length === 0){
20+
// Open editor and then on quite make a
21+
// gist of the file
22+
editor = process.env.EDITOR || "vim"
23+
temp.open("gist-file", function(err, data){
24+
exec(editor, [data.path])
25+
})
26+
}
27+
28+
var privacy = ["anonymous", "public", "secret"]
2529
.some(function(d, i, arr){
2630
privacy = arr[i]
27-
if (arr[i] == "private"){
31+
if (arr[i] == "secret"){
2832
return true
2933
}
3034
return program[d]
3135
})
3236

33-
post_gist(program.args, privacy, process.description)
37+
38+
post_gist(program.args, privacy, process.description, program.open)

lib/post.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ module.exports = simple_post
33
var USER_AGENT = require("./user_agent")
44
, oauth = require("./OAuth")
55
, request = require("request")
6+
, open = require("open")
67
, path = require("path")
8+
, ap = require("ap")
79
, fs = require("fs")
810

911

1012
// file_names is a list of file names, privacy is an array with three possible
11-
function simple_post(file_names, privacy, description){
13+
function simple_post(file_names, privacy, description, to_open){
1214
// iterate through each file, opening it and reading the contents
1315

1416
var payload
@@ -24,9 +26,9 @@ function simple_post(file_names, privacy, description){
2426
, files: files
2527
}
2628

27-
if (privacy === "public" || privacy === "private") {
29+
if (privacy === "public" || privacy === "secret") {
2830
// otherwise this needs to be not set
29-
payload.public = privacy === "public"
31+
payload.public = privacy === "public"
3032
}
3133

3234
for (var i = 0, len = file_names.length; i < len; ++i){
@@ -41,31 +43,35 @@ function simple_post(file_names, privacy, description){
4143
}
4244
// So long as we have not exhausted the list of files, call handler with
4345
// the next one.
44-
(++counter < file_names.length) || done(payload)
46+
(++counter < file_names.length) || ap([to_open], done)(payload)
4547
}
4648
}
4749

48-
function done(payload){
50+
function done(to_open, payload){
4951
var r = {
5052
url: "https://api.github.com/gists"
5153
, headers: {"User-Agent": USER_AGENT }
5254
, json: payload
5355
}
54-
56+
var this_report = ap([to_open], report)
5557
if (payload.public !== undefined){
5658
// then we need to get an Oauth token
5759
oauth(function(err, token){
5860
if (err) console.log(err)
5961
r.headers.Authorization = "bearer " + token
60-
request.post(r, report)
62+
request.post(r, this_report)
6163
})
6264
} else {
63-
request.post(r, report)
65+
request.post(r, this_report)
6466
}
6567
}
6668
}
6769

68-
function report(err, data){
70+
function report(to_open, err, data){
6971
// TODO open browser automatically
7072
console.log("url: ", data.body.html_url)
73+
if (to_open) {
74+
console.log("Opening the page in your browser")
75+
open(data.body.html_url)
76+
}
7177
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"dependencies": {
2323
"commander": "~1.1.1",
2424
"request": "~2.20.0",
25-
"ap": "~0.2.0"
25+
"ap": "~0.2.0",
26+
"temp": "~0.5.0"
2627
},
2728
"preferGlobal": "true",
2829
"bin": {

0 commit comments

Comments
 (0)