Skip to content

Instantly share code, notes, and snippets.

@meme

meme/example.ini Secret

Last active January 19, 2020 03:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meme/f7f94fcff5df505f65cc9378fade4df2 to your computer and use it in GitHub Desktop.
Save meme/f7f94fcff5df505f65cc9378fade4df2 to your computer and use it in GitHub Desktop.
[ssh]
ip=127.0.0.1
username=root
key_path=$HOME/.ssh/id_rsa
const std = @import("std");
const INIReader = struct {
data: []const u8,
const Error = error {
Malformed,
KeyNotFound,
};
pub fn new(data: []const u8) anyerror!INIReader {
return INIReader{ .data = data };
}
fn splitItems(comptime data: []const u8, comptime target: u8) usize {
comptime var n: usize = 1;
inline for (data) |c| {
if (c == target) n += 1;
}
return n;
}
fn split(comptime data: []const u8, comptime target: u8) [splitItems(data, target)][]const u8 {
var entries: [splitItems(data, target)][]const u8 = undefined;
comptime var start: usize = 0;
comptime var end: usize = 0;
comptime var n: usize = 0;
inline for (data) |c, i| {
if (c == target) {
end = i;
entries[n] = data[start..end];
n += 1;
start = end + 1; // Skip the _target_ value itself
}
if (i == data.len - 1) {
entries[n] = data[start..data.len];
return entries;
}
}
return entries;
}
pub fn search(comptime self: INIReader, section: []const u8, name: []const u8) INIReader.Error![]const u8 {
const entries = split(self.data, '\n');
comptime var current_section: ?[]const u8 = null;
inline for (entries) |entry| {
if (entry.len > 0) {
if (entry[0] == '[') {
current_section = null;
inline for (entry) |c, i| {
if (c == ']') {
current_section = entry[1..i];
break;
}
}
if (current_section == null) {
return Error.Malformed;
}
} else if (entry[0] == ';') {
continue;
} else {
if (current_section) |s| {
if (std.mem.eql(u8, s, section)) {
comptime var key: []const u8 = undefined;
comptime var value: []const u8 = undefined;
inline for (entry) |c, i| {
if (c == '=') {
key = entry[0..i];
value = entry[i + 1..entry.len];
break;
}
}
if (std.mem.eql(u8, key, name)) {
return value;
}
}
}
}
}
}
return Error.KeyNotFound;
}
};
pub fn main() anyerror!void {
comptime {
const data = @embedFile("../example.ini");
const reader = try INIReader.new(data);
const key_path = reader.search("ssh", "key_path") catch unreachable;
@compileLog(key_path);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment