z_mirror/hare-fmm
git clone https://git.raiguard.me/z_mirror/hare-fmm
74b341b
trunk
1use dirs; 2use fmt; 3use getopt; 4use os; 5use strings; 6 7fn abort_usage( 8 title: str, 9 help: []getopt::help, 10 error: fmt::formattable... 11) never = { 12 if (len(error) > 0) { 13 fmt::errorf("{}: ", title)!; 14 fmt::errorln(error...)!; 15 }; 16 getopt::printusage(os::stdout, title, help)!; 17 os::exit(1); 18}; 19 20export fn main() void = { 21 const help: []getopt::help = [ 22 "manage your Factorio mods", 23 ('c', "file", "use the given configuration file"), 24 ('d', "directory", "use the given mods directory"), 25 "<clean | disable | enable | install | query | sync | publish>", 26 "args..." 27 ]; 28 const cmd = getopt::parse(os::args, help...); 29 defer getopt::finish(&cmd); 30 if (len(cmd.args) == 0) { 31 abort_usage(os::args[0], help); 32 }; 33 // Default config path 34 let config_path = fmt::asprintf( 35 "{}/{}", dirs::config("fmm"), "fmm.ini" 36 )!; 37 // User-specified config path 38 for (let opt &.. cmd.opts) { 39 if (opt.0 == 'c') { 40 free(config_path); 41 config_path = strings::dup(opt.1)!; 42 break; 43 }; 44 }; 45 defer free(config_path); 46 // Base configuration from config file 47 let config = config_parse(config_path); 48 defer config_free(config); 49 // Parse global CLI options 50 for (let opt &.. cmd.opts) { 51 switch (opt.0) { 52 case 'd' => 53 free(config.mods_dir); 54 config.mods_dir = strings::dup(opt.1)!; 55 case => void; 56 }; 57 }; 58 59 let subcmd = cmd.args[0]; 60 const task = switch (cmd.args[0]) { 61 case "clean", "c" => 62 yield &clean; 63 case "disable", "d" => 64 yield &disable; 65 case "enable", "e" => 66 yield &enable; 67 case "install", "i" => 68 yield &install; 69 case "query", "q" => 70 yield &query; 71 case "sync", "s" => 72 yield &sync; 73 case "publish", "p" => 74 yield &publish; 75 case => 76 abort_usage("fmm", help, "unrecognized subcommand:", subcmd); 77 }; 78 79 task(config, cmd.args); 80};