@@ -131,7 +131,7 @@ fn ignoreHttpCallback(request: []const u8) void {
131131 _ = request ;
132132}
133133
134- fn allocInstallDirStringXdg (allocator : Allocator ) ! []const u8 {
134+ fn allocInstallDirStringXdg (allocator : Allocator ) error { AlreadyReported } ! []const u8 {
135135 // see https://specifications.freedesktop.org/basedir-spec/latest/#variables
136136 // try $XDG_DATA_HOME/zigup first
137137 xdg_var : {
@@ -141,7 +141,7 @@ fn allocInstallDirStringXdg(allocator: Allocator) ![]const u8 {
141141 std .log .err ("$XDG_DATA_HOME environment variable '{s}' is not an absolute path" , .{xdg_data_home });
142142 return error .AlreadyReported ;
143143 }
144- return std .fs .path .join (allocator , &[_ ][]const u8 { xdg_data_home , "zigup" });
144+ return std .fs .path .join (allocator , &[_ ][]const u8 { xdg_data_home , "zigup" }) catch | e | oom ( e ) ;
145145 }
146146 // .. then fallback to $HOME/.local/share/zigup
147147 const home = std .posix .getenv ("HOME" ) orelse {
@@ -152,14 +152,47 @@ fn allocInstallDirStringXdg(allocator: Allocator) ![]const u8 {
152152 std .log .err ("$HOME environment variable '{s}' is not an absolute path" , .{home });
153153 return error .AlreadyReported ;
154154 }
155- return std .fs .path .join (allocator , &[_ ][]const u8 { home , ".local" , "share" , "zigup" });
155+ return std .fs .path .join (allocator , &[_ ][]const u8 { home , ".local" , "share" , "zigup" }) catch | e | oom ( e ) ;
156156}
157157
158- fn allocInstallDirString (allocator : Allocator ) ! []const u8 {
158+ fn getSettingsDir (allocator : Allocator ) ? []const u8 {
159+ return std .fs .getAppDataDir (allocator , "zigup" ) catch | err | switch (err ) {
160+ error .OutOfMemory = > | e | oom (e ),
161+ error .AppDataDirUnavailable = > return null ,
162+ };
163+ }
164+
165+ fn readInstallDir (allocator : Allocator ) ! ? []const u8 {
166+ const settings_dir = getSettingsDir (allocator ) orelse return null ;
167+ defer allocator .free (settings_dir );
168+
169+ const setting_path = std .fs .path .join (
170+ allocator ,
171+ &.{ settings_dir , "install-dir" },
172+ ) catch | e | oom (e );
173+ defer allocator .free (setting_path );
174+ var file = std .fs .cwd ().openFile (setting_path , .{}) catch | err | switch (err ) {
175+ error .FileNotFound = > return null ,
176+ else = > | e | {
177+ std .log .err ("open '{s}' failed with {s}" , .{ setting_path , @errorName (e ) });
178+ return error .AlreadyReported ;
179+ },
180+ };
181+ defer file .close ();
182+
183+ std .debug .panic ("TODO: read install-dir from '{s}'" , .{setting_path });
184+ }
185+
186+ fn allocInstallDirString (allocator : Allocator ) error {AlreadyReported }! []const u8 {
159187 // TODO: maybe support ZIG_INSTALL_DIR environment variable?
188+ if (try readInstallDir (allocator )) | d | return d ;
189+
160190 // TODO: maybe support a file on the filesystem to configure install dir?
161191 if (builtin .os .tag == .windows ) {
162- const self_exe_dir = try std .fs .selfExeDirPathAlloc (allocator );
192+ const self_exe_dir = std .fs .selfExeDirPathAlloc (allocator ) catch | e | {
193+ std .log .err ("failed to get exe dir path with {s}" , .{@errorName (e )});
194+ return error .AlreadyReported ;
195+ };
163196 defer allocator .free (self_exe_dir );
164197 return std .fs .path .join (allocator , &.{ self_exe_dir , "zig" });
165198 }
@@ -205,8 +238,12 @@ fn toAbsolute(allocator: Allocator, path: []const u8) ![]u8 {
205238 return std .fs .path .join (allocator , &[_ ][]const u8 { cwd , path });
206239}
207240
208- fn help () void {
209- std .io .getStdErr ().writeAll (
241+ fn help (allocator : Allocator ) ! void {
242+ const default_install_dir = allocInstallDirString (allocator ) catch | err | switch (err ) {
243+ error .AlreadyReported = > "unknown (see error printed above)" ,
244+ };
245+
246+ try std .io .getStdErr ().writer ().print (
210247 \\Download and manage zig compilers.
211248 \\
212249 \\Common Usage:
@@ -226,6 +263,7 @@ fn help() void {
226263 \\
227264 \\Common Options:
228265 \\ --install-dir DIR override the default install location
266+ \\ default: {s}
229267 \\ --path-link PATH path to the `zig` symlink that points to the default compiler
230268 \\ this will typically be a file path within a PATH directory so
231269 \\ that the user can just run `zig`
@@ -234,7 +272,9 @@ fn help() void {
234272 ++ " " ++ default_index_url ++
235273 \\
236274 \\
237- ) catch unreachable ;
275+ ,
276+ .{default_install_dir },
277+ );
238278}
239279
240280fn getCmdOpt (args : [][:0 ]u8 , i : * usize ) ! []const u8 {
@@ -287,7 +327,7 @@ pub fn main2() !u8 {
287327 } else if (std .mem .eql (u8 , "--index" , arg )) {
288328 index_url = try getCmdOpt (args , & i );
289329 } else if (std .mem .eql (u8 , "-h" , arg ) or std .mem .eql (u8 , "--help" , arg )) {
290- help ();
330+ try help (allocator );
291331 return 0 ;
292332 } else {
293333 if (newlen == 0 and std .mem .eql (u8 , "run" , arg )) {
@@ -300,7 +340,7 @@ pub fn main2() !u8 {
300340 args = args [0.. newlen ];
301341 }
302342 if (args .len == 0 ) {
303- help ();
343+ try help (allocator );
304344 return 1 ;
305345 }
306346 if (std .mem .eql (u8 , "fetch-index" , args [0 ])) {
0 commit comments