@@ -3,10 +3,11 @@ const zigdcore = @import("zigdcore.zig");
33const utils = @import ("utils.zig" );
44
55const Command = enum {
6- help ,
76 install ,
8- version ,
7+ setup ,
98 exists ,
9+ help ,
10+ version ,
1011};
1112
1213pub fn main () ! void {
@@ -25,6 +26,7 @@ pub fn main() !void {
2526 if (std .meta .stringToEnum (Command , args [1 ])) | command | {
2627 return try switch (command ) {
2728 .install = > install (allocator , args ),
29+ .setup = > setup (allocator , args ),
2830 .exists = > exists (allocator , args ),
2931 .help = > help_menu (),
3032 .version = > version (),
@@ -39,10 +41,11 @@ fn help_menu() !void {
3941 try std .io .getStdOut ().writer ().print (
4042 \\> zigd ({s}) cli: Manage zigd stuff
4143 \\
42- \\help - Outputs this help Menu
43- \\version - Outputs zigd version
4444 \\install [version] - Install a zig version
45+ \\setup [version] - First time setup (creates a config and installs the version)
4546 \\exists [version] - Check if a zig version is installed on the system
47+ \\help - Outputs this help Menu
48+ \\version - Outputs zigd version
4649 \\
4750 , .{utils .zigd_version });
4851 return ;
@@ -77,7 +80,78 @@ fn install(allocator: std.mem.Allocator, args: []const []const u8) !void {
7780 const zigd_path = try zigdcore .getZigdPath (allocator );
7881 defer allocator .free (zigd_path );
7982
80- try zigdcore .install_zig (allocator , download_url , zigd_path , zig_version .as_string );
83+ if (! try zigdcore .install_zig (allocator , download_url , zigd_path , zig_version )) {
84+ std .log .err ("Installation failed!" , .{});
85+ }
86+
87+ return ;
88+ }
89+
90+ fn setup (allocator : std.mem.Allocator , args : []const []const u8 ) ! void {
91+ if (args .len <= 2 ) {
92+ std .log .err ("Wrong Usage!\n " , .{});
93+ try std .io .getStdOut ().writer ().print (
94+ \\Usage: zigd setup [version]
95+ \\For more help use zigd help
96+ \\
97+ , .{});
98+ return ;
99+ }
100+
101+ const zigd_path = try zigdcore .getZigdPath (allocator );
102+ defer allocator .free (zigd_path );
103+
104+ const config_path = try std .fs .path .join (allocator , &.{ zigd_path , "config" });
105+ defer allocator .free (config_path );
106+
107+ if (try utils .isFile (config_path )) {
108+ o : while (true ) {
109+ std .log .warn ("A config file already exists, overwrite (y/n?)" , .{});
110+ const byte = try std .io .getStdIn ().reader ().readByte ();
111+
112+ switch (byte ) {
113+ 'y' = > break :o ,
114+ 'n' = > return ,
115+ else = > continue :o ,
116+ }
117+ }
118+ }
119+
120+ var zig_version = try zigdcore .ZigVersion .parse (allocator , args [2 ], & user_arg , false );
121+ defer zig_version .deinitIfMasterOrZigverOrZonver (allocator );
122+
123+ if (zig_version .source == .Master ) {
124+ o : while (true ) {
125+ std .log .warn ("It is not recommended to setup with master, are you sure you want to set master as default in the config? (y/n)" , .{});
126+ const byte = try std .io .getStdIn ().reader ().readByte ();
127+
128+ switch (byte ) {
129+ 'y' = > break :o ,
130+ 'n' = > return ,
131+ else = > continue :o ,
132+ }
133+ }
134+ }
135+
136+ const download_url = try zigdcore .downloadUrlFromVersion (allocator , zig_version .as_string , zig_version .source == .Master );
137+ defer allocator .free (download_url );
138+
139+ try std .io .getStdOut ().writer ().print ("Installing version {s}...\n " , .{zig_version });
140+ _ = try zigdcore .install_zig (allocator , download_url , zigd_path , zig_version );
141+
142+ try std .io .getStdOut ().writer ().print ("Creating a config...\n " , .{});
143+ const config_file = try std .fs .createFileAbsolute (config_path , .{
144+ .truncate = true ,
145+ });
146+ defer config_file .close ();
147+
148+ try config_file .writer ().writeAll ("# Generated by `zigd setup`, btw :)\n " );
149+ try config_file .writer ().writeAll ("default=" );
150+ switch (zig_version .source ) {
151+ .Master = > try config_file .writeAll ("master" ),
152+ else = > try config_file .writeAll (zig_version .as_string ),
153+ }
154+ try config_file .writer ().writeByte ('\n ' );
81155}
82156
83157fn exists (allocator : std.mem.Allocator , args : []const []const u8 ) ! void {
0 commit comments