Skip to content

Instantly share code, notes, and snippets.

@lukecampbell
Last active December 14, 2015 17:09
Show Gist options
  • Save lukecampbell/2492264 to your computer and use it in GitHub Desktop.
Save lukecampbell/2492264 to your computer and use it in GitHub Desktop.
utility scripts
#!/usr/bin/env ruby
couchdb = "http://localhost:5984/"
if ARGV[0] == 'admin'
couchdb = couchdb + '_utils/index.html'
exec 'open ' + couchdb
elsif ARGV[0] == 'wipe'
require 'couchrest'
cr = CouchRest.new('http://localhost:5984')
cr.databases.each do |db_name|
cr.database(db_name).delete!
end
end
#!/usr/bin/env python
from elasticpy import *
import json
import urllib2
def wipe(host, port):
es = ElasticSearch()
indices = es.index_list()
for index in indices:
es.index_delete(index)
if __name__ == '__main__':
import sys
if len(sys.argv) > 2:
wipe(sys.argv[1],sys.argv[2])
elif len(sys.argv) == 2 and sys.argv[1]=="head":
import subprocess
subprocess.call("open http://localhost:9200/_plugin/head/".split())
elif len(sys.argv) == 2 and sys.argv[1]=='bigdesk':
import subprocess
subprocess.call("open http://localhost:9200/_plugin/bigdesk/".split())
elif len(sys.argv) == 2 and sys.argv[1]=="wipe":
wipe('localhost','9200')
#!/usr/bin/env ruby
#
require 'slop'
require 'git'
class GitHub
def initialize(remote=nil, branch=nil, url='https://github.com/')
if File.directory? '.git'
@g = Git.open('./')
else
git_dir = `cat .git`.split('gitdir: ')[1].chomp()
@g = Git.open('./', :repository => git_dir, :index => git_dir + '/index')
end
@remote = @g.remote(remote || 'origin')
if @g.branches.to_a.any? {|b| b.name == branch}
@branch = @g.branch(branch)
else
@branch = @g.branch(@g.current_branch)
end
@url = url
end
def compare(end_branch=nil, begin_branch=nil)
if not end_branch
end_branch = @branch.name
end
if not begin_branch
url = self._remote + 'compare/' + end_branch
else
c_end = @g.branch(end_branch).gcommit.sha
c_begin = @g.branch(begin_branch).gcommit.sha
url = self._remote + 'compare/' + c_begin + '...' + c_end
end
return url
end
def file(file, line1=nil, line2=nil)
url = self._remote + 'blob/' + @branch.name + '/' + file
if line1
url += '#L' + line1.to_s
end
if line2
url += '-' + line2.to_s
end
return url
end
def pulls(num=nil)
return self.pull(num)
end
def pull(num=nil)
url = self._remote + 'pulls/'
if num
url = self._remote + 'pull/' + num.to_s
end
return url
end
def show(commit)
if commit and commit.include? 'HEAD'
commit = `git rev-parse #{commit}`
end
commit = commit || @branch.gcommit.sha
url = self._remote + 'commit/' + commit
return url
end
def remote()
url = self._remote
return url
end
def _remote()
remote_url = @g.remote(@remote).url
if remote_url.include? '@'
remote_url = @url + remote_url.split(':')[1]
elsif remote_url.include? 'github.com/'
remote_url = @url + remote_url.split('github.com/')[1]
else
puts 'Failed to determine _remote'
end
remote_url = remote_url.sub('.git','/')
if not remote_url.end_with? '/'
remote_url += '/'
end
return remote_url
end
def _branch()
@branch
end
def branch()
remote = self._remote
if remote[-1] != '/'
url = self._remote + '/tree/' + @branch.name
else
url = self._remote + 'tree/' + @branch.name
end
return url
end
def blob(file, line1=nil, line2=nil)
commit = @branch.gcommit.sha
url = self._remote + 'blob/' + commit.to_s + '/' + file
if line1
url += '#L' + line1.to_s
end
if line2
url += '-' + line2.to_s
end
return url
end
end
# pattern is http://github.com/<user>/<repo>/
# args: remote/repo branch
#
opts = Slop.parse do
banner = 'github [-b branch] [-r remote] <command>'
on :b, :branch, 'Branch Name', :argument=>:optional
on :r, :remote, 'Remote name', :argument=>:required
on :c, :compare,'Compare Branch', :argument=>:optional
on :f, :file, 'Show File', :argument=>:required
on :l, :line, 'Line Number', :argument=>:required
on :n, :endline,'Ending Line Number', :argument=>:required
on :s, :show, 'Show Commit', :argument=>:optional
on :p, :pull, 'Pull requests', :argument=>:optional
on :o, :blob, 'Blob', :argument=>:required
end
gh = GitHub.new(opts[:remote], opts[:branch])
url = gh.remote
if opts[:blob]
url = gh.blob(opts[:blob], opts[:line], opts[:endline])
elsif opts[:file]
url = gh.file(opts[:file], opts[:line], opts[:endline])
elsif opts.pull?
url = gh.pull(opts[:pull])
elsif opts.compare?
url = gh.compare(opts[:compare])
elsif opts.show?
url = gh.show(opts[:show])
elsif opts.branch?
url = gh.branch
else
url = gh.remote
end
puts url
exec 'open ' + url
#!/bin/bash
TMPFILE=`mktemp /tmp/diff.XXXXXX`
git diff $PWD > $TMPFILE
mvim $TMPFILE
#!/usr/bin/env python
url = 'http://localhost:8080'
from sh import open
def gmap():
return url + '/map'
def reslist(type_):
return url + '/list/%s' % type_
def view(res_id):
return url + '/view/%s' % res_id
def usage():
print 'pyonweb [mode] [param]'
print ' map - Loads the gmap interface'
print ' list <ResourceType> - Loads a list of resources with the specified type.'
print ' view <ResourceId> - Loads the resource page for the specified resource.'
if __name__ == '__main__':
open_url = url
import sys
if len(sys.argv) > 1:
if sys.argv[1] == 'map':
open_url = gmap()
open(open_url)
elif sys.argv[1] == 'list':
open_url = reslist(sys.argv[2])
open(open_url)
elif sys.argv[1] == 'view':
open_url = view(sys.argv[2])
open(open_url)
else:
usage()
else:
open(open_url)
#!/usr/bin/env python
import requests
from distutils.version import StrictVersion
url = 'http://localhost:55672'
def wipe():
r = requests.get('%s/api/queues'%url, auth=('guest','guest'), headers={'content-type':'application/json; charset utf-8'})
if StrictVersion(requests.__version__) < StrictVersion('1.0.0'):
queues = [i['name'] for i in r.json]
else:
queues = [i['name'] for i in r.json()]
r = requests.get('%s/api/exchanges'%url, auth=('guest','guest'), headers={'content-type':'application/json; charset utf-8'})
if StrictVersion(requests.__version__) < StrictVersion('1.0.0'):
exchanges = [i['name'] for i in r.json]
else:
exchanges = [i['name'] for i in r.json()]
for queue in queues:
requests.delete('%s/api/queues/%%2f/%s'%(url,queue), auth=('guest','guest'))
print 'Queue %s deleted' % queue
for exchange in exchanges:
requests.delete('%s/api/exchanges/%%2f/%s'%(url,exchange), auth=('guest','guest'))
print 'Exchange %s deleted' % exchange
if __name__ == '__main__':
import sys
import subprocess
try :
if sys.argv[1] == 'queue':
queue_name = sys.argv[2]
args = 'open %s/#/queues/%%2F/%s'%(url,queue_name)
subprocess.call(args.split())
elif sys.argv[1] == 'exchange':
exchange_name = sys.argv[2]
args = 'open %s/#/exchanges/%%2F/%s'%(url,exchange_name)
subprocess.call(args.split())
elif sys.argv[1] == 'wipe':
wipe()
elif sys.argv[1] == 'admin':
args = 'open %s/#/' % url
subprocess.call(args.split())
except IndexError:
args = 'open %s/#/' % url
subprocess.call(args.split())
#!/usr/bin/env ruby
#
if not ARGV.length
puts 'Usage: wiki [query]'
else
query = ARGV.join(' ')
query = query.split().join('+')
url = 'open "http://en.wikipedia.org/wiki/Special:Search?\&search=' + query + '"'
puts url
exec url
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment