Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: replace symbolic links to executable proxy file on Windows #892

Merged
merged 3 commits into from
Jun 19, 2022
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
10 changes: 9 additions & 1 deletion pkg/controller/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/aquaproj/aqua/pkg/config/aqua"
registry "github.com/aquaproj/aqua/pkg/install-registry"
"github.com/aquaproj/aqua/pkg/installpackage"
"github.com/aquaproj/aqua/pkg/runtime"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
)
Expand All @@ -27,16 +28,18 @@ type Controller struct {
configReader reader.ConfigReader
registryInstaller registry.Installer
fs afero.Fs
runtime *runtime.Runtime
}

func New(param *config.Param, configFinder finder.ConfigFinder, configReader reader.ConfigReader, registInstaller registry.Installer, pkgInstaller installpackage.Installer, fs afero.Fs) *Controller {
func New(param *config.Param, configFinder finder.ConfigFinder, configReader reader.ConfigReader, registInstaller registry.Installer, pkgInstaller installpackage.Installer, fs afero.Fs, rt *runtime.Runtime) *Controller {
return &Controller{
rootDir: param.RootDir,
configFinder: configFinder,
configReader: configReader,
registryInstaller: registInstaller,
packageInstaller: pkgInstaller,
fs: fs,
runtime: rt,
}
}

Expand All @@ -46,6 +49,11 @@ func (ctrl *Controller) Install(ctx context.Context, param *config.Param, logE *
if err := ctrl.fs.MkdirAll(rootBin, dirPermission); err != nil {
return fmt.Errorf("create the directory: %w", err)
}
if ctrl.runtime.GOOS == "windows" {
if err := ctrl.fs.MkdirAll(filepath.Join(ctrl.rootDir, "bat"), dirPermission); err != nil {
return fmt.Errorf("create the directory: %w", err)
}
}

if err := ctrl.packageInstaller.InstallProxy(ctx, logE); err != nil {
return err //nolint:wrapcheck
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/install/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ packages:
downloader := download.NewPackageDownloader(nil, d.rt, download.NewHTTPDownloader(http.DefaultClient))
executor := exec.NewMock(0, nil)
pkgInstaller := installpackage.New(d.param, downloader, d.rt, fs, linker, executor)
ctrl := install.New(d.param, finder.NewConfigFinder(fs), reader.New(fs), registry.New(d.param, registryDownloader, fs), pkgInstaller, fs)
ctrl := install.New(d.param, finder.NewConfigFinder(fs), reader.New(fs), registry.New(d.param, registryDownloader, fs), pkgInstaller, fs, d.rt)
if err := ctrl.Install(ctx, d.param, logE); err != nil {
if d.isErr {
return
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions pkg/installpackage/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ type installer struct {
executor exec.Executor
}

func isWindows(goos string) bool {
return goos == "windows"
}

func (inst *installer) InstallPackages(ctx context.Context, cfg *aqua.Config, registries map[string]*registry.Config, binDir string, onlyLink, isTest bool, logE *logrus.Entry) error {
pkgs, failed := inst.createLinks(cfg, registries, binDir, logE)
if onlyLink {
Expand Down Expand Up @@ -186,6 +190,13 @@ func (inst *installer) createLinks(cfg *aqua.Config, registries map[string]*regi
PackageInfo: pkgInfo,
})
for _, file := range pkgInfo.GetFiles() {
if isWindows(inst.runtime.GOOS) {
if err := inst.createProxyWindows(file.Name, logE); err != nil {
logerr.WithError(logE, err).Error("create the proxy file")
failed = true
}
continue
}
if err := inst.createLink(filepath.Join(binDir, file.Name), proxyName, logE); err != nil {
logerr.WithError(logE, err).Error("create the symbolic link")
failed = true
Expand Down
58 changes: 58 additions & 0 deletions pkg/installpackage/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package installpackage
import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/sirupsen/logrus"
"github.com/spf13/afero"
)

func (inst *installer) createLink(linkPath, linkDest string, logE *logrus.Entry) error {
Expand Down Expand Up @@ -58,3 +61,58 @@ func (inst *installer) recreateLink(linkPath, linkDest string, logE *logrus.Entr
}
return nil
}

const (
batTemplate = `@echo off
aqua exec -- <COMMAND> %*
`
scrTemplate = `#!/usr/bin/env bash
exec aqua exec -- $0 $@
`
proxyPermission os.FileMode = 0o755
)

func (inst *installer) createProxyWindows(binName string, logE *logrus.Entry) error {
if err := inst.createBinWindows(filepath.Join(inst.rootDir, "bin", binName), scrTemplate, logE); err != nil {
return err
}
if err := inst.createBinWindows(filepath.Join(inst.rootDir, "bat", binName+".bat"), strings.Replace(batTemplate, "<COMMAND>", binName, 1), logE); err != nil {
return err
}
return nil
}

func (inst *installer) createBinWindows(binPath, binTxt string, logE *logrus.Entry) error {
if fileInfo, err := inst.linker.Lstat(binPath); err == nil {
switch mode := fileInfo.Mode(); {
case mode.IsDir():
// if file is a directory, raise error
return fmt.Errorf("%s has already existed and is a directory", binPath)
case mode&os.ModeNamedPipe != 0:
// if file is a pipe, raise error
return fmt.Errorf("%s has already existed and is a named pipe", binPath)
case mode.IsRegular():
// TODO check content
return nil
case mode&os.ModeSymlink != 0:
if err := inst.fs.Remove(binPath); err != nil {
return fmt.Errorf("remove a symbolic link (%s): %w", binPath, err)
}
return inst.writeBinWindows(binPath, binTxt, logE)
default:
return fmt.Errorf("unexpected file mode %s: %s", binPath, mode.String())
}
}

return inst.writeBinWindows(binPath, binTxt, logE)
}

func (inst *installer) writeBinWindows(proxyPath, binTxt string, logE *logrus.Entry) error {
logE.WithFields(logrus.Fields{
"proxy_path": proxyPath,
}).Info("create a proxy file")
if err := afero.WriteFile(inst.fs, proxyPath, []byte(binTxt), proxyPermission); err != nil {
return fmt.Errorf("create a proxy file (%s): %w", proxyPath, err)
}
return nil
}
6 changes: 3 additions & 3 deletions pkg/installpackage/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import (
const ProxyVersion = "v1.1.2" // renovate: depName=aquaproj/aqua-proxy

func (inst *installer) InstallProxy(ctx context.Context, logE *logrus.Entry) error {
if isWindows(inst.runtime.GOOS) {
return nil
}
proxyAssetTemplate := `aqua-proxy_{{.OS}}_{{.Arch}}.tar.gz`
pkg := &config.Package{
Package: &aqua.Package{
Expand Down Expand Up @@ -63,9 +66,6 @@ func (inst *installer) InstallProxy(ctx context.Context, logE *logrus.Entry) err

// create a symbolic link
binName := proxyName
if inst.runtime.GOOS == "windows" {
binName += ".exe"
}
a, err := filepath.Rel(filepath.Join(inst.rootDir, "bin"), filepath.Join(pkgPath, binName))
if err != nil {
return fmt.Errorf("get a relative path: %w", err)
Expand Down