Skip to content
Open
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
14 changes: 11 additions & 3 deletions get.go
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,8 @@ func install(ctx context.Context, logger *log.Logger, r *runner.Runner, modDir s
}

// go install does not define -modfile flag, so we mimic go install with go build -o instead.
binPath := filepath.Join(gobin, fmt.Sprintf("%s-%s", name, pkg.Module.Version))
binName := fmt.Sprintf("%s-%s", name, pkg.Module.Version)
binPath := filepath.Join(gobin, binName)

// New context with new environment files.
modCtx = r.With(ctx, modFile.Filepath(), modDir, pkg.BuildEnvs)
Expand All @@ -825,12 +826,19 @@ func install(ctx context.Context, logger *log.Logger, r *runner.Runner, modDir s
return nil
}

if err := os.RemoveAll(filepath.Join(gobin, name)); err != nil {
return installSymlink(gobin, binName, name)
}

func installSymlink(dir, oldName, newName string) error {
newPath := filepath.Join(dir, newName)
if err := os.RemoveAll(newPath); err != nil {
return errors.Wrap(err, "rm")
}
if err := os.Symlink(binPath, filepath.Join(gobin, name)); err != nil {

if err := os.Symlink(oldName, newPath); err != nil {
return errors.Wrap(err, "symlink")
}

return nil
}

Expand Down
37 changes: 37 additions & 0 deletions get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package main

import (
"os"
"path/filepath"
"testing"

"github.com/efficientgo/core/errors"
Expand Down Expand Up @@ -89,3 +91,38 @@ func TestParseTarget(t *testing.T) {
}

}

func TestInstallSymlink(t *testing.T) {
dir := t.TempDir()
oldName := "some-binary-1.2.3"
newName := "some-binary"
oldPath := filepath.Join(dir, oldName)
newPath := filepath.Join(dir, newName)
testData := "#!/bin/true"

// simulate an existing symlink to test the removal logic
f, err := os.Create(newPath)
testutil.Ok(t, err)
testutil.Ok(t, f.Close())

// create a dummy target ...
f, err = os.Create(oldPath)
testutil.Ok(t, err)
// ... and write some data for verification later
_, err = f.Write([]byte(testData))
testutil.Ok(t, err)
testutil.Ok(t, f.Close())

err = installSymlink(dir, oldName, newName)
testutil.Ok(t, err)

gotPath, err := os.Readlink(newPath)
testutil.Ok(t, err)
testutil.Equals(t, oldName, gotPath)

gotData, err := os.ReadFile(newPath)
testutil.Ok(t, err)

// ensure the symlink leads to the desired target
testutil.Equals(t, testData, string(gotData))
}
Loading