-
Notifications
You must be signed in to change notification settings - Fork 27
/
octohub_cmd.py
executable file
·123 lines (95 loc) · 3.29 KB
/
octohub_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
#!/usr/bin/python3
# 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
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=-
http://developer.github.com/v3/
"""
import os
import sys
import getopt
import json
from octohub.connection import Connection, Pager
from octohub.exceptions import ResponseError
def fatal(e):
print('Error: ' + str(e), file=sys.stderr)
sys.exit(1)
def usage(e=None):
if e:
print('Error: ' + str(e), file=sys.stderr)
cmd = os.path.basename(sys.argv[0])
print('Syntax: %s method uri [arg=val...]' % cmd, file=sys.stderr)
print(__doc__.strip(), file=sys.stderr)
sys.exit(1)
def main():
try:
opts, args = getopt.gnu_getopt(sys.argv[1:], 'h',
['help', 'input=', 'max-pages='])
except getopt.GetoptError as e:
usage(e)
data = None
max_pages = None
for opt, val in opts:
if opt in ('-h', '--help'):
usage()
elif opt == '--input':
if val == '-':
data = sys.stdin
else:
with open(val, 'r') as fob:
data = json.load(fob)
elif opt == '--max-pages':
max_pages = int(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)
conn = Connection(token)
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), end=' ')
except ResponseError as e:
fatal(e)
if __name__ == '__main__':
main()