Skip to content
Closed
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
32 changes: 31 additions & 1 deletion cmd/task/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,15 @@ func run(ctx context.Context, args []string, base string, exec execFunc) error {
}
file, err := find(wd)
if err != nil {
return err
// Outside a repository that pins Task, the Task that is installed: so this command can be
// installed as `task` itself (go install .../cmd/task@latest into a directory ahead of the
// system's), and `task` runs the pinned one with the tools where a repository has a
// versions.yaml and the system's everywhere else.
other := elsewhere(os.Getenv("PATH"))
if other == "" {
return err
}
return exec(other, append([]string{other}, args...), os.Environ())
}
v, err := versions.Load(file)
if err != nil {
Expand Down Expand Up @@ -89,6 +97,28 @@ func withPath(env []string, path string) []string {
return append(out, "PATH="+path)
}

// elsewhere is the first executable named task on path that is not this program; "" when there
// is none.
func elsewhere(path string) string {
self, err := os.Executable()
if err != nil { // mutate-exempt: the running program always has a path on the platforms this runs on
return ""
}
me, err := os.Stat(self)
if err != nil { // mutate-exempt: the program that is running is there to stat
return ""
}
for _, dir := range filepath.SplitList(path) {
p := filepath.Join(dir, "task")
fi, err := os.Stat(p)
if err != nil || fi.IsDir() || fi.Mode()&0o111 == 0 || os.SameFile(fi, me) {
continue
}
return p
}
return ""
}

// find is versions.yaml in dir or the nearest directory above it, as Task finds its Taskfile.
func find(dir string) (string, error) {
for {
Expand Down
40 changes: 40 additions & 0 deletions cmd/task/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ func TestTaskIsThePinnedTaskWithTheLatestToolsOnItsPath(t *testing.T) {
// pins no Task, one that does not read, no go-tools release to be had.
func TestWhatCannotRunTaskIsSaid(t *testing.T) {
_, base := repository(t)
// No other Task to run in its place.
t.Setenv("PATH", t.TempDir())
for _, tc := range []struct {
name, file, base, says string
}{
Expand Down Expand Up @@ -167,3 +169,41 @@ func TestWhatCannotRunTaskIsSaid(t *testing.T) {
}
})
}

// Where no versions.yaml is above, it is the next Task on the PATH that runs, as it was given -
// never this program again, which would be a loop - and with none, the reason it cannot run.
func TestOutsideARepositoryTheInstalledTaskRuns(t *testing.T) {
_, base := repository(t)
t.Chdir(t.TempDir())
self, err := os.Executable()
if err != nil {
t.Fatal(err)
}
me, aDir, notTask, system := t.TempDir(), t.TempDir(), t.TempDir(), t.TempDir()
if err := os.Symlink(self, filepath.Join(me, "task")); err != nil {
t.Fatal(err)
}
for dir, mode := range map[string]os.FileMode{notTask: 0o644, system: 0o755} {
if err := os.WriteFile(filepath.Join(dir, "task"), []byte("#!/bin/sh\n"), mode); err != nil {
t.Fatal(err)
}
}
if err := os.Mkdir(filepath.Join(aDir, "task"), 0o755); err != nil {
t.Fatal(err)
}
t.Setenv("PATH", strings.Join([]string{filepath.Join(t.TempDir(), "none"), me, aDir, notTask, system}, string(os.PathListSeparator)))
var c call
if err := run(t.Context(), []string{"build", "X=1"}, base, c.exec); err != nil {
t.Fatal(err)
}
want := filepath.Join(system, "task")
if !c.called || c.path != want || !slices.Equal(c.argv, []string{want, "build", "X=1"}) {
t.Errorf("exec'd %v %q %v; want %s with the arguments as given", c.called, c.path, c.argv, want)
}

t.Setenv("PATH", me)
c = call{}
if err := run(t.Context(), nil, base, c.exec); err == nil || !strings.Contains(err.Error(), "no versions.yaml here or above") || c.called {
t.Errorf("only this program on the PATH: err = %v, exec'd %v", err, c.called)
}
}
10 changes: 10 additions & 0 deletions site/src/content/docs/start.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ user's cache - Task held to its pin, the tools to the sha256 their release's `ch
latest is asked at most every ten minutes, and a machine that cannot ask goes on with the one it
has. Task's and the tools' own modules - about 450 together - are in no `go.mod` and no CI cache.

To type `task` rather than `go tool task`, install the command as `task` in a directory that comes
before the system's on your `PATH`:

```sh
GOBIN=~/.local/bin go install github.com/spin-stack/go-tools/cmd/task@latest
```

In a repository with a `versions.yaml` it runs the Task that file pins, with the tools; anywhere
else it runs the next `task` on the `PATH`, as if it were not there.

No repository bumps the tools: a release of go-tools is in every repository's next run. What a
repository imports of go-tools as a library - `versions`, `provenance` - is `go.mod`'s. Each
release is also tagged `v0.<YYYYMMDD>.<N>`, a module version, so Dependabot's `gomod` updates
Expand Down
Loading