Skip to content

Add bin_dir setting and use sudo only for unwritable bin directories - #20

Merged
jsnjack merged 1 commit into
jsnjack:masterfrom
patrick-5546:bin-dir-setting
Nov 2, 2026
Merged

jsnjack merged 1 commit into
jsnjack:masterfrom
patrick-5546:bin-dir-setting

Conversation

@patrick-5546

@patrick-5546 patrick-5546 commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

grm installs binaries to /usr/local/bin/ and copies and removes them through sudo. On machines where the user has no sudo access, or prefers to keep tools in the home directory, there is no way to choose another directory. Running the commands through sudo also makes binaries in a user-owned directory owned by root.

Change

  • grm set bin_dir <absolute path> stores the directory in the config file. Relative paths are rejected, and an empty value restores the default. GrmConfig.BinDir() returns the setting or DefaultBinDir, and installRelease uses it for new binary components, including --rename.
  • Binary copies and removals run through sudo only when the current user cannot create files in the target directory. The check creates and removes a temporary file in that directory, or in its nearest existing parent when the directory does not exist yet. System package operations still always use runPrivileged.
  • If the target directory does not exist, installBinaryTo creates it with mkdir -p before the first copy, using the same rule. A new ~/.local/bin is created without sudo, and a directory under a root-owned parent is created through sudo.
  • README and AGENTS.md describe the setting.

Behavior change

When the target directory is already writable by the current user, grm no longer uses sudo for binary files. For example, if Homebrew has made /usr/local/bin writable by the user, binaries installed there are now owned by the user instead of root. When the directory is not writable, grm uses sudo as before.

A missing target directory is now created. This also applies to the default: if /usr/local/bin does not exist, grm creates it through sudo, where master fails to copy the binary.

Existing installs keep their recorded paths. After bin_dir changes, a binary moves to the new directory on its next update or grm install --refresh, and the installation plan lists the old file for removal.

Tested

  • New tests cover the setting validation, BinDir(), installRelease with bin_dir (with and without --rename, and with a directory that does not exist yet), and sudo use for writable, unwritable, and missing directories. The missing-directory cases fail without the directory handling.
  • TestInstallReleaseMigratesLegacyConfig and TestInstallReselectsOnlyWhenFilterChanges replace PATH with fake programs and install to /usr/local/bin, so they now also provide a fake mkdir. Without it, they fail on systems where /usr/local/bin does not exist.
  • make fmt vet build lint passes. go test ./... passes in a fedora:44 container as root and as a regular user, with and without /usr/local/bin. On Ubuntu, three existing asset filter tests fail on master too, because they expect rpm to be installed and dpkg to be missing.
  • E2E with a failing sudo first on PATH: after grm set bin_dir <dir> with a <dir> that does not exist yet, grm install bootandy/dust -f tar.gz -y creates the directory and installs the binary owned by the user, without sudo. Under an unwritable parent, only the mkdir runs through sudo.

Comment thread cmd/main_utils.go Resolved
The bin_dir setting changes where grm installs binaries. It defaults to
/usr/local/bin/ and must be an absolute path; an empty value restores
the default. A missing directory is created before the first binary is
installed. Binary file operations use sudo only when the current user
cannot create files in the target directory, or in its nearest existing
parent when the directory does not exist yet, so a user-owned directory
such as ~/.local/bin works without sudo and keeps user ownership.
System package operations still use sudo.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@jsnjack
jsnjack self-requested a review November 2, 2026 16:13
@jsnjack

jsnjack commented Nov 2, 2026

Copy link
Copy Markdown
Owner

Thanks!

@jsnjack
jsnjack merged commit f0e722b into jsnjack:master Nov 2, 2026
2 checks passed
@patrick-5546
patrick-5546 deleted the bin-dir-setting branch November 2, 2026 17:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants