Skip to content
Merged
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
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ dist/

# Config with secrets
pvt.yaml

# Zig build artifacts
tui/zig-out/
tui/.zig-cache/

vitui.md
74 changes: 74 additions & 0 deletions cmd/tui.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package cmd

import (
"fmt"
"os"
"os/exec"
"path/filepath"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var tuiCmd = &cobra.Command{
Use: "tui",
Short: "Launch the interactive TUI (vitui)",
Long: `Launches vitui, the interactive terminal UI for monitoring and managing your Talos-on-Proxmox cluster.`,
RunE: runTUI,
}

func init() {
rootCmd.AddCommand(tuiCmd)
}

func runTUI(cmd *cobra.Command, args []string) error {
binary, err := findVitui()
if err != nil {
return fmt.Errorf("vitui binary not found: %w\n\nInstall vitui by running: cd tui && zig build -Doptimize=ReleaseSafe", err)
}

// Build vitui args, forwarding the resolved config path
var vituiArgs []string
configPath := cfgFile
if configPath == "" {
// Use viper's resolved config path if available
if f := viper.ConfigFileUsed(); f != "" {
configPath = f
}
}
if configPath != "" {
vituiArgs = append(vituiArgs, "--config", configPath)
}

proc := exec.Command(binary, vituiArgs...)
proc.Stdin = os.Stdin
proc.Stdout = os.Stdout
proc.Stderr = os.Stderr

return proc.Run()
}

// findVitui searches for the vitui binary in standard locations.
func findVitui() (string, error) {
// 1. Adjacent to the pvt binary
self, err := os.Executable()
if err == nil {
adjacent := filepath.Join(filepath.Dir(self), "vitui")
if _, err := os.Stat(adjacent); err == nil {
return adjacent, nil
}
}

// 2. In the tui/zig-out/bin/ directory relative to working dir
local := filepath.Join("tui", "zig-out", "bin", "vitui")
if _, err := os.Stat(local); err == nil {
return local, nil
}

// 3. In $PATH
if p, err := exec.LookPath("vitui"); err == nil {
return p, nil
}

return "", fmt.Errorf("not in PATH, not adjacent to pvt binary, and not in tui/zig-out/bin/")
}
55 changes: 55 additions & 0 deletions tui/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const std = @import("std");

pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

// Dependencies
const vaxis_dep = b.dependency("vaxis", .{
.target = target,
.optimize = optimize,
});
const yaml_dep = b.dependency("zig_yaml", .{
.target = target,
.optimize = optimize,
});

// Executable
const exe = b.addExecutable(.{
.name = "vitui",
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "vaxis", .module = vaxis_dep.module("vaxis") },
.{ .name = "yaml", .module = yaml_dep.module("yaml") },
},
}),
});
b.installArtifact(exe);

// Run step
const run_step = b.step("run", "Run vitui");
const run_cmd = b.addRunArtifact(exe);
run_step.dependOn(&run_cmd.step);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}

// Tests
const exe_tests = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "vaxis", .module = vaxis_dep.module("vaxis") },
.{ .name = "yaml", .module = yaml_dep.module("yaml") },
},
}),
});
const test_step = b.step("test", "Run tests");
test_step.dependOn(&b.addRunArtifact(exe_tests).step);
}
21 changes: 21 additions & 0 deletions tui/build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
.{
.name = .vitui,
.version = "0.1.0",
.fingerprint = 0xc340ce385d55450f,
.minimum_zig_version = "0.15.2",
.dependencies = .{
.vaxis = .{
.url = "git+https://github.com/rockorager/libvaxis#41fff922316dcb8776332ec460e73eaf397d5033",
.hash = "vaxis-0.5.1-BWNV_JJOCQAtdJyLvrYCKbKIhX9q3liQkKMAzujWS4HJ",
},
.zig_yaml = .{
.url = "git+https://github.com/kubkon/zig-yaml#a6c2cd8760bf45c49b17a3f6259c4dfe3ded528e",
.hash = "zig_yaml-0.2.0-C1161pmrAgDnipDTh_4v4RQD27XN5GNaVlzzvlXf1jfW",
},
},
.paths = .{
"build.zig",
"build.zig.zon",
"src",
},
}
120 changes: 120 additions & 0 deletions tui/src/api/http_client.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
const std = @import("std");
const config = @import("../config.zig");
const Allocator = std.mem.Allocator;

/// HTTP client that uses curl subprocess for Proxmox API requests.
/// Handles PVE API token auth and TLS certificate skipping for self-signed certs.
pub const HttpClient = struct {
allocator: Allocator,
endpoint: []const u8,
token_id: []const u8,
token_secret: []const u8,
tls_verify: bool,

pub fn init(allocator: Allocator, pve: config.ProxmoxCluster) HttpClient {
return .{
.allocator = allocator,
.endpoint = pve.endpoint,
.token_id = pve.token_id,
.token_secret = pve.token_secret,
.tls_verify = pve.tls_verify,
};
}

/// Perform a GET request. Caller owns the returned memory.
pub fn get(self: HttpClient, path: []const u8) ![]const u8 {
return self.request("GET", path);
}

/// Perform a DELETE request. Caller owns the returned memory.
pub fn delete(self: HttpClient, path: []const u8) ![]const u8 {
return self.request("DELETE", path);
}

fn request(self: HttpClient, method: []const u8, path: []const u8) ![]const u8 {
const url = try std.fmt.allocPrint(self.allocator, "{s}{s}", .{ self.endpoint, path });
defer self.allocator.free(url);

const auth = try std.fmt.allocPrint(self.allocator, "Authorization: PVEAPIToken={s}={s}", .{ self.token_id, self.token_secret });
defer self.allocator.free(auth);

var argv_list: std.ArrayListUnmanaged([]const u8) = .empty;
defer argv_list.deinit(self.allocator);

try argv_list.appendSlice(self.allocator, &.{ "curl", "-s", "-f", "--max-time", "10" });
if (!std.mem.eql(u8, method, "GET")) {
try argv_list.appendSlice(self.allocator, &.{ "-X", method });
}
try argv_list.appendSlice(self.allocator, &.{ "-H", auth });
if (!self.tls_verify) {
try argv_list.append(self.allocator, "-k");
}
try argv_list.append(self.allocator, url);

const result = std.process.Child.run(.{
.allocator = self.allocator,
.argv = argv_list.items,
.max_output_bytes = 1024 * 1024,
}) catch {
return error.HttpRequestFailed;
};
defer self.allocator.free(result.stderr);

const term = result.term;
if (term == .Exited and term.Exited == 0) {
return result.stdout;
}

self.allocator.free(result.stdout);
return error.HttpRequestFailed;
}

pub fn deinit(self: *HttpClient) void {
_ = self;
}
};

/// Parse a JSON response body and extract the "data" field.
/// Returns the parsed JSON Value. Caller must call `parsed.deinit()`.
pub fn parseJsonResponse(allocator: Allocator, body: []const u8) !std.json.Parsed(std.json.Value) {
return std.json.parseFromSlice(std.json.Value, allocator, body, .{
.ignore_unknown_fields = true,
.allocate = .alloc_always,
});
}

/// Extract a string field from a JSON object, returning a default if missing.
pub fn jsonStr(obj: std.json.ObjectMap, key: []const u8, default: []const u8) []const u8 {
const val = obj.get(key) orelse return default;
return switch (val) {
.string => |s| s,
else => default,
};
}

/// Extract an integer field from a JSON object, returning a default if missing.
pub fn jsonInt(obj: std.json.ObjectMap, key: []const u8, default: i64) i64 {
const val = obj.get(key) orelse return default;
return switch (val) {
.integer => |i| i,
.float => |f| @intFromFloat(f),
.string => |s| std.fmt.parseInt(i64, s, 10) catch default,
else => default,
};
}

/// Extract a float field from a JSON object, returning a default if missing.
pub fn jsonFloat(obj: std.json.ObjectMap, key: []const u8, default: f64) f64 {
const val = obj.get(key) orelse return default;
return switch (val) {
.float => |f| f,
.integer => |i| @floatFromInt(i),
else => default,
};
}

test "jsonStr returns default for missing key" {
var map = std.json.ObjectMap.init(std.testing.allocator);
defer map.deinit();
try std.testing.expectEqualStrings("fallback", jsonStr(map, "missing", "fallback"));
}
Loading
Loading