M4: CLI polish — version flag, mountpoint override, stable aliases

Add --version flag with build-time injection via ldflags. Add
--mountpoint/-m flag to mount for one-off mount point override. Change
init aliases from device path basename (sda1) to UUID prefix (b8b2f8e3)
for stability across boots. Add .gitignore. Update flake.nix with
version injection.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-24 08:01:57 -07:00
parent ff3147e73b
commit e21ff8039b
6 changed files with 54 additions and 15 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
/arca
result

View File

@@ -6,6 +6,7 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"git.wntrmute.dev/kyle/arca/internal/config" "git.wntrmute.dev/kyle/arca/internal/config"
"git.wntrmute.dev/kyle/arca/internal/udisks" "git.wntrmute.dev/kyle/arca/internal/udisks"
"github.com/godbus/dbus/v5" "github.com/godbus/dbus/v5"
@@ -62,7 +63,7 @@ func runInit(cmd *cobra.Command, args []string) error {
continue continue
} }
alias := aliasFromPath(dev.DevicePath) alias := aliasFromUUID(dev.UUID)
cfg.Devices[alias] = config.DeviceConfig{ cfg.Devices[alias] = config.DeviceConfig{
UUID: dev.UUID, UUID: dev.UUID,
Methods: []string{"passphrase"}, Methods: []string{"passphrase"},
@@ -101,8 +102,12 @@ func isRootBacking(path dbus.ObjectPath, rootDevices []dbus.ObjectPath) bool {
return false return false
} }
func aliasFromPath(devPath string) string { func aliasFromUUID(uuid string) string {
// "/dev/sda1" -> "sda1", "/dev/nvme0n1p2" -> "nvme0n1p2" // Use first 8 chars of UUID as a stable alias.
base := filepath.Base(devPath) // "b8b2f8e3-4cde-4aca-a96e-df9274019f9f" -> "b8b2f8e3"
return strings.TrimPrefix(base, "dm-") clean := strings.ReplaceAll(uuid, "-", "")
if len(clean) > 8 {
clean = clean[:8]
}
return clean
} }

View File

@@ -12,6 +12,8 @@ import (
"golang.org/x/term" "golang.org/x/term"
) )
var mountpoint string
var mountCmd = &cobra.Command{ var mountCmd = &cobra.Command{
Use: "mount <device|alias>", Use: "mount <device|alias>",
Short: "Unlock and mount a LUKS volume", Short: "Unlock and mount a LUKS volume",
@@ -20,6 +22,7 @@ var mountCmd = &cobra.Command{
} }
func init() { func init() {
mountCmd.Flags().StringVarP(&mountpoint, "mountpoint", "m", "", "mount point override (privileged path only)")
rootCmd.AddCommand(mountCmd) rootCmd.AddCommand(mountCmd)
} }
@@ -35,6 +38,12 @@ func runMount(cmd *cobra.Command, args []string) error {
devCfg := cfg.ResolveDevice(target) devCfg := cfg.ResolveDevice(target)
// CLI flag overrides config mountpoint.
mp := devCfg.Mountpoint
if mountpoint != "" {
mp = mountpoint
}
dev, err := client.FindDevice(devCfg.UUID, target) dev, err := client.FindDevice(devCfg.UUID, target)
if err != nil { if err != nil {
return err return err
@@ -43,12 +52,12 @@ func runMount(cmd *cobra.Command, args []string) error {
// Check if already unlocked. // Check if already unlocked.
if cleartext, err := client.CleartextDevice(dev); err == nil { if cleartext, err := client.CleartextDevice(dev); err == nil {
// Already unlocked — check if mounted too. // Already unlocked — check if mounted too.
if mp, mounted := client.IsMounted(cleartext); mounted { if existing, mounted := client.IsMounted(cleartext); mounted {
fmt.Println(mp) fmt.Println(existing)
return nil return nil
} }
// Unlocked but not mounted — just mount it. // Unlocked but not mounted — just mount it.
return doMount(client, cleartext, devCfg) return doMount(client, cleartext, mp)
} }
// Need to unlock. // Need to unlock.
@@ -63,23 +72,35 @@ func runMount(cmd *cobra.Command, args []string) error {
} }
if result.Privileged { if result.Privileged {
mountpoint, err := cryptsetup.Mount(result.Device.DevicePath, devCfg.Mountpoint) mnt, err := cryptsetup.Mount(result.Device.DevicePath, mp)
if err != nil { if err != nil {
return fmt.Errorf("mounting: %w", err) return fmt.Errorf("mounting: %w", err)
} }
fmt.Println(mountpoint) fmt.Println(mnt)
return nil return nil
} }
return doMount(client, result.Device, devCfg) if mp != "" {
fmt.Fprintf(os.Stderr, "warning: --mountpoint is ignored for udisks2 mounts (passphrase/keyfile path)\n")
}
return doMount(client, result.Device, "")
} }
func doMount(client *udisks.Client, cleartext *udisks.BlockDevice, devCfg config.ResolvedDevice) error { func doMount(client *udisks.Client, cleartext *udisks.BlockDevice, mp string) error {
mountpoint, err := client.Mount(cleartext) if mp != "" {
// udisks2 doesn't support custom mount points; use privileged mount.
mnt, err := cryptsetup.Mount(cleartext.DevicePath, mp)
if err != nil { if err != nil {
return fmt.Errorf("mounting: %w", err) return fmt.Errorf("mounting: %w", err)
} }
fmt.Println(mountpoint) fmt.Println(mnt)
return nil
}
mnt, err := client.Mount(cleartext)
if err != nil {
return fmt.Errorf("mounting: %w", err)
}
fmt.Println(mnt)
return nil return nil
} }

View File

@@ -12,6 +12,10 @@ var rootCmd = &cobra.Command{
Short: "Mount and unmount LUKS-encrypted volumes", Short: "Mount and unmount LUKS-encrypted volumes",
} }
func SetVersion(v string) {
rootCmd.Version = v
}
func Execute() { func Execute() {
if err := rootCmd.Execute(); err != nil { if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)

View File

@@ -10,13 +10,17 @@
let let
system = "x86_64-linux"; system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system}; pkgs = nixpkgs.legacyPackages.${system};
version = "1.0.0";
in in
{ {
packages.${system}.default = pkgs.buildGoModule { packages.${system}.default = pkgs.buildGoModule {
pname = "arca"; pname = "arca";
version = "0.1.0"; inherit version;
src = ./.; src = ./.;
vendorHash = null; vendorHash = null;
ldflags = [
"-X main.version=${version}"
];
}; };
}; };
} }

View File

@@ -2,6 +2,9 @@ package main
import "git.wntrmute.dev/kyle/arca/cmd" import "git.wntrmute.dev/kyle/arca/cmd"
var version = "dev"
func main() { func main() {
cmd.SetVersion(version)
cmd.Execute() cmd.Execute()
} }