tangled
alpha
login
or
join now
altagos.dev
/
space
0
fork
atom
A SpaceTraders Agent
0
fork
atom
overview
issues
pulls
pipelines
update to latest zig@master
altagos.dev
2 months ago
f5804352
c55f888e
verified
This commit was signed with the committer's
known signature
.
altagos.dev
SSH Key Fingerprint:
SHA256:UbTjEcCZlc6GzQWLCuDK3D//HESWD2xFPkzue9XMras=
+131
-110
5 changed files
expand all
collapse all
unified
split
build.zig.zon
src
agent
config.zig
main.zig
meta
fmt.zig
st
http.zig
+2
-2
build.zig.zon
···
3
3
.version = "0.0.1",
4
4
.dependencies = .{
5
5
.known_folders = .{
6
6
-
.url = "git+https://github.com/ziglibs/known-folders.git#bafef170a73c064dc706fcfbdc2e406a35681a9c",
7
7
-
.hash = "known_folders-0.0.0-Fy-PJovNAAAtqbaXgBhV6G-Z4-WNo7P0Rov-x-npZq21",
6
6
+
.url = "git+https://github.com/ziglibs/known-folders.git?ref=master#8465ba6cfc49fe941f95c75b3ae48c03aa6125b5",
7
7
+
.hash = "known_folders-0.0.0-Fy-PJqrJAACSvJM3Ux3aJi3wu5hOUYBRvcTM-9NTelui",
8
8
},
9
9
},
10
10
.minimum_zig_version = "0.16.0-dev.1246+4b593a6c2",
+15
-6
src/agent/config.zig
···
8
8
.account = "Bearer <TOKEN>",
9
9
.agent = "Bearer <TOKEN>",
10
10
},
11
11
+
12
12
+
pub fn updateAgentToken(self: *Config, token: []const u8) void {
13
13
+
self.auth.agent = "Bearer " ++ token;
14
14
+
}
11
15
};
12
16
13
17
const log = std.log.scoped(.agent);
14
18
15
15
-
pub fn load(io: std.Io, allocator: std.mem.Allocator) !Config {
19
19
+
pub fn load(io: std.Io, allocator: std.mem.Allocator, environ: *std.process.Environ.Map) !Config {
16
20
log.debug("Loading config", .{});
17
21
18
18
-
const dir = try kf.open(io, allocator, .roaming_configuration, .{ .follow_symlinks = true }) orelse return error.NoConfigFolder;
22
22
+
const dir = try kf.open(
23
23
+
io,
24
24
+
allocator,
25
25
+
environ.*,
26
26
+
.roaming_configuration,
27
27
+
.{ .follow_symlinks = true },
28
28
+
) orelse return error.NoConfigFolder;
19
29
var file: ?std.Io.File = null;
20
30
defer if (file) |f| f.close(io);
21
31
22
22
-
_ = dir.statPath(io, "space/config.zon", .{ .follow_symlinks = true }) catch {
32
32
+
_ = dir.statFile(io, "space/config.zon", .{ .follow_symlinks = true }) catch {
23
33
file = try create(io, dir);
24
34
};
25
35
···
40
50
log.warn("Creating new config file", .{});
41
51
const config = Config{};
42
52
43
43
-
try dir.makePath(io, "space");
53
53
+
try dir.createDirPath(io, "space");
44
54
const file = try dir.createFile(io, "space/config.zon", .{ .read = true });
45
45
-
const file_old = std.fs.File.adaptFromNewApi(file);
46
55
47
56
var buffer: [64]u8 = undefined;
48
48
-
var writer = file_old.writer(&buffer);
57
57
+
var writer = file.writer(io, &buffer);
49
58
50
59
try std.zon.stringify.serializeArbitraryDepth(config, .{}, &writer.interface);
51
60
try writer.interface.flush();
+19
-11
src/main.zig
···
26
26
// _ = io;
27
27
// _ = client;
28
28
29
29
-
var fleet_f = try api.fleet.listShips(client, .{});
30
30
-
defer _ = fleet_f.cancel(io) catch {};
29
29
+
var register_f = try api.agent.register(client, "ALTAGOS", .ANCIENTS);
30
30
+
defer _ = register_f.cancel(io) catch {};
31
31
+
32
32
+
const register = try register_f.await(io);
33
33
+
defer register.deinit();
34
34
+
35
35
+
std.log.info("Registered agent\n{f}", .{pretty(register.value)});
31
36
32
32
-
const fleet = try fleet_f.await(io);
33
33
-
defer fleet.deinit();
37
37
+
// var fleet_f = try api.fleet.listShips(client, .{});
38
38
+
// defer _ = fleet_f.cancel(io) catch {};
34
39
35
35
-
std.log.info("{f}", .{pretty(fleet.value)});
40
40
+
// const fleet = try fleet_f.await(io);
41
41
+
// defer fleet.deinit();
42
42
+
43
43
+
// std.log.info("{f}", .{pretty(fleet.value)});
36
44
}
37
45
38
38
-
pub fn main() !void {
39
39
-
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
40
40
-
defer _ = gpa.deinit();
41
41
-
const allocator = gpa.allocator();
46
46
+
pub fn main(init: std.process.Init) !void {
47
47
+
const allocator = init.gpa;
42
48
43
43
-
var io_impl: std.Io.Threaded = .init(allocator);
49
49
+
var io_impl: std.Io.Threaded = .init(allocator, .{
50
50
+
.environ = init.minimal.environ,
51
51
+
});
44
52
defer io_impl.deinit();
45
53
const io = io_impl.io();
46
54
47
55
std.log.debug("Io threads: {}", .{io_impl.concurrent_limit.toInt() orelse 0});
48
56
49
49
-
const config = try agent.config.load(io, allocator);
57
57
+
const config = try agent.config.load(io, allocator, init.environ_map);
50
58
defer agent.config.free(allocator, config);
51
59
52
60
var client = Client.init(allocator, io, .{
+92
-88
src/meta/fmt.zig
···
29
29
30
30
pub fn Pretty(comptime T: type) type {
31
31
const global = struct {
32
32
-
var conf: ?std.Io.tty.Config = null;
32
32
+
var conf: ?std.Io.Terminal = null;
33
33
};
34
34
return struct {
35
35
value: T,
···
38
38
}
39
39
pub fn format(this: *const @This(), w: *std.Io.Writer) error{WriteFailed}!void {
40
40
if (global.conf == null) {
41
41
-
global.conf = .detect(.stderr());
41
41
+
var buffer: [64]u8 = undefined;
42
42
+
const stderr = std.debug.lockStderr(&buffer).terminal();
43
43
+
global.conf = stderr;
44
44
+
global.conf.?.writer = w;
45
45
+
std.debug.unlockStderr();
42
46
}
43
43
-
return innerFmt(w, global.conf.?, T, this.value, false, .{}, .{});
47
47
+
std.log.debug("", .{});
48
48
+
return innerFmt(w, global.conf.?, T, this.value, !@inComptime(), .{}, .{});
44
49
}
45
50
};
46
51
}
···
54
59
};
55
60
56
61
fn setColor(
57
57
-
w: *Io.Writer,
58
58
-
tty: Io.tty.Config,
62
62
+
tty: Io.Terminal,
59
63
color: Color,
60
64
) void {
61
65
switch (color) {
62
66
.dim => {
63
63
-
tty.setColor(w, .dim) catch {};
67
67
+
tty.setColor(.dim) catch {};
64
68
},
65
69
.reset => {
66
66
-
tty.setColor(w, .reset) catch {};
70
70
+
tty.setColor(.reset) catch {};
67
71
},
68
72
.text => {
69
69
-
// tty.setColor(w, .italic) catch {};
70
70
-
tty.setColor(w, .dim) catch {};
71
71
-
tty.setColor(w, .blue) catch {};
73
73
+
// tty.setColor(.italic) catch {};
74
74
+
tty.setColor(.dim) catch {};
75
75
+
tty.setColor(.blue) catch {};
72
76
},
73
77
.field => {
74
74
-
tty.setColor(w, .green) catch {};
78
78
+
tty.setColor(.green) catch {};
75
79
},
76
80
.value => {
77
77
-
// tty.setColor(w, .italic) catch {};
78
78
-
tty.setColor(w, .blue) catch {};
81
81
+
// tty.setColor(.italic) catch {};
82
82
+
tty.setColor(.blue) catch {};
79
83
},
80
84
}
81
85
}
82
86
83
87
fn innerFmt(
84
88
w: *std.Io.Writer,
85
85
-
tty: std.Io.tty.Config,
89
89
+
tty: std.Io.Terminal,
86
90
comptime T: type,
87
91
value: T,
88
92
comptime runtime: bool,
···
105
109
.array => |array| try printArray(w, tty, array, value, runtime, ctx, opts),
106
110
.@"struct" => |str| try printStruct(T, w, tty, str, value, runtime, ctx, opts),
107
111
else => {
108
108
-
tty.setColor(w, .red) catch {};
112
112
+
tty.setColor(.red) catch {};
109
113
try w.print("Unimplemented! ({})", .{info});
110
110
-
setColor(w, tty, .reset);
114
114
+
setColor(tty, .reset);
111
115
},
112
116
}
113
117
}
114
118
115
119
fn printBool(
116
120
w: *Io.Writer,
117
117
-
tty: Io.tty.Config,
121
121
+
tty: Io.Terminal,
118
122
value: bool,
119
123
opts: Options,
120
124
) !void {
121
125
if (opts.type_name) {
122
122
-
setColor(w, tty, .dim);
126
126
+
setColor(tty, .dim);
123
127
try w.print("bool => ", .{});
124
124
-
setColor(w, tty, .reset);
128
128
+
setColor(tty, .reset);
125
129
} else {
126
126
-
tty.setColor(w, .dim) catch {};
130
130
+
tty.setColor(.dim) catch {};
127
131
try w.print("=> ", .{});
128
128
-
setColor(w, tty, .reset);
132
132
+
setColor(tty, .reset);
129
133
}
130
130
-
setColor(w, tty, .value);
134
134
+
setColor(tty, .value);
131
135
if (value) {
132
132
-
tty.setColor(w, .bright_green) catch {};
136
136
+
tty.setColor(.bright_green) catch {};
133
137
} else {
134
134
-
tty.setColor(w, .bright_red) catch {};
138
138
+
tty.setColor(.bright_red) catch {};
135
139
}
136
140
try w.print("{}", .{value});
137
137
-
setColor(w, tty, .reset);
141
141
+
setColor(tty, .reset);
138
142
}
139
143
140
144
fn printInt(
141
145
w: *Io.Writer,
142
142
-
tty: Io.tty.Config,
146
146
+
tty: Io.Terminal,
143
147
int: Type.Int,
144
148
value: anytype,
145
149
opts: Options,
146
150
) !void {
147
151
if (opts.type_name) {
148
148
-
tty.setColor(w, .dim) catch {};
152
152
+
tty.setColor(.dim) catch {};
149
153
try w.print("{s}{} => ", .{ if (int.signedness == .signed) "i" else "u", int.bits });
150
150
-
setColor(w, tty, .reset);
154
154
+
setColor(tty, .reset);
151
155
} else {
152
152
-
tty.setColor(w, .dim) catch {};
156
156
+
tty.setColor(.dim) catch {};
153
157
try w.print("=> ", .{});
154
154
-
setColor(w, tty, .reset);
158
158
+
setColor(tty, .reset);
155
159
}
156
156
-
setColor(w, tty, .value);
160
160
+
setColor(tty, .value);
157
161
try w.print("{}", .{value});
158
158
-
setColor(w, tty, .reset);
162
162
+
setColor(tty, .reset);
159
163
}
160
164
161
165
fn printFloat(
162
166
w: *Io.Writer,
163
163
-
tty: Io.tty.Config,
167
167
+
tty: Io.Terminal,
164
168
float: Type.Float,
165
169
value: anytype,
166
170
opts: Options,
167
171
) !void {
168
172
if (opts.type_name) {
169
169
-
tty.setColor(w, .dim) catch {};
173
173
+
tty.setColor(.dim) catch {};
170
174
try w.print("f{} => ", .{float.bits});
171
171
-
setColor(w, tty, .reset);
175
175
+
setColor(tty, .reset);
172
176
} else {
173
173
-
tty.setColor(w, .dim) catch {};
177
177
+
tty.setColor(.dim) catch {};
174
178
try w.print("=> ", .{});
175
175
-
setColor(w, tty, .reset);
179
179
+
setColor(tty, .reset);
176
180
}
177
177
-
setColor(w, tty, .value);
181
181
+
setColor(tty, .value);
178
182
try w.print("{}", .{value});
179
179
-
setColor(w, tty, .reset);
183
183
+
setColor(tty, .reset);
180
184
}
181
185
182
186
fn printEnum(
183
187
w: *Io.Writer,
184
184
-
tty: Io.tty.Config,
188
188
+
tty: Io.Terminal,
185
189
value: anytype,
186
190
opts: Options,
187
191
) !void {
188
192
if (opts.type_name) {
189
189
-
tty.setColor(w, .dim) catch {};
193
193
+
tty.setColor(.dim) catch {};
190
194
try w.print("{s} => ", .{@typeName(@TypeOf(value))});
191
191
-
setColor(w, tty, .reset);
195
195
+
setColor(tty, .reset);
192
196
} else {
193
193
-
tty.setColor(w, .dim) catch {};
197
197
+
tty.setColor(.dim) catch {};
194
198
try w.print("=> ", .{});
195
195
-
setColor(w, tty, .reset);
199
199
+
setColor(tty, .reset);
196
200
}
197
197
-
setColor(w, tty, .value);
201
201
+
setColor(tty, .value);
198
202
try w.print("{}", .{value});
199
199
-
setColor(w, tty, .reset);
203
203
+
setColor(tty, .reset);
200
204
}
201
205
202
206
fn printOptional(
203
207
w: *Io.Writer,
204
204
-
tty: Io.tty.Config,
208
208
+
tty: Io.Terminal,
205
209
optional: Type.Optional,
206
210
value: anytype,
207
211
comptime runtime: bool,
···
213
217
next_opts.last_layer_type_name = opts.type_name;
214
218
215
219
if (opts.type_name) {
216
216
-
tty.setColor(w, .dim) catch {};
220
220
+
tty.setColor(.dim) catch {};
217
221
try w.print("?", .{});
218
218
-
setColor(w, tty, .reset);
222
222
+
setColor(tty, .reset);
219
223
}
220
224
221
225
if (value) |val| {
222
226
try innerFmt(w, tty, optional.child, val, runtime, ctx, next_opts);
223
227
} else {
224
228
if (opts.type_name) {
225
225
-
tty.setColor(w, .dim) catch {};
229
229
+
tty.setColor(.dim) catch {};
226
230
try w.print("{s} => ", .{@typeName(optional.child)});
227
227
-
setColor(w, tty, .reset);
231
231
+
setColor(tty, .reset);
228
232
} else {
229
229
-
tty.setColor(w, .dim) catch {};
233
233
+
tty.setColor(.dim) catch {};
230
234
try w.print("=> ", .{});
231
231
-
setColor(w, tty, .reset);
235
235
+
setColor(tty, .reset);
232
236
}
233
237
234
234
-
tty.setColor(w, .blue) catch {};
238
238
+
tty.setColor(.blue) catch {};
235
239
try w.writeAll("null");
236
236
-
setColor(w, tty, .reset);
240
240
+
setColor(tty, .reset);
237
241
}
238
242
}
239
243
240
244
fn printArray(
241
245
w: *Io.Writer,
242
242
-
tty: Io.tty.Config,
246
246
+
tty: Io.Terminal,
243
247
array: Type.Array,
244
248
value: anytype,
245
249
comptime runtime: bool,
···
253
257
next_opts.last_layer_type_name = opts.type_name;
254
258
255
259
if (opts.type_name) {
256
256
-
tty.setColor(w, .dim) catch {};
260
260
+
tty.setColor(.dim) catch {};
257
261
try w.print("[{}]{s} =>", .{ array.len, @typeName(array.child) });
258
258
-
setColor(w, tty, .reset);
262
262
+
setColor(tty, .reset);
259
263
} else {
260
260
-
tty.setColor(w, .dim) catch {};
264
264
+
tty.setColor(.dim) catch {};
261
265
try w.print("=> ", .{});
262
262
-
setColor(w, tty, .reset);
266
266
+
setColor(tty, .reset);
263
267
}
264
268
265
269
inline for (value, 0..) |item, idx| {
266
270
try indent(w, runtime, ctx);
267
271
268
268
-
setColor(w, tty, .field);
272
272
+
setColor(tty, .field);
269
273
try w.print("[{}] ", .{idx});
270
270
-
setColor(w, tty, .reset);
274
274
+
setColor(tty, .reset);
271
275
272
276
next_ctx.indent_level = ctx.indent_level + 1;
273
277
next_opts.type_name = false;
···
278
282
279
283
fn printPointer(
280
284
w: *Io.Writer,
281
281
-
tty: Io.tty.Config,
285
285
+
tty: Io.Terminal,
282
286
ptr: Type.Pointer,
283
287
value: anytype,
284
288
comptime runtime: bool,
···
292
296
switch (ptr.size) {
293
297
.one => {
294
298
if (opts.type_name) {
295
295
-
tty.setColor(w, .dim) catch {};
299
299
+
tty.setColor(.dim) catch {};
296
300
try w.print("*{s}", .{if (ptr.is_const) "const " else ""});
297
297
-
setColor(w, tty, .reset);
301
301
+
setColor(tty, .reset);
298
302
}
299
303
300
304
if (ptr.child == anyopaque or @typeInfo(ptr.child) == .@"fn")
···
304
308
},
305
309
.slice => {
306
310
if (opts.type_name) {
307
307
-
setColor(w, tty, .dim);
311
311
+
setColor(tty, .dim);
308
312
try w.print("[]{s}", .{if (ptr.is_const) "const " else ""});
309
309
-
setColor(w, tty, .reset);
313
313
+
setColor(tty, .reset);
310
314
}
311
315
312
316
if (ptr.child == u8) {
313
313
-
tty.setColor(w, .dim) catch {};
317
317
+
tty.setColor(.dim) catch {};
314
318
try w.print("{s}=>", .{if (opts.type_name) "u8 " else ""});
315
315
-
setColor(w, tty, .reset);
319
319
+
setColor(tty, .reset);
316
320
317
321
try indent(w, runtime, ctx);
318
322
319
319
-
setColor(w, tty, .text);
323
323
+
setColor(tty, .text);
320
324
try w.print("\"{s}\"", .{value});
321
321
-
setColor(w, tty, .reset);
325
325
+
setColor(tty, .reset);
322
326
return;
323
327
}
324
328
325
325
-
setColor(w, tty, .dim);
329
329
+
setColor(tty, .dim);
326
330
try w.print("{s}{s}", .{
327
331
if (opts.type_name) @typeName(ptr.child) else "",
328
332
if (opts.type_name) " =>" else "=>",
329
333
});
330
330
-
setColor(w, tty, .reset);
334
334
+
setColor(tty, .reset);
331
335
332
336
const run_ctx: Context(true) = .{ .indent_width = ctx.indent_width, .indent_level = ctx.indent_level };
333
337
var next_ctx: Context(true) = .{ .indent_width = ctx.indent_width, .indent_level = ctx.indent_level };
···
336
340
for (value, 0..) |item, idx| {
337
341
try indent(w, true, run_ctx);
338
342
339
339
-
setColor(w, tty, .field);
343
343
+
setColor(tty, .field);
340
344
try w.print("[{}] ", .{idx});
341
341
-
setColor(w, tty, .reset);
345
345
+
setColor(tty, .reset);
342
346
343
347
next_ctx.indent_level = ctx.indent_level + 1;
344
348
next_opts.type_name = false;
···
348
352
}
349
353
350
354
if (count == 0) {
351
351
-
setColor(w, tty, .dim);
355
355
+
setColor(tty, .dim);
352
356
try w.writeAll(" empty");
353
353
-
setColor(w, tty, .reset);
357
357
+
setColor(tty, .reset);
354
358
}
355
359
},
356
360
else => {
···
362
366
fn printStruct(
363
367
comptime T: type,
364
368
w: *Io.Writer,
365
365
-
tty: Io.tty.Config,
369
369
+
tty: Io.Terminal,
366
370
str: Type.Struct,
367
371
value: anytype,
368
372
comptime runtime: bool,
···
373
377
next_opts.type_name = if (!opts.type_name and !opts.last_layer_type_name) false else true;
374
378
next_opts.last_layer_type_name = opts.type_name;
375
379
376
376
-
setColor(w, tty, .dim);
380
380
+
setColor(tty, .dim);
377
381
if (opts.type_name) {
378
382
try w.print("{s} =>", .{@typeName(T)});
379
383
} else {
380
384
try w.print("=>", .{});
381
385
}
382
382
-
setColor(w, tty, .reset);
386
386
+
setColor(tty, .reset);
383
387
384
388
if (runtime) {
385
389
var next_ctx: Context(true) = ctx;
···
387
391
inline for (str.fields) |field| {
388
392
try indent(w, runtime, ctx);
389
393
390
390
-
setColor(w, tty, .field);
394
394
+
setColor(tty, .field);
391
395
try w.print(".{s}", .{field.name});
392
392
-
setColor(w, tty, .reset);
396
396
+
setColor(tty, .reset);
393
397
394
394
-
setColor(w, tty, .dim);
398
398
+
setColor(tty, .dim);
395
399
if (opts.type_name) {
396
400
try w.writeAll(": ");
397
401
} else try w.writeByte(' ');
398
398
-
setColor(w, tty, .reset);
402
402
+
setColor(tty, .reset);
399
403
400
404
next_ctx.indent_level = ctx.indent_level + 1;
401
405
···
415
419
inline for (str.fields) |field| {
416
420
try indent(w, runtime, ctx);
417
421
418
418
-
setColor(w, tty, .field);
422
422
+
setColor(tty, .field);
419
423
try w.print(".{s}", .{field.name});
420
420
-
setColor(w, tty, .reset);
424
424
+
setColor(tty, .reset);
421
425
422
422
-
setColor(w, tty, .dim);
426
426
+
setColor(tty, .dim);
423
427
if (opts.type_name) {
424
428
try w.writeAll(": ");
425
429
} else try w.writeByte(' ');
426
426
-
setColor(w, tty, .reset);
430
430
+
setColor(tty, .reset);
427
431
428
432
next_ctx.indent_level = ctx.indent_level + 1;
429
433
+3
-3
src/st/http.zig
···
10
10
const log = std.log.scoped(.SpaceTraders);
11
11
12
12
const Semaphore = struct {
13
13
-
mutex: Io.Mutex = .{ .state = .unlocked },
14
14
-
cond: Io.Condition = .{},
13
13
+
mutex: Io.Mutex = .init,
14
14
+
cond: Io.Condition = .init,
15
15
/// It is OK to initialise this field to any value.
16
16
permits: u64 = 0,
17
17
···
56
56
duration: i64,
57
57
time: ?Io.Timestamp,
58
58
59
59
-
mutex: Io.Mutex = .{ .state = .unlocked },
59
59
+
mutex: Io.Mutex = .init,
60
60
semaphor: Semaphore,
61
61
62
62
pub fn init(opts: struct { points: u54 = 2, duration: i64 = 1000 }) Limiter {