Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fs = require 'fs'
- os = require 'os'
- path = require 'path'
- https = require 'https'
- querystring = require 'querystring'
- {exec, spawn} = require 'child_process'
- opt = require('optimist')
- .usage('Usage: ngist [options] [file.ext] [file...]')
- .argv
- run = ->
- return usage() if opt.h or opt.help
- return file() if opt.f or opt.file
- gist =
- 'file_ext[gistfile1]': (file.match(/([^\/\\]+)\.(\w+)$/))[2] or ''
- 'file_name[gistfile1]': file or ''
- fs.readFile file, 'utf8', (er, data) ->
- unless er
- gist['file_contents[gistfile1]'] = data
- callback gist
- else
- switch os.type()
- when 'Darwin' then clipboard = 'pbpaste'
- when 'Linux' then clipboard = 'xclip -out -selection clipboard'
- exec clipboard, (err, stdout) ->
- gist['file_contents[gistfile1]'] = stdout
- callback gist
- getCredentials = (callback) ->
- exec "git config --global github.user", (err, stdout, stderr) ->
- unless err
- user = stdout or ''
- exec "git config --global github.token", (err, stdout, stderr) ->
- token = stdout or ''
- callback user, token
- setCredentials = ->
- exec "git config --global github.user #{opt.u or opt.user}", (err, stdout, stderr) ->
- unless err
- exec "git config --global github.token #{opt.t or opt.token}", (err, stdout, stderr) ->
- gist['login'] = opt.u or opt.user
- gist['token'] = opt.t or opt.token
- post()
- post = (gist) ->
- gistData = querystring.stringify gist
- options =
- host: 'gist.github.com'
- port: 443
- path: '/gists'
- method: 'POST'
- headers:
- 'Content-Type': 'application/x-www-form-urlencoded'
- 'Content-Length': gistData.length
- req = https.request options, (res) ->
- console.log res.statusCode
- console.log JSON.stringify res.headers
- results = ''
- res.on 'data', (d) ->
- results += d
- res.on 'end', ->
- console.log results
- req.end gistData
- file = ->
- gist = {}
- files = []
- files.push opt.f or opt.file
- if opt._
- for i of opt._
- files.push opt._[i]
- for file, i in files
- path.exists file, (exists) ->
- throw new Error "File not found: #{file}" unless exists
- fs.readFile file, 'utf8', (er, data) ->
- gist["file_ext[gistfile#{i}]"] = path.extname file
- gist["file_name[gistfile#{i}]"] = path.basename file
- gist["file_contents[gistfile#{i}]"] = data
- post gist
- usage = ->
- console.log '''
- Usage: ngist [options] [-f file.ext] [files...]
- -f, --file use file(s) for gist
- -c, --clip use clipboard for gist
- -p, --private make private gist
- -l, --login github.com username
- -t, --token github.com API token
- -h, --help display this help message
- '''
- run()
Add Comment
Please, Sign In to add comment