daily pastebin goal
19%
SHARE
TWEET

Untitled

a guest Jul 22nd, 2018 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. fs            = require 'fs'
  2. os            = require 'os'
  3. path          = require 'path'
  4. https         = require 'https'
  5. querystring   = require 'querystring'
  6. {exec, spawn} = require 'child_process'
  7. opt           = require('optimist')
  8.               .usage('Usage: ngist [options] [file.ext] [file...]')
  9.               .argv
  10.  
  11. run = ->
  12.   return usage() if opt.h or opt.help
  13.   return file() if opt.f or opt.file
  14.  
  15.  
  16.   gist =
  17.     'file_ext[gistfile1]': (file.match(/([^\/\\]+)\.(\w+)$/))[2] or ''
  18.     'file_name[gistfile1]': file or ''
  19.    
  20.   fs.readFile file, 'utf8', (er, data) ->
  21.     unless er
  22.       gist['file_contents[gistfile1]'] = data
  23.       callback gist
  24.     else
  25.       switch os.type()
  26.         when 'Darwin' then clipboard = 'pbpaste'
  27.         when 'Linux' then clipboard = 'xclip -out -selection clipboard'
  28.    
  29.       exec clipboard, (err, stdout) ->
  30.         gist['file_contents[gistfile1]'] = stdout
  31.         callback gist
  32.    
  33. getCredentials = (callback) ->
  34.   exec "git config --global github.user", (err, stdout, stderr) ->
  35.     unless err
  36.       user = stdout or ''
  37.       exec "git config --global github.token", (err, stdout, stderr) ->
  38.         token = stdout or ''
  39.         callback user, token
  40.      
  41. setCredentials = ->
  42.   exec "git config --global github.user #{opt.u or opt.user}", (err, stdout, stderr) ->
  43.     unless err
  44.       exec "git config --global github.token #{opt.t or opt.token}", (err, stdout, stderr) ->
  45.         gist['login'] = opt.u or opt.user
  46.         gist['token'] = opt.t or opt.token
  47.         post()
  48.    
  49. post = (gist) ->
  50.   gistData = querystring.stringify gist
  51.   options =
  52.     host: 'gist.github.com'
  53.     port: 443
  54.     path: '/gists'
  55.     method: 'POST'
  56.     headers:
  57.       'Content-Type': 'application/x-www-form-urlencoded'
  58.       'Content-Length': gistData.length
  59.  
  60.   req = https.request options, (res) ->
  61.     console.log res.statusCode
  62.     console.log JSON.stringify res.headers
  63.    
  64.     results = ''
  65.     res.on 'data', (d) ->
  66.       results += d
  67.  
  68.     res.on 'end', ->
  69.       console.log results
  70.    
  71.   req.end gistData
  72.  
  73. file = ->
  74.   gist = {}
  75.   files = []
  76.   files.push opt.f or opt.file
  77.   if opt._
  78.     for i of opt._
  79.       files.push opt._[i]
  80.      
  81.   for file, i in files
  82.     path.exists file, (exists) ->
  83.       throw new Error "File not found: #{file}" unless exists
  84.       fs.readFile file, 'utf8', (er, data) ->
  85.         gist["file_ext[gistfile#{i}]"] = path.extname file
  86.         gist["file_name[gistfile#{i}]"] = path.basename file
  87.         gist["file_contents[gistfile#{i}]"] = data
  88.         post gist
  89.        
  90.  
  91. usage = ->
  92.   console.log '''
  93.  
  94.               Usage: ngist [options] [-f file.ext] [files...]
  95.              
  96.               -f, --file         use file(s) for gist
  97.               -c, --clip         use clipboard for gist
  98.               -p, --private      make private gist
  99.               -l, --login        github.com username
  100.               -t, --token        github.com API token
  101.               -h, --help         display this help message
  102.              
  103.               '''
  104.  
  105. run()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top