lens

posix system fetch tool
git clone git://brookjeynes.dev/bjeynes/lens.git
Log | Files | Refs | README

commit a2023eb9a028dbc6ce0ed33a2a472f98771b8571
parent 24468b28d86d226c4972b54c48c484b29c4cddd9
Author: BrookJeynes <jeynesbrook@gmail.com>
Date:   Fri, 10 Jan 2025 07:50:46 +1000

rename to lens

Diffstat:
A.github/workflows/create-draft-release.yml | 67+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
MREADME.md | 20++++++++++++--------
Mbuild.zig | 43+++++++++++++++++--------------------------
Mbuild.zig.zon | 12+++++-------
Msrc/environment.zig | 22+++++++++++++++++++---
Msrc/main.zig | 58+++++++++++++++++++++++++++++++++++++++++-----------------
6 files changed, 161 insertions(+), 61 deletions(-)

diff --git a/.github/workflows/create-draft-release.yml b/.github/workflows/create-draft-release.yml @@ -0,0 +1,67 @@ +name: Create draft release + +on: + workflow_dispatch: + +env: + # https://github.com/cli/cli/issues/9514#issuecomment-2311517523 + GH_TOKEN: ${{ secrets.TOKEN }} + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Zig + uses: korandoru/setup-zig@v1 + with: + zig-version: "master" + + - name: Build application + run: | + zig build -Dall-targets + + - name: Upload artifact + uses: actions/upload-artifact@v4 + with: + name: builds + path: zig-out/ + + release: + needs: build + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Download artifacts + uses: actions/download-artifact@v4 + with: + path: artifacts + + - name: Create tarballs + run: | + mkdir -p tarballs + for dir in artifacts/builds/*; do + if [ -d "$dir" ] && [ $(basename "$dir") != "bin" ]; then + tar -czf "tarballs/$(basename "$dir").tar.gz" -C "$dir" . + fi + done + + - name: gh log + run: | + gh --version + gh auth status + + - name: Create release + run: | + gh release create ${{ github.ref_name }} tarballs/* \ + --title "Release ${{ github.ref_name }}" \ + --notes "Automated release with build artifacts." \ + --draft + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/README.md b/README.md @@ -1,13 +1,13 @@ -zysys +lens ----- A simple unix system fetch tool. ``` -bjeyn@fedora +user@fedora -------------- distro Fedora Linux 40 (Workstation Edition) x86_64 -uptime 6 13 57 +uptime 6d 13h 57m kernel 6.10.12-200.fc40.x86_64 desktop river shell zsh @@ -17,11 +17,15 @@ cpu AMD Ryzen 7 7840U w/ Radeon 780M Graphics disk 30G / 1.9T (2%) ``` -An example config can be found in `./example-config.ziggy` with its schema -defined in `./config.ziggy-schema`. +Configure `lens` by editing the external configuration file located at either: +- `$HOME/.lens/config.ziggy` +- `$XDG_CONFIG_HOME/lens/config.ziggy`. -The config will first read from `$XDG_CONFIG_HOME/zysys/config.ziggy`, falling -back to `$HOME/.config/zysys/config.ziggy` if not set. +lens will look for these env variables specifically. If they are not set, lens +will not be able to find the config file. +An example config file can be found [here](./example-config.ziggy). +The config schema can be found [here](./config.ziggy-schema). -This project is built using Zig `v0.14.0-dev.2126+e27b4647d`. +Contributions, issues, and feature requests are always welcome! This project +is built using Zig `v0.14.0-dev.2628+5b5c60f43`. diff --git a/build.zig b/build.zig @@ -7,6 +7,21 @@ const release_targets: []const std.Target.Query = &.{ .{ .cpu_arch = .x86_64, .os_tag = .macos }, }; +fn createExe(b: *std.Build, exe_name: []const u8, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) !*std.Build.Step.Compile { + const ziggy = b.dependency("ziggy", .{ .target = target, .optimize = optimize }).module("ziggy"); + + const exe = b.addExecutable(.{ + .name = exe_name, + .root_source_file = b.path("src/main.zig"), + .target = target, + .optimize = optimize, + }); + + exe.root_module.addImport("ziggy", ziggy); + + return exe; +} + pub fn build(b: *std.Build) !void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); @@ -18,19 +33,7 @@ pub fn build(b: *std.Build) !void { return; } - const ziggy_dep = b.dependency("ziggy", .{ - .target = target, - .optimize = optimize, - }); - const ziggy = ziggy_dep.module("ziggy"); - - const exe = b.addExecutable(.{ - .name = "zysys", - .root_source_file = b.path("src/main.zig"), - .target = target, - .optimize = optimize, - }); - exe.root_module.addImport("ziggy", ziggy); + const exe = try createExe(b, "lens", target, optimize); b.installArtifact(exe); const run_cmd = b.addRunArtifact(exe); @@ -47,19 +50,7 @@ fn build_targets(b: *std.Build) !void { for (release_targets) |t| { const target = b.resolveTargetQuery(t); - const ziggy_dep = b.dependency("ziggy", .{ - .target = target, - .optimize = .ReleaseSafe, - }); - const ziggy = ziggy_dep.module("ziggy"); - - const exe = b.addExecutable(.{ - .name = "zysys", - .root_source_file = b.path("src/main.zig"), - .target = target, - .optimize = .ReleaseSafe, - }); - exe.root_module.addImport("ziggy", ziggy); + const exe = try createExe(b, "lens", target, .ReleaseSafe); b.installArtifact(exe); const target_output = b.addInstallArtifact(exe, .{ diff --git a/build.zig.zon b/build.zig.zon @@ -1,14 +1,12 @@ .{ - .name = "zysys", - .version = "0.1.1", - .minimum_zig_version = "0.14.0-dev.2126+e27b4647d", + .name = "lens", + .version = "0.2.0", + .minimum_zig_version = "0.14.0-dev.2628+5b5c60f43", .dependencies = .{ - // Move to https://github.com/kristoff-it/ziggy/tree/zig-0.14.0-dev - // once https://github.com/kristoff-it/ziggy/pull/43 has been merged. .ziggy = .{ - .url = "git+https://github.com/BrookJeynes/ziggy?ref=add-enum-support#fb224b22f0a30005e521bde9a874bc959463362b", - .hash = "122034c46e43d6d7bc031caad233eb2547352df32e2a2b00fa494742ef6bca52d161", + .url = "git+https://github.com/kristoff-it/ziggy?ref=zig-0.14.0-dev#b807ecacdfe3d8e7f3cee15e0905eb6e0a75424b", + .hash = "12205d8c19f4e2fae9157e45ac1f03fc56a5a268d7e60d9e89176acb4c20c2edfa29", }, }, diff --git a/src/environment.zig b/src/environment.zig @@ -1,18 +1,18 @@ const std = @import("std"); -pub fn get_home_dir() !?std.fs.Dir { +pub fn getHomeDir() !?std.fs.Dir { return try std.fs.openDirAbsolute(std.posix.getenv("HOME") orelse { return null; }, .{ .iterate = true }); } -pub fn get_xdg_config_home_dir() !?std.fs.Dir { +pub fn getXdgConfigHomeDir() !?std.fs.Dir { return try std.fs.openDirAbsolute(std.posix.getenv("XDG_CONFIG_HOME") orelse { return null; }, .{ .iterate = true }); } -pub fn file_exists(dir: std.fs.Dir, path: []const u8) bool { +pub fn fileExists(dir: std.fs.Dir, path: []const u8) bool { const result = blk: { _ = dir.openFile(path, .{}) catch |err| { switch (err) { @@ -26,3 +26,19 @@ pub fn file_exists(dir: std.fs.Dir, path: []const u8) bool { }; return result; } + +pub fn dirExists(dir: std.fs.Dir, path: []const u8) bool { + const result = blk: { + _ = dir.openDir(path, .{}) catch |err| { + switch (err) { + error.FileNotFound => break :blk false, + else => { + std.log.info("{}", .{err}); + break :blk true; + }, + } + }; + break :blk true; + }; + return result; +} diff --git a/src/main.zig b/src/main.zig @@ -37,6 +37,10 @@ const default_config = Config{ }, }; +const CONFIG_NAME = "config.ziggy"; +const HOME_DIR_NAME = ".lens"; +const XDG_CONFIG_HOME_DIR_NAME = "lens"; + pub fn main() !void { const stdout = std.io.getStdOut().writer(); var gpa = std.heap.GeneralPurposeAllocator(.{}){}; @@ -47,27 +51,47 @@ pub fn main() !void { // Read config const config = lbl: { - var config_path: []u8 = undefined; - defer alloc.free(config_path); - - var config_home: std.fs.Dir = undefined; - defer config_home.close(); - - if (try environment.get_xdg_config_home_dir()) |path| { - config_home = path; - config_path = try std.fs.path.join(alloc, &.{ "zysys", "config.ziggy" }); - } else { - if (try environment.get_home_dir()) |path| { - config_home = path; - config_path = try std.fs.path.join(alloc, &.{ ".config", "zysys", "config.ziggy" }); + var config_dir = dir: { + if (try environment.getXdgConfigHomeDir()) |home_dir| { + defer { + var dir = home_dir; + dir.close(); + } + + if (!environment.dirExists(home_dir, XDG_CONFIG_HOME_DIR_NAME)) { + break :lbl default_config; + } + + const dir = try home_dir.openDir(XDG_CONFIG_HOME_DIR_NAME, .{ .iterate = true }); + if (!environment.fileExists(dir, CONFIG_NAME)) { + break :lbl default_config; + } + + break :dir dir; + } + + if (try environment.getHomeDir()) |home_dir| { + defer { + var dir = home_dir; + dir.close(); + } + + if (!environment.dirExists(home_dir, HOME_DIR_NAME)) { + break :lbl default_config; + } + + const dir = try home_dir.openDir(HOME_DIR_NAME, .{ .iterate = true }); + if (!environment.fileExists(dir, CONFIG_NAME)) { + break :lbl default_config; + } + + break :dir dir; } - } - if (!environment.file_exists(config_home, config_path)) { break :lbl default_config; - } + }; - const contents = config_home.readFileAlloc(alloc, config_path, 4096) catch break :lbl default_config; + const contents = config_dir.readFileAlloc(alloc, CONFIG_NAME, 4096) catch break :lbl default_config; defer alloc.free(contents); const contentsZ = try alloc.dupeZ(u8, contents); defer alloc.free(contentsZ);