-
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
// Copyright 2025 Shota FUJI
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
//! This script downloads linuxdeploy AppImage file and make it executable.
//! Returns an error code if it has no download option for the specified
//! CPU architecture or version.
const std = @import("std");
const download_url_tmpl = "https://github.com/linuxdeploy/linuxdeploy/releases/download/{s}/linuxdeploy-{s}.AppImage";
const ExitCode = enum(u8) {
ok = 0,
generic_error = 1,
incorrect_usage = 2,
unsupported_cpu_arch = 3,
download_failed = 4,
pub inline fn to_u8(self: @This()) u8 {
return @intFromEnum(self);
}
};
pub fn main() !u8 {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const allocator = arena.allocator();
var args = try std.process.ArgIterator.initWithAllocator(allocator);
defer args.deinit();
var arg_cpu_arch: ?std.Target.Cpu.Arch = null;
var arg_version: ?[]const u8 = null;
var arg_out: ?[]const u8 = null;
// Skip exe name
_ = args.skip();
arg_loop: while (args.next()) |arg| {
var iter = std.mem.splitScalar(u8, arg, '=');
const key = iter.next() orelse {
continue;
};
const value = iter.next() orelse {
std.log.err("Arg must be --key=value format (parsing {s})", .{key});
return ExitCode.incorrect_usage.to_u8();
};
if (std.mem.eql(u8, "--tool-version", key)) {
arg_version = value;
continue;
}
if (std.mem.eql(u8, "--out", key)) {
arg_out = value;
continue;
}
if (std.mem.eql(u8, "--cpu-arch", key)) {
inline for (@typeInfo(std.Target.Cpu.Arch).@"enum".fields) |field| {
if (std.mem.eql(u8, value, field.name)) {
arg_cpu_arch = @enumFromInt(field.value);
continue :arg_loop;
}
}
std.log.err("Unknown CPU arch: {s}", .{value});
return ExitCode.incorrect_usage.to_u8();
}
std.log.err("Unknown arg: {s}", .{key});
return ExitCode.incorrect_usage.to_u8();
}
const cpu_arch = arg_cpu_arch orelse {
std.log.err("--cpu-arch is required", .{});
return ExitCode.incorrect_usage.to_u8();
};
const version = arg_version orelse {
std.log.err("--tool-version is required", .{});
return ExitCode.incorrect_usage.to_u8();
};
const out = arg_out orelse {
std.log.err("--out is required", .{});
return ExitCode.incorrect_usage.to_u8();
};
const upstream_arch = switch (cpu_arch) {
.x86_64 => "x86_64",
.aarch64, .aarch64_be => "aarch64",
.arm => "armhf",
.x86 => "i386",
else => {
std.log.err("linuxdeploy cannot run on {s}", .{@tagName(cpu_arch)});
return ExitCode.unsupported_cpu_arch.to_u8();
},
};
var download_url = std.ArrayList(u8).init(allocator);
defer download_url.deinit();
try std.fmt.format(download_url.writer(), download_url_tmpl, .{ version, upstream_arch });
var http_client = std.http.Client{ .allocator = allocator };
defer http_client.deinit();
var http_resp = std.ArrayList(u8).init(allocator);
defer http_resp.deinit();
const result = try http_client.fetch(.{
.location = .{ .url = download_url.items },
.method = .GET,
.keep_alive = false,
.response_storage = .{ .dynamic = &http_resp },
.max_append_size = std.math.maxInt(u32),
});
if (result.status != .ok) {
std.log.err("Download failed: {s} ({d})", .{ @tagName(result.status), result.status });
return ExitCode.download_failed.to_u8();
}
const file = try std.fs.createFileAbsolute(out, .{ .mode = 0o755 });
defer file.close();
try file.writeAll(http_resp.items);
return ExitCode.ok.to_u8();
}