mirror of
https://github.com/daylinmorgan/oizys.git
synced 2024-12-27 14:10:45 -06:00
Compare commits
7 commits
3040b77bff
...
69ac3fa6a6
Author | SHA1 | Date | |
---|---|---|---|
69ac3fa6a6 | |||
7dfe185a9f | |||
a7302bd609 | |||
bf192f478d | |||
7fc911b1a8 | |||
a160e2eccc | |||
6bbc8aa28a |
4 changed files with 74 additions and 26 deletions
30
flake.lock
30
flake.lock
|
@ -205,11 +205,11 @@
|
|||
"xdph": "xdph"
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1712586782,
|
||||
"narHash": "sha256-8/JkRxsXLhiGELcw7t5RBf1mk94mbHc/oJiBzoPYTv8=",
|
||||
"lastModified": 1712835979,
|
||||
"narHash": "sha256-m44SvLfwPc8qPpBQa5ObOtTe6RbVNfQLpn+bCdlHFxQ=",
|
||||
"owner": "hyprwm",
|
||||
"repo": "Hyprland",
|
||||
"rev": "63e3668529f6c06478ec288723d0ba46374c4bc5",
|
||||
"rev": "185a3b48814cc4a1afbf32a69792a6161c4038cd",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
|
@ -385,11 +385,11 @@
|
|||
},
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1712573573,
|
||||
"narHash": "sha256-xxon7WwNm4/EadMKg1eF40/5s0O78nXUy2ILZt6vT7E=",
|
||||
"lastModified": 1712757991,
|
||||
"narHash": "sha256-kR7C7Fqt3JP40h0mzmSZeWI5pk1iwqj4CSeGjnUbVHc=",
|
||||
"owner": "nixos",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "0d28066770464d19d637f6e8e42e8688420b6ac6",
|
||||
"rev": "d6b3ddd253c578a7ab98f8011e59990f21dc3932",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
|
@ -424,11 +424,11 @@
|
|||
]
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1712589286,
|
||||
"narHash": "sha256-pyppS3qgeZagY/YZF0ug2rf97Z91+gaUwnS0McFuH1k=",
|
||||
"lastModified": 1712844911,
|
||||
"narHash": "sha256-HByQWVHn0sG7BFj+7z9UhhMY9CcCqaWBPXX5H3QKRyQ=",
|
||||
"owner": "nix-community",
|
||||
"repo": "nixpkgs-wayland",
|
||||
"rev": "2547cb5b49297ad22dee07f6847c7a99a99f7721",
|
||||
"rev": "ed9ef02d368c00e69e8e67fa86d0226559df75aa",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
|
@ -490,11 +490,11 @@
|
|||
},
|
||||
"stable": {
|
||||
"locked": {
|
||||
"lastModified": 1712437997,
|
||||
"narHash": "sha256-g0whLLwRvgO2FsyhY8fNk+TWenS3jg5UdlWL4uqgFeo=",
|
||||
"lastModified": 1712741485,
|
||||
"narHash": "sha256-bCs0+MSTra80oXAsnM6Oq62WsirOIaijQ/BbUY59tR4=",
|
||||
"owner": "nixos",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "e38d7cb66ea4f7a0eb6681920615dfcc30fc2920",
|
||||
"rev": "b2cf36f43f9ef2ded5711b30b1f393ac423d8f72",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
|
@ -691,11 +691,11 @@
|
|||
]
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1712539287,
|
||||
"narHash": "sha256-TIcl3dKFqSymIFUj3vRN05Ahl2eAH/EqJRf/P5nCBtY=",
|
||||
"lastModified": 1712798505,
|
||||
"narHash": "sha256-eWGnAesSlXa7E2ugjwPT7g1HVs0uPPoLL2NT9sOzKCU=",
|
||||
"owner": "Cloudef",
|
||||
"repo": "zig2nix",
|
||||
"rev": "53e12c1a03c34b0b35bcb0eb5b495ba90f62302f",
|
||||
"rev": "c5279639cbbb10df20a340f54dd95b562fafc552",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
|
|
|
@ -3,9 +3,13 @@ const std = @import("std");
|
|||
const yazap = @import("yazap");
|
||||
const App = yazap.App;
|
||||
const Arg = yazap.Arg;
|
||||
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
allocator: Allocator,
|
||||
app: App,
|
||||
matches: *const yazap.ArgMatches = undefined,
|
||||
forward: ?[][]const u8 = null,
|
||||
process_args: ?[]const [:0]u8 = null,
|
||||
|
||||
pub fn init(allocator: Allocator) !Cli {
|
||||
var app = App.init(allocator, "oizys", "nix begat oizys");
|
||||
|
@ -17,19 +21,55 @@ pub fn init(allocator: Allocator) !Cli {
|
|||
var cmd_output = app.createCommand("output", "show system flake output path");
|
||||
var cmd_boot = app.createCommand("boot", "nixos rebuild boot");
|
||||
var cmd_switch = app.createCommand("switch", "nixos rebuild switch");
|
||||
const commands = .{ &cmd_dry, &cmd_build, &cmd_cache, &cmd_output, &cmd_switch, &cmd_boot };
|
||||
|
||||
try cmd_cache.addArg(Arg.singleValueOption("name", 'n', "name of cachix cache"));
|
||||
inline for (commands) |subcmd| {
|
||||
inline for (.{
|
||||
&cmd_dry,
|
||||
&cmd_build,
|
||||
&cmd_cache,
|
||||
&cmd_output,
|
||||
&cmd_switch,
|
||||
&cmd_boot,
|
||||
}) |subcmd| {
|
||||
try subcmd.addArg(Arg.positional("forward", null, null));
|
||||
try subcmd.addArg(Arg.singleValueOption("flake", 'f', "path to flake"));
|
||||
try subcmd.addArg(Arg.singleValueOption("host", null, "hostname (default: current host)"));
|
||||
try subcmd.addArg(Arg.booleanOption("no-pinix", null, "don't use pinix"));
|
||||
try oizys.addSubcommand(subcmd.*);
|
||||
}
|
||||
|
||||
return Cli{ .app = app };
|
||||
return Cli{
|
||||
.allocator = allocator,
|
||||
.app = app,
|
||||
};
|
||||
}
|
||||
|
||||
fn get_forward_args(self: *Cli, args: []const [:0]u8) !usize {
|
||||
var forward = std.ArrayList([]const u8).init(self.allocator);
|
||||
|
||||
const delim_idx: usize = delim_lookup: for (args, 0..) |arg, i| {
|
||||
if (std.mem.eql(u8, "--", arg)) break :delim_lookup i;
|
||||
} else args.len;
|
||||
|
||||
if (args.len > delim_idx)
|
||||
for (args[delim_idx + 1 ..]) |fwd|
|
||||
try forward.append(fwd);
|
||||
|
||||
self.forward = try forward.toOwnedSlice();
|
||||
return delim_idx;
|
||||
}
|
||||
|
||||
pub fn parse(self: *Cli) !void {
|
||||
self.process_args = try std.process.argsAlloc(self.allocator);
|
||||
const delim_idx = try self.get_forward_args(self.process_args.?);
|
||||
self.matches = try self.app.parseFrom(self.process_args.?[1..delim_idx]);
|
||||
}
|
||||
|
||||
pub fn deinit(self: *Cli) void {
|
||||
std.process.argsFree(self.allocator, self.process_args.?);
|
||||
if (self.forward) |fwd| {
|
||||
for (fwd) |arg| self.allocator.free(arg);
|
||||
self.allocator.free(fwd);
|
||||
}
|
||||
self.app.deinit();
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@ output: []const u8,
|
|||
cmd: OizysCmd,
|
||||
no_pinix: bool,
|
||||
debug: bool = false,
|
||||
forward: ?[][]const u8,
|
||||
|
||||
pub const OizysCmd = enum {
|
||||
dry,
|
||||
|
@ -21,11 +22,13 @@ pub const OizysCmd = enum {
|
|||
build,
|
||||
};
|
||||
|
||||
pub fn init(allocator: std.mem.Allocator, matches: *const ArgMatches) !Oizys {
|
||||
pub fn init(allocator: std.mem.Allocator, matches: *const ArgMatches, forward: ?[][]const u8) !Oizys {
|
||||
const cmd = matches.subcommand.?.name;
|
||||
const flags = matches.subcommandMatches(cmd).?;
|
||||
const host = flags.getSingleValue("host") orelse try Oizys.getDefaultHostName(allocator);
|
||||
const flake = flags.getSingleValue("flake") orelse try Oizys.getDefaultFlake(allocator);
|
||||
const host = flags.getSingleValue("host") orelse
|
||||
try Oizys.getDefaultHostName(allocator);
|
||||
const flake = flags.getSingleValue("flake") orelse
|
||||
try Oizys.getDefaultFlake(allocator);
|
||||
|
||||
return Oizys{
|
||||
.allocator = allocator,
|
||||
|
@ -39,6 +42,7 @@ pub fn init(allocator: std.mem.Allocator, matches: *const ArgMatches) !Oizys {
|
|||
.cmd = std.meta.stringToEnum(OizysCmd, cmd).?,
|
||||
.cache_name = flags.getSingleValue("cache") orelse "daylin",
|
||||
.no_pinix = flags.containsArg("no-pinix"),
|
||||
.forward = forward,
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -89,6 +93,7 @@ pub fn runNixCmd(self: *Oizys, cmd: NixCmd, argv: []const []const u8) !void {
|
|||
NixCmd.NixosRebuild => try args.appendSlice(&.{ "sudo", self.nixos_rebuild() }),
|
||||
}
|
||||
try args.appendSlice(argv);
|
||||
if (self.forward) |fwd| try args.appendSlice(fwd);
|
||||
var p = std.ChildProcess.init(args.items, self.allocator);
|
||||
_ = try p.spawnAndWait();
|
||||
}
|
||||
|
@ -127,7 +132,8 @@ pub fn run(self: *Oizys) !void {
|
|||
try self.runNixCmd(NixCmd.Nix, &.{ "build", self.output });
|
||||
},
|
||||
.output => {
|
||||
std.debug.print("{s}", .{self.output});
|
||||
const stdout = std.io.getStdOut().writer();
|
||||
try stdout.print("{s}\n", .{self.output});
|
||||
},
|
||||
.cache => {
|
||||
try self.cache();
|
||||
|
|
|
@ -9,14 +9,16 @@ pub fn main() !void {
|
|||
const allocator = arena.allocator();
|
||||
|
||||
var cli = try Cli.init(allocator);
|
||||
const matches = try cli.app.parseProcess();
|
||||
try cli.parse();
|
||||
defer cli.deinit();
|
||||
|
||||
if (!matches.containsArgs()) {
|
||||
if (!cli.matches.containsArgs()) {
|
||||
try cli.app.displayHelp();
|
||||
return;
|
||||
}
|
||||
|
||||
var oizys = try Oizys.init(allocator, matches);
|
||||
var oizys = try Oizys.init(allocator, cli.matches, cli.forward);
|
||||
defer oizys.deinit();
|
||||
try oizys.run();
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue