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
3 changes: 3 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ that ordering.
`--binary` are strict, repeatable selectors. Changing them works at the same
release version and replaces the affected selection. Unselected old outputs
are listed in the plan and removed only after the new installs succeed.
`--filter` counts as a change only when it differs from the saved filter,
because scripts repeat the same install command and expect nothing to happen
when the package is current.
- **`--yes` installs all archive binaries on initial selection.** Asset selection
still defaults to the first compatible candidate, with RPMs ranked first.
Remembered archive subsets stay unchanged on updates, including with `--yes`.
Expand Down
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,13 @@ archive binaries are not added to remembered selections automatically.

Passing `--asset` replaces the saved asset selection; passing `--binary`
replaces the binary selection in the assets it applies to. Changing `--filter`
starts asset selection again. These flags work even when the release version is
already installed. The installation plan lists all outputs and any previously
tracked components that will be removed when the new selection succeeds. grm
asks for confirmation only when the plan removes components, so an install
with one suitable asset and one binary runs without prompts.
starts asset selection again; passing the saved filter again keeps the
selection, so a repeated install command does not reinstall the package. These
flags work even when the release version is already installed. The installation
plan lists all outputs and any previously tracked components that will be
removed when the new selection succeeds. grm asks for confirmation only when the
plan removes components, so an install with one suitable asset and one binary
runs without prompts.

All components share a repository's release and lock. `update` updates them
together; `remove` removes every tracked binary and system package. Downloads
Expand Down
10 changes: 7 additions & 3 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os/exec"
"path"
"runtime"
"slices"
"sort"
"strings"

Expand Down Expand Up @@ -81,7 +82,10 @@ Examples:
previous.Components = append([]InstalledComponent(nil), previous.Components...)
pkg = &previous
}
if cmd.Flags().Changed("filter") {
// Changed only reports that --filter was passed. Repeating the saved
// filter keeps the remembered selection, so the command stays idempotent.
filterChanged := cmd.Flags().Changed("filter") && !slices.Equal(installFilter, pkg.Filter)
if filterChanged {
pkg.Filter = installFilter
pkg.Selections = nil
}
Expand All @@ -96,7 +100,7 @@ Examples:
if err != nil {
return err
}
options := installOptions{assets: installAssets, binaries: binaries, rename: installRename, lock: installLock, reselect: cmd.Flags().Changed("filter")}
options := installOptions{assets: installAssets, binaries: binaries, rename: installRename, lock: installLock, reselect: filterChanged}

// Check that package is not locked
for _, installedItem := range config.Packages {
Expand All @@ -115,7 +119,7 @@ Examples:
}
msgOK("Found release %s", boldCyan(release.GetTagName()))

if !installRefresh && !installLock && requested.VersionFilter == "" && !cmd.Flags().Changed("asset") && !cmd.Flags().Changed("binary") && !cmd.Flags().Changed("rename") && !cmd.Flags().Changed("filter") {
if !installRefresh && !installLock && requested.VersionFilter == "" && !cmd.Flags().Changed("asset") && !cmd.Flags().Changed("binary") && !cmd.Flags().Changed("rename") && !filterChanged {
// Check if package of selected release has already been installed
for _, installedItem := range config.Packages {
if installedItem.GetFullName() == pkg.GetFullName() {
Expand Down
90 changes: 90 additions & 0 deletions cmd/install_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package cmd

import (
"bytes"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"slices"
"strings"
"testing"

"github.com/google/go-github/v93/github"
Expand Down Expand Up @@ -662,3 +670,85 @@ func stringInSlice(a string, list []string) bool {
}
return false
}

func withInstallFilter(t *testing.T, value string) {
t.Helper()
t.Cleanup(func() {
installFilter = nil
installCmd.Flags().Lookup("filter").Changed = false
})
if err := installCmd.Flags().Set("filter", value); err != nil {
t.Fatal(err)
}
}

func TestInstallReselectsOnlyWhenFilterChanges(t *testing.T) {
cases := []struct {
name, filter, latest string
wantDownloads int
wantBinaries []string
}{
{name: "same filter at installed version", filter: "linux", latest: "v1.0.0", wantBinaries: []string{"tool"}},
{name: "same filter keeps remembered binaries", filter: "linux", latest: "v2.0.0", wantDownloads: 1, wantBinaries: []string{"tool"}},
{name: "changed filter selects again", filter: "zip", latest: "v1.0.0", wantDownloads: 1, wantBinaries: []string{"helper", "tool"}},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
withYes(t, true)
payload, err := os.ReadFile(releaseArchive(t, map[string][]byte{
"tools-" + c.latest + "/helper": fixtureBinary,
"tools-" + c.latest + "/tool": fixtureBinary,
}))
if err != nil {
t.Fatal(err)
}
release := fmt.Sprintf(`{"tag_name":%q,"assets":[{"id":1,"name":"tools_%s_linux_amd64.zip"}]}`, c.latest, c.latest)
downloads := 0
withGitHubTransport(t, func(request *http.Request) (*http.Response, error) {
if strings.HasSuffix(request.URL.Path, "/releases/assets/1") {
downloads++
return &http.Response{StatusCode: http.StatusOK, Header: http.Header{"Content-Type": {"application/octet-stream"}}, Body: io.NopCloser(bytes.NewReader(payload)), Request: request}, nil
}
return httpResponse(request, http.StatusOK, []byte(release)), nil
})
fakes := t.TempDir()
fakeProgram(t, fakes, "sudo", `exec "$@"`)
for _, program := range []string{"cp", "chmod", "mv", "rm"} {
fakeProgram(t, fakes, program, "exit 0")
}
t.Setenv("PATH", fakes)
// The installed binary lives in a temporary directory so VerifyVersion can hash it.
installed := filepath.Join(t.TempDir(), "tool")
if err := os.WriteFile(installed, fixtureBinary, 0755); err != nil {
t.Fatal(err)
}
config, err := ReadConfig(ConfigFile)
if err != nil {
t.Fatal(err)
}
if err := config.PutPackage(&Package{
Owner: "owner", Repo: "tools", Version: "v1.0.0", Filter: []string{"linux"},
Selections: []AssetSelection{{Pattern: "tools_*_linux_amd64.zip", Binaries: []BinarySelection{{Path: "tools-*/tool"}}}},
Components: []InstalledComponent{{Asset: "tools_v1.0.0_linux_amd64.zip", Source: "tools-v1.0.0/tool", Version: "v1.0.0", Filename: installed}},
}); err != nil {
t.Fatal(err)
}
withInstallFilter(t, c.filter)
if err := installCmd.RunE(installCmd, []string{"owner/tools"}); err != nil {
t.Fatal(err)
}
config, err = ReadConfig(ConfigFile)
if err != nil {
t.Fatal(err)
}
var binaries []string
for _, component := range config.Packages["owner/tools"].Components {
binaries = append(binaries, filepath.Base(component.Filename))
}
slices.Sort(binaries)
if downloads != c.wantDownloads || !slices.Equal(binaries, c.wantBinaries) {
t.Errorf("downloaded %d assets and installed %v, want %d and %v", downloads, binaries, c.wantDownloads, c.wantBinaries)
}
})
}
}