Skip to content

Commit f5b7436

Browse files
authored
Merge pull request #30 from kristapsk/sysupdates-url
Display correct current sysupdates source URL in GUI
2 parents fbba1a3 + 21eb333 commit f5b7436

File tree

5 files changed

+48
-15
lines changed

5 files changed

+48
-15
lines changed

src/comm.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ pub const Message = union(MessageTag) {
262262
slock_enabled: bool,
263263
hostname: []const u8, // see .set_nodename
264264
sysupdates: struct {
265+
url: []const u8,
265266
channel: SysupdatesChan,
266267
},
267268
};

src/nd/Config.zig

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const SYSUPDATES_CRON_SCRIPT_PATH = "/etc/cron.hourly/sysupdate";
1313
/// must be the same as https://github.com/nakamochi/sysupdates/blob/master/update.sh
1414
const SYSUPDATES_RUN_SCRIPT_NAME = "update.sh";
1515
const SYSUPDATES_RUN_SCRIPT_PATH = "/ssd/sysupdates/" ++ SYSUPDATES_RUN_SCRIPT_NAME;
16+
const SYSUPDATES_DEFAULT_URL = "https://github.com/nakamochi/sysupdates.git";
1617

1718
/// must be the same as https://github.com/nakamochi/sysupdates/tree/master/lnd
1819
pub const LND_OS_USER = "lnd";
@@ -47,6 +48,7 @@ pub const Data = struct {
4748
bcrypt_hash: []const u8, // std.crypto.bcrypt .phc format
4849
incorrect_attempts: u8, // reset after each successful unlock
4950
} = null,
51+
sysurl: []const u8,
5052
syschannel: SysupdatesChannel,
5153
syscronscript: []const u8,
5254
sysrunscript: []const u8,
@@ -105,27 +107,47 @@ fn initData(allocator: std.mem.Allocator, filepath: []const u8) !Data {
105107
}
106108

107109
fn inferData() Data {
110+
const sysupdates_channel_data = inferSysupdatesChannel(SYSUPDATES_CRON_SCRIPT_PATH);
108111
return .{
109-
.syschannel = inferSysupdatesChannel(SYSUPDATES_CRON_SCRIPT_PATH),
112+
.sysurl = sysupdates_channel_data.url,
113+
.syschannel = sysupdates_channel_data.channel,
110114
.syscronscript = SYSUPDATES_CRON_SCRIPT_PATH,
111115
.sysrunscript = SYSUPDATES_RUN_SCRIPT_PATH,
112116
};
113117
}
114118

115-
fn inferSysupdatesChannel(cron_script_path: []const u8) SysupdatesChannel {
119+
fn inferSysupdatesChannel(cron_script_path: []const u8) struct { url: []const u8, channel: SysupdatesChannel } {
116120
var buf: [1024]u8 = undefined;
117-
const bytes = std.fs.cwd().readFile(cron_script_path, &buf) catch return .master;
121+
const bytes = std.fs.cwd().readFile(cron_script_path, &buf) catch return .{ .url = SYSUPDATES_DEFAULT_URL, .channel = .master };
118122
var it = std.mem.tokenizeScalar(u8, bytes, '\n');
119-
// looking for "/ssd/sysupdates/update.sh <channel>?" where <channel> may be in quotes
123+
// looking for "/ssd/sysupdates/update.sh [<channel> [<url>]]", both may be in quotes
120124
const needle = SYSUPDATES_RUN_SCRIPT_NAME;
121125
while (it.next()) |line| {
122126
if (std.mem.indexOf(u8, line, needle)) |i| {
123127
var s = line[i + needle.len ..];
124-
s = std.mem.trim(u8, s, " \n'\"");
125-
return std.meta.stringToEnum(SysupdatesChannel, s) orelse .master;
128+
const spacepos = std.mem.indexOf(u8, s, " ");
129+
if (spacepos == null) {
130+
s = std.mem.trim(u8, s, "\n'\"");
131+
const channel = std.meta.stringToEnum(SysupdatesChannel, s) orelse return .{ .url = SYSUPDATES_DEFAULT_URL, .channel = .master };
132+
return .{ .url = SYSUPDATES_DEFAULT_URL, .channel = channel };
133+
} else {
134+
var it2 = std.mem.tokenizeScalar(u8, s, ' ');
135+
var channelstr = it2.next();
136+
if (channelstr == null) {
137+
break;
138+
}
139+
channelstr = std.mem.trim(u8, channelstr.?, "\n'\"");
140+
const channel = std.meta.stringToEnum(SysupdatesChannel, channelstr.?) orelse .master;
141+
var url = it2.next();
142+
if (url == null) {
143+
return .{ .url = SYSUPDATES_DEFAULT_URL, .channel = channel };
144+
}
145+
url = std.mem.trim(u8, url.?, "\n'\"");
146+
return .{ .url = url.?, .channel = channel };
147+
}
126148
}
127149
}
128-
return .master;
150+
return .{ .url = SYSUPDATES_DEFAULT_URL, .channel = .master };
129151
}
130152

131153
fn inferStaticData(allocator: std.mem.Allocator) !StaticData {
@@ -509,6 +531,7 @@ test "ndconfig: init existing" {
509531
defer tmp.cleanup();
510532
try tmp.dir.writeFile("conf.json",
511533
\\{
534+
\\"sysurl": "https://github.com/nakamochi/sysupdates.git",
512535
\\"syschannel": "dev",
513536
\\"syscronscript": "/cron/sysupdates.sh",
514537
\\"sysrunscript": "/sysupdates/run.sh"
@@ -546,6 +569,7 @@ test "ndconfig: dump" {
546569
.arena = &conf_arena,
547570
.confpath = confpath,
548571
.data = .{
572+
.sysurl = SYSUPDATES_DEFAULT_URL,
549573
.syschannel = .master,
550574
.syscronscript = "cronscript.sh",
551575
.sysrunscript = "runscript.sh",
@@ -579,6 +603,7 @@ test "ndconfig: switch sysupdates and infer" {
579603
.arena = &conf_arena,
580604
.confpath = confpath,
581605
.data = .{
606+
.sysurl = SYSUPDATES_DEFAULT_URL,
582607
.syschannel = .master,
583608
.syscronscript = cronscript,
584609
.sysrunscript = SYSUPDATES_RUN_SCRIPT_PATH,
@@ -591,7 +616,7 @@ test "ndconfig: switch sysupdates and infer" {
591616
const parsed = try testLoadConfigData(confpath);
592617
defer parsed.deinit();
593618
try t.expectEqual(SysupdatesChannel.dev, parsed.value.syschannel);
594-
try t.expectEqual(SysupdatesChannel.dev, inferSysupdatesChannel(cronscript));
619+
try t.expectEqual(SysupdatesChannel.dev, inferSysupdatesChannel(cronscript).channel);
595620
}
596621

597622
test "ndconfig: switch sysupdates with .run=true" {
@@ -618,6 +643,7 @@ test "ndconfig: switch sysupdates with .run=true" {
618643
.arena = conf_arena,
619644
.confpath = try tmp.join(&.{"conf.json"}),
620645
.data = .{
646+
.sysurl = SYSUPDATES_DEFAULT_URL,
621647
.syschannel = .master,
622648
.syscronscript = try tmp.join(&.{"cronscript.sh"}),
623649
.sysrunscript = try tmp.join(&.{runscript}),
@@ -653,6 +679,7 @@ test "ndconfig: genLndConfig" {
653679
.arena = conf_arena,
654680
.confpath = undefined, // unused
655681
.data = .{
682+
.sysurl = SYSUPDATES_DEFAULT_URL,
656683
.syschannel = .master, // unused
657684
.syscronscript = undefined, // unused
658685
.sysrunscript = undefined, // unused
@@ -756,6 +783,7 @@ test "ndconfig: screen lock" {
756783
const confpath = try tmp.join(&.{"conf.json"});
757784
try tmp.dir.writeFile(confpath,
758785
\\{
786+
\\"sysurl": "https://github.com/nakamochi/sysupdates.git",
759787
\\"syschannel": "dev",
760788
\\"syscronscript": "/cron/sysupdates.sh",
761789
\\"sysrunscript": "/sysupdates/run.sh"
@@ -774,6 +802,7 @@ test "ndconfig: screen lock" {
774802
try tmp.dir.writeFile(confpath,
775803
\\{
776804
\\"slock": null,
805+
\\"sysurl": "https://github.com/nakamochi/sysupdates.git",
777806
\\"syschannel": "dev",
778807
\\"syscronscript": "/cron/sysupdates.sh",
779808
\\"sysrunscript": "/sysupdates/run.sh"
@@ -793,6 +822,7 @@ test "ndconfig: screen lock" {
793822
.confpath = newpinconf,
794823
.data = .{
795824
.slock = null,
825+
.sysurl = SYSUPDATES_DEFAULT_URL,
796826
.syschannel = .master, // unused
797827
.syscronscript = undefined, // unused
798828
.sysrunscript = undefined, // unused

src/nd/Daemon.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ fn mainThreadLoopCycle(self: *Daemon) !void {
349349
.hostname = static.hostname,
350350
.slock_enabled = conf.slock != null,
351351
.sysupdates = .{
352+
.url = conf.sysurl,
352353
.channel = switch (conf.syschannel) {
353354
.dev => .edge,
354355
.master => .stable,
@@ -1457,6 +1458,7 @@ fn dummyTestConfig() !Config {
14571458
.confpath = "/dummy.conf",
14581459
.data = .{
14591460
.slock = null,
1461+
.sysurl = "",
14601462
.syschannel = .master,
14611463
.syscronscript = "",
14621464
.sysrunscript = "",

src/test/guiplay.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ fn commWriteThread(gpa: std.mem.Allocator, w: anytype) !void {
218218
const sett: comm.Message.Settings = .{
219219
.slock_enabled = state.slock_pincode != null,
220220
.hostname = state.nodename.val(),
221-
.sysupdates = .{ .channel = .edge },
221+
.sysupdates = .{ .url = "", .channel = .edge },
222222
};
223223
comm.write(gpa, w, .{ .settings = sett }) catch |err| {
224224
logger.err("{}", .{err});

src/ui/settings.zig

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ var tab: struct {
6767
card: lvgl.Card,
6868
chansel: lvgl.Dropdown,
6969
switchbtn: lvgl.TextButton,
70+
currurl: lvgl.Label,
7071
currchan: lvgl.Label,
7172
},
7273
} = undefined;
@@ -165,12 +166,10 @@ pub fn initScreenlockPanel(cont: lvgl.Container) !lvgl.Card {
165166
/// must be called only once at program startup.
166167
pub fn initSysupdatesPanel(cont: lvgl.Container) !lvgl.Card {
167168
tab.sysupdates.card = try lvgl.Card.new(cont, symbol.Loop ++ " SYSUPDATES", .{ .spinner = true });
168-
const l1 = try lvgl.Label.new(tab.sysupdates.card, "" //
169-
++ "https://github.com/nakamochi/sysupdates " // TODO: make this configurable?
170-
++ "is the source of system updates.", .{});
171-
l1.setPad(15, .top, .{});
172-
l1.setWidth(lvgl.sizePercent(100));
173-
l1.setHeightToContent();
169+
tab.sysupdates.currurl = try lvgl.Label.new(tab.sysupdates.card, cmark ++ "CURRENT SOURCE:# unknown", .{ .recolor = true });
170+
tab.sysupdates.currurl.setPad(15, .top, .{});
171+
tab.sysupdates.currurl.setWidth(lvgl.sizePercent(100));
172+
tab.sysupdates.currurl.setHeightToContent();
174173

175174
const row = try lvgl.FlexLayout.new(tab.sysupdates.card, .row, .{});
176175
row.setWidth(lvgl.sizePercent(100));
@@ -214,6 +213,7 @@ pub fn initSysupdatesPanel(cont: lvgl.Container) !lvgl.Card {
214213
pub fn update(sett: comm.Message.Settings) !void {
215214
// sysupdates channel
216215
var buf: [512]u8 = undefined;
216+
try tab.sysupdates.currurl.setTextFmt(&buf, cmark ++ "CURRENT SOURCE:# {s}", .{sett.sysupdates.url});
217217
try tab.sysupdates.currchan.setTextFmt(&buf, cmark ++ "CURRENT CHANNEL:# {s}", .{@tagName(sett.sysupdates.channel)});
218218
state.curr_sysupdates_chan = sett.sysupdates.channel;
219219

0 commit comments

Comments
 (0)