forked from turnkeylinux/octohub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.py
executable file
·129 lines (104 loc) · 3.67 KB
/
cmd.py
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python
# Copyright (c) 2013 Alon Swartz <[email protected]>
#
# This file is part of OctoHub.
#
# OctoHub is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
"""
OctoHub: GitHub API CLI
Arguments:
method Request HTTP method (e.g., GET, POST, DELETE, ...)
uri Request URI (e.g., /user/issues)
key=val Request params key=value pairs (e.g., filter=assigned)
Options:
--input <file> Path to json encoded file for data (- for stdin)
--max-pages <int> Maximum pagination calls (only GET method supported)
For all pages, specify 0
--preview <key> Enable Github API "preview" features
See the Github API Doc for the feature "key" (custom media type)
e.g. --preview=drax will enable the "FLOSS License Use" API
Environment:
OCTOHUB_TOKEN GitHub personal access token
OCTOHUB_LOGLEVEL Log level debugging sent to stderr
Example usage:
octohub GET /users/:user
octohub GET /user/issues filter=assigned labels=bug
octohub GET /repos/:owner/:repo/issues
octohub GET /repos/:owner/:repo/issues sort=updated --max-pages=3
octohub POST /repos/:owner/:repo/issues --input=issue.json
octohub POST /user/repos --input=repo.json
cat repo.json | octohub POST /orgs/:org/repos --input=-
https://developer.github.com/v3/
"""
import os
import sys
import getopt
import simplejson as json
from octohub.connection import Connection, Pager
from octohub.exceptions import ResponseError
def fatal(e):
print >> sys.stderr, 'Error: ' + str(e)
sys.exit(1)
def usage(e=None):
if e:
print >> sys.stderr, 'Error: ' + str(e)
cmd = os.path.basename(sys.argv[0])
print >> sys.stderr, 'Syntax: %s method uri [arg=val...]' % cmd
print >> sys.stderr, __doc__.strip()
sys.exit(1)
def main():
try:
opts, args = getopt.gnu_getopt(
sys.argv[1:], 'h',
['help', 'input=', 'max-pages=', 'preview='])
except getopt.GetoptError, e:
usage(e)
data = None
max_pages = None
preview_key = None
for opt, val in opts:
if opt in ('-h', '--help'):
usage()
elif opt == '--input':
if val == '-':
data = sys.stdin
else:
data = file(val, 'r')
elif opt == '--max-pages':
max_pages = int(val)
elif opt == '--preview':
preview_key = val
if len(args) == 0:
usage()
if len(args) < 2:
usage('incorrect number of arguments')
method = args[0]
uri = args[1]
if max_pages is not None and method != 'GET':
fatal('--max-pages is only supported with method GET')
params = {}
for arg in args[2:]:
key, val = arg.split('=')
params[key] = val
token = os.environ.get('OCTOHUB_TOKEN', None)
headers = {}
if preview_key:
headers['Accept'] = 'application/vnd.github.%s-preview+json' % preview_key
conn = Connection(token, headers=headers)
try:
if max_pages is None:
response = conn.send(method, uri, params, data)
print json.dumps(response.parsed, indent=1)
else:
parsed = []
pager = Pager(conn, uri, params, max_pages=max_pages)
for response in pager:
parsed.extend(response.parsed)
print json.dumps(parsed, indent=1),
except ResponseError, e:
fatal(e)
if __name__ == '__main__':
main()