Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/args.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const clap = @import("clap");
const logging = @import("log.zig");
const types = @import("types.zig");

const SubCommands = enum {
get,
Expand All @@ -18,3 +19,42 @@ pub const main_params = clap.parseParamsComptime(
\\<command>
\\
);

pub const get_parsers = .{
.OUTPUT = clap.parsers.enumeration(types.Output),
.STR = clap.parsers.string,
.PATH = clap.parsers.string,
.LEVEL = clap.parsers.enumeration(logging.Level),
};

pub const get_params = clap.parseParamsComptime(
\\-h, --help Display this help and exit.
\\-n, --namespace <STR> Namespace to get resources from.
\\-A, --all-namespaces If present, list all objects across all namespaces. Specifying --namespace will be ignored.
\\-s, --sort Prints the resources in order.
\\-e, --exclude <STR>... Exclude crd types. Multiple can be excluded eg: "-e <CRD> -e <CRD>"
\\-o, --output <OUTPUT> Changes the output format of the results. [default: tty, tty|json|sqlite]
\\-d, --database <PATH> Path to the sqlite file to save the results. If the files does not exist it will be created.
\\-l, --label <STR> Set the label that will be saved with entries when using the --database option.
\\--log-level <LEVEL> Set the log level. All logs are saved to file. Possible values are (debug, info, warn, error). Default level is warn.
\\
);

pub const diff_parsers = .{
.PATH = clap.parsers.string,
.STR = clap.parsers.string,
.LABEL = clap.parsers.string,
.LEVEL = clap.parsers.enumeration(logging.Level),
.OUTPUT = clap.parsers.enumeration(types.Output),
};

pub const diff_params = clap.parseParamsComptime(
\\-h, --help Display this help and exit.
\\-d, --database <PATH> Path to SQLite database to load data from.
\\-e, --exclude <STR>... Exclude resource types. Multiple can be excluded eg: "-e <KIND> -e <KIND>"
\\-o, --output <OUTPUT> Changes the output format of the results. [default: tty, tty|json]
\\--log-level <LEVEL> Set the log level. All logs are saved to file. Possible values are (debug, info, warn, error). Default level is warn.
\\<LABEL> Older label used.
\\<LABEL> Newer label used.
\\
);
51 changes: 31 additions & 20 deletions src/diff.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,11 @@ const db = @import("database.zig");
const table = @import("table.zig");
const types = @import("types.zig");
const help = @import("help.zig");
const args = @import("args.zig");

pub fn diffMain(io: std.Io, gpa: std.mem.Allocator, iter: *std.process.Args.Iterator) !void {
const params = comptime clap.parseParamsComptime(
\\-h, --help Display this help and exit.
\\-d, --database <PATH> Path to SQLite database to load data from.
\\-e, --exclude <STR>... Exclude resource types. Multiple can be excluded eg: "-e <KIND> -e <KIND>"
\\-o, --output <OUTPUT> Changes the output format of the results. [default: tty, tty|json]
\\--log-level <LEVEL> Set the log level. All logs are saved to file. Possible values are (debug, info, warn, error). Default level is warn.
\\<LABEL> Older label used.
\\<LABEL> Newer label used.
\\
);

const parsers = comptime .{
.PATH = clap.parsers.string,
.STR = clap.parsers.string,
.LABEL = clap.parsers.string,
.LEVEL = clap.parsers.enumeration(logging.Level),
.OUTPUT = clap.parsers.enumeration(types.Output),
};

var diag = clap.Diagnostic{};
var res = clap.parseEx(clap.Help, &params, parsers, iter, .{
var res = clap.parseEx(clap.Help, &args.diff_params, args.diff_parsers, iter, .{
.diagnostic = &diag,
.allocator = gpa,
}) catch |err| {
Expand Down Expand Up @@ -70,6 +52,11 @@ pub fn diffMain(io: std.Io, gpa: std.mem.Allocator, iter: *std.process.Args.Iter
try printSection(io, "Added Resources", added_resources, max_width);
try printSection(io, "Updated Resources", updated_resources, max_width);
try printSection(io, "Removed Resources", deleted_resources, max_width);
try printSummary(io, max_width, .{
.added = added_resources,
.updated = updated_resources,
.deleted = deleted_resources,
});
},
.json => {
try printJson(io, gpa, added_resources, updated_resources, deleted_resources);
Expand All @@ -92,6 +79,11 @@ fn printJson(io: std.Io, gpa: std.mem.Allocator, added: types.ResourceList, upda
try printResourceListJson(stdout, gpa, updated);
try stdout.print(", \"deleted\": ", .{});
try printResourceListJson(stdout, gpa, deleted);
try stdout.print(", \"summary\": {{\"added\": {}, \"updated\": {}, \"deleted\": {}}}", .{
added.items.len,
updated.items.len,
deleted.items.len,
});
try stdout.print("}}\n", .{});
try stdout.flush();
}
Expand Down Expand Up @@ -166,3 +158,22 @@ fn section(io: std.Io, header: []const u8, width: usize) !void {
try stdout.print("{s}\n\n", .{reset});
try stdout.flush();
}

const values = struct {
added: types.ResourceList,
updated: types.ResourceList,
deleted: types.ResourceList,
};

fn printSummary(io: std.Io, width: usize, value: values) !void {
try section(io, "Summary", width);

var buf: [4096]u8 = undefined;
var file_writer = std.Io.File.stdout().writer(io, &buf);
const stdout = &file_writer.interface;

try stdout.print("Added: {}\n", .{value.added.items.len});
try stdout.print("Updated: {}\n", .{value.updated.items.len});
try stdout.print("Removed: {}\n", .{value.deleted.items.len});
try stdout.flush();
}
27 changes: 2 additions & 25 deletions src/get.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,15 @@ const types = @import("types.zig");
const table = @import("table.zig");
const utils = @import("utils.zig");
const help = @import("help.zig");
const args = @import("args.zig");

pub fn getMain(io: std.Io, gpa: std.mem.Allocator, iter: *std.process.Args.Iterator) !void {
var stdout_buf: [4096]u8 = undefined;
var stdout_writer = std.Io.File.stdout().writer(io, &stdout_buf);
const stdout = &stdout_writer.interface;
// First we specify what parameters our program can take.
// We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`.
const params = comptime clap.parseParamsComptime(
\\-h, --help Display this help and exit.
\\-n, --namespace <STR> Namespace to get resources from.
\\-A, --all-namespaces If present, list all objects across all namespaces. Specifying --namespace will be ignored.
\\-s, --sort Prints the resources in order.
\\-e, --exclude <STR>... Exclude crd types. Multiple can be excluded eg: "-e <CRD> -e <CRD>"
\\-o, --output <OUTPUT> Changes the output format of the results. [default: tty, tty|json|sqlite]
\\-d, --database <PATH> Path to the sqlite file to save the results. If the files does not exist it will be created.
\\-l, --label <STR> Set the label that will be saved with entries when using the --database option.
\\--log-level <LEVEL> Set the log level. All logs are saved to file. Possible values are (debug, info, warn, error). Default level is warn.
\\
);

const parsers = comptime .{
.OUTPUT = clap.parsers.enumeration(types.Output),
.STR = clap.parsers.string,
.PATH = clap.parsers.string,
.LEVEL = clap.parsers.enumeration(logging.Level),
};

// Initialize our diagnostics, which can be used for reporting useful errors.
// This is optional. You can also pass `.{}` to `clap.parse` if you don't
// care about the extra information `Diagnostic` provides.
var diag = clap.Diagnostic{};
var res = clap.parseEx(clap.Help, &params, parsers, iter, .{
var res = clap.parseEx(clap.Help, &args.get_params, args.get_parsers, iter, .{
.diagnostic = &diag,
.allocator = gpa,
}) catch |err| {
Expand Down
Loading