-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitHub.zig
More file actions
263 lines (223 loc) · 8.88 KB
/
Copy pathGitHub.zig
File metadata and controls
263 lines (223 loc) · 8.88 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
const std = @import("std");
const print = std.debug.print;
const builtin = @import("builtin");
const native_os = builtin.os.tag;
const BASE_URL = "https://api.github.com/users";
const USER_AGENT = "io.github.Miqueas.Github-REST-API-Examples.Zig/1.0";
pub fn get(allocator: std.mem.Allocator, url: []const u8) !std.json.Parsed(std.json.Value) {
var threaded = std.Io.Threaded.init_single_threaded;
defer threaded.deinit();
const io = threaded.io();
var client = std.http.Client { .allocator = allocator, .io = io };
defer client.deinit();
var allocating = std.Io.Writer.Allocating.init(allocator);
defer allocating.deinit();
const response = try client.fetch(.{
.method = .GET,
.headers = .{ .user_agent = .{ .override = USER_AGENT } },
.location = .{ .url = url },
.response_writer = &allocating.writer,
});
const body = try allocating.toOwnedSlice();
defer allocator.free(body);
if (response.status != .ok) {
print("Failed to fetch data: HTTP {d}\n", .{ response.status });
@panic(body);
}
return std.json.parseFromSlice(std.json.Value, allocator, body, .{ .parse_numbers = true });
}
const GithubUserDataKind = enum {
repos,
gists,
followers,
following,
pub fn key(self: GithubUserDataKind) []const u8 {
return switch (self) {
.repos => "name",
.gists => "description",
else => "login",
};
}
};
const GithubUserData = struct {
count: i64,
data: std.ArrayListAligned([]const u8, null),
};
const GithubUser = struct {
username: []const u8,
name: []const u8,
bio: []const u8,
link: []const u8,
repos: GithubUserData,
gists: GithubUserData,
followers: GithubUserData,
following: GithubUserData,
fn new(alloc: std.mem.Allocator, username: []const u8) !GithubUser {
const username_owned = try alloc.dupe(u8, username);
errdefer alloc.free(username_owned);
return .{
.username = username_owned,
.name = "",
.bio = "",
.link = "",
.repos = .{ .count = 0, .data = .empty, },
.gists = .{ .count = 0, .data = .empty, },
.followers = .{ .count = 0, .data = .empty, },
.following = .{ .count = 0, .data = .empty, },
};
}
pub fn create(alloc: std.mem.Allocator, username: []const u8) !GithubUser {
var this = try GithubUser.new(alloc, username);
errdefer this.deinit(alloc);
const url = try std.fmt.allocPrint(alloc, "{s}/{s}", .{ BASE_URL, this.username });
const parsed = try get(alloc, url);
const json = parsed.value.object;
defer {
alloc.free(url);
parsed.deinit();
}
this.name = try alloc.dupe(u8, json.get("name").?.string);
this.bio = try alloc.dupe(u8, json.get("bio").?.string);
this.link = try alloc.dupe(u8, json.get("html_url").?.string);
this.repos.count = json.get("public_repos").?.integer;
this.gists.count = json.get("public_gists").?.integer;
this.followers.count = json.get("followers").?.integer;
this.following.count = json.get("following").?.integer;
return this;
}
pub fn deinit(self: *GithubUser, alloc: std.mem.Allocator) void {
alloc.free(self.username);
alloc.free(self.name);
alloc.free(self.bio);
alloc.free(self.link);
for (self.repos.data.items) |data| alloc.free(data);
self.repos.data.deinit(alloc);
for (self.gists.data.items) |data| alloc.free(data);
self.gists.data.deinit(alloc);
for (self.followers.data.items) |data| alloc.free(data);
self.followers.data.deinit(alloc);
for (self.following.data.items) |data| alloc.free(data);
self.following.data.deinit(alloc);
}
pub fn fetch(self: *GithubUser, alloc: std.mem.Allocator, thing: GithubUserDataKind) !void {
const url = try std.fmt.allocPrint(alloc, "{s}/{s}/{s}", .{
BASE_URL,
self.username,
std.enums.tagName(GithubUserDataKind, thing).?,
});
const parsed = try get(alloc, url);
const json = parsed.value.array;
defer {
alloc.free(url);
parsed.deinit();
}
for (json.items) |item| {
const data = item.object.get(thing.key()).?.string;
const owned = try alloc.dupe(u8, data);
switch (thing) {
.repos => try self.repos.data.append(alloc, owned),
.gists => try self.gists.data.append(alloc, owned),
.followers => try self.followers.data.append(alloc, owned),
.following => try self.following.data.append(alloc, owned),
}
}
}
};
const CommandLine = struct {
help: bool,
repos: bool,
gists: bool,
followers: bool,
following: bool,
usernames: std.ArrayListAligned([]const u8, null),
pub fn parse(alloc: std.mem.Allocator, args: std.process.Args) !CommandLine {
var this = CommandLine{
.help = false,
.repos = false,
.gists = false,
.followers = false,
.following = false,
.usernames = .empty,
};
var it = if (native_os == .windows) try args.iterateAllocator(alloc) else args.iterate();
defer it.deinit();
_ = it.next(); // Skip executable name
while (it.next()) |arg| {
if (arg[0] != '-') {
try this.usernames.append(alloc, try alloc.dupe(u8, arg));
continue;
} else {
const isLong = if (arg[1] == '-') true else false;
const flag = if (isLong) arg[2..] else arg[1..];
if (isLong) {
if (std.mem.eql(u8, flag, "help")) this.help = true;
if (std.mem.eql(u8, flag, "repos")) this.repos = true;
if (std.mem.eql(u8, flag, "gists")) this.gists = true;
if (std.mem.eql(u8, flag, "followers")) this.followers = true;
if (std.mem.eql(u8, flag, "following")) this.following = true;
} else {
for (flag) |c| {
switch (c) {
'h' => this.help = true,
'r' => this.repos = true,
'g' => this.gists = true,
'f' => this.followers = true,
'F' => this.following = true,
else => {},
}
}
}
}
}
return this;
}
pub fn deinit(self: *CommandLine, alloc: std.mem.Allocator) void {
for (self.usernames.items) |username| alloc.free(username);
self.usernames.deinit(alloc);
}
};
pub fn main(init: std.process.Init) !void {
var cmd = try CommandLine.parse(init.gpa, init.minimal.args);
defer cmd.deinit(init.gpa);
if (cmd.usernames.items.len == 0 or cmd.help) {
print("Usage: Github [options] <usernames...>\n", .{});
print("Options:\n", .{});
print(" -h, --help Show this help message\n", .{});
print(" -r, --repos Show user repositories\n", .{});
print(" -g, --gists Show user gists\n", .{});
print(" -f, --followers Show user followers\n", .{});
print(" -F, --following Show user following\n", .{});
return;
}
const rc = "\x1B[0m";
const bc = "\x1B[1;32m";
for (cmd.usernames.items) |username| {
var user = try GithubUser.create(init.gpa, username);
defer user.deinit(init.gpa);
print("{s}Username{s}: @{s}\n", .{ bc, rc, user.username });
print("{s}Name{s}: {s}\n", .{ bc, rc, user.name });
print("{s}Bio{s}: {s}\n", .{ bc, rc, user.bio });
print("{s}Link{s}: {s}\n", .{ bc, rc, user.link });
print("{s}Repos{s}: {d}\n", .{ bc, rc, user.repos.count });
if (cmd.repos) {
try user.fetch(init.gpa, .repos);
for (user.repos.data.items) |repo| print(" | {s}\n", .{ repo });
}
print("{s}Gists{s}: {d}\n", .{ bc, rc, user.gists.count });
if (cmd.gists) {
try user.fetch(init.gpa, .gists);
for (user.gists.data.items) |gist| print(" | {s}\n", .{ gist });
}
print("{s}Followers{s}: {d}\n", .{ bc, rc, user.followers.count });
if (cmd.followers) {
try user.fetch(init.gpa, .followers);
for (user.followers.data.items) |follower| print(" | @{s}\n", .{ follower });
}
print("{s}Following{s}: {d}\n", .{ bc, rc, user.following.count });
if (cmd.following) {
try user.fetch(init.gpa, .following);
for (user.following.data.items) |following| print(" | @{s}\n", .{ following });
}
print("\n", .{});
}
}