Skip to content

Commit

Permalink
feat: support cargo install's --features and --all-features options
Browse files Browse the repository at this point in the history
  • Loading branch information
suzuki-shunsuke committed May 20, 2023
1 parent 26d8a5b commit 972e475
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 17 deletions.
21 changes: 20 additions & 1 deletion pkg/config/registry/package_info.go
Expand Up @@ -30,6 +30,7 @@ type PackageInfo struct {
Link string `json:"link,omitempty" yaml:",omitempty"`
Asset *string `json:"asset,omitempty" yaml:",omitempty"`
Crate *string `json:"crate,omitempty" yaml:",omitempty"`
Cargo *Cargo `json:"cargo,omitempty"`
URL *string `json:"url,omitempty" yaml:",omitempty"`
Path *string `json:"path,omitempty" yaml:",omitempty"`
Format string `json:"format,omitempty" jsonschema:"example=tar.gz,example=raw,example=zip,example=dmg" yaml:",omitempty"`
Expand All @@ -54,6 +55,11 @@ type PackageInfo struct {
ErrorMessage string `json:"-" yaml:"-"`
}

type Cargo struct {
Features []string `json:"features,omitempty"`
AllFeatures bool `yaml:"all_features" json:"all_features,omitempty"`
}

func (pkgInfo *PackageInfo) Copy() *PackageInfo {
pkg := &PackageInfo{
Name: pkgInfo.Name,
Expand All @@ -62,6 +68,7 @@ func (pkgInfo *PackageInfo) Copy() *PackageInfo {
RepoName: pkgInfo.RepoName,
Asset: pkgInfo.Asset,
Crate: pkgInfo.Crate,
Cargo: pkgInfo.Cargo,
Path: pkgInfo.Path,
Format: pkgInfo.Format,
Files: pkgInfo.Files,
Expand Down Expand Up @@ -91,21 +98,24 @@ func (pkgInfo *PackageInfo) Copy() *PackageInfo {
return pkg
}

func (pkgInfo *PackageInfo) resetByPkgType(typ string) {
func (pkgInfo *PackageInfo) resetByPkgType(typ string) { //nolint:funlen
switch typ {
case PkgInfoTypeGitHubRelease:
pkgInfo.URL = nil
pkgInfo.Path = nil
pkgInfo.Crate = nil
pkgInfo.Cargo = nil
case PkgInfoTypeGitHubContent:
pkgInfo.URL = nil
pkgInfo.Asset = nil
pkgInfo.Crate = nil
pkgInfo.Cargo = nil
case PkgInfoTypeGitHubArchive:
pkgInfo.URL = nil
pkgInfo.Path = nil
pkgInfo.Asset = nil
pkgInfo.Crate = nil
pkgInfo.Cargo = nil
pkgInfo.Format = ""
case PkgInfoTypeHTTP:
pkgInfo.Path = nil
Expand All @@ -114,6 +124,7 @@ func (pkgInfo *PackageInfo) resetByPkgType(typ string) {
pkgInfo.URL = nil
pkgInfo.Asset = nil
pkgInfo.Crate = nil
pkgInfo.Cargo = nil
pkgInfo.WindowsExt = ""
pkgInfo.CompleteWindowsExt = nil
pkgInfo.Cosign = nil
Expand Down Expand Up @@ -151,6 +162,9 @@ func (pkgInfo *PackageInfo) overrideVersion(child *VersionOverride) *PackageInfo
if child.Crate != nil {
pkg.Crate = child.Crate
}
if child.Cargo != nil {
pkg.Cargo = child.Cargo
}
if child.Path != nil {
pkg.Path = child.Path
}
Expand Down Expand Up @@ -254,6 +268,10 @@ func (pkgInfo *PackageInfo) OverrideByRuntime(rt *runtime.Runtime) { //nolint:cy
pkgInfo.Crate = ov.Crate
}

if ov.Cargo != nil {
pkgInfo.Cargo = ov.Cargo
}

if ov.Files != nil {
pkgInfo.Files = ov.Files
}
Expand Down Expand Up @@ -287,6 +305,7 @@ type VersionOverride struct {
RepoName string `yaml:"repo_name,omitempty" json:"repo_name,omitempty"`
Asset *string `yaml:",omitempty" json:"asset,omitempty"`
Crate *string `json:"crate,omitempty" yaml:",omitempty"`
Cargo *Cargo `json:"cargo,omitempty"`
Path *string `yaml:",omitempty" json:"path,omitempty"`
URL *string `yaml:",omitempty" json:"url,omitempty"`
Files []*File `yaml:",omitempty" json:"files,omitempty"`
Expand Down
1 change: 1 addition & 0 deletions pkg/config/registry/replacements.go
Expand Up @@ -11,6 +11,7 @@ type Override struct {
Format string `yaml:",omitempty" json:"format,omitempty" jsonschema:"example=tar.gz,example=raw,example=zip"`
Asset *string `yaml:",omitempty" json:"asset,omitempty"`
Crate *string `json:"crate,omitempty" yaml:",omitempty"`
Cargo *Cargo `json:"cargo,omitempty"`
Files []*File `yaml:",omitempty" json:"files,omitempty"`
URL *string `yaml:",omitempty" json:"url,omitempty"`
CompleteWindowsExt *bool `json:"complete_windows_ext,omitempty" yaml:"complete_windows_ext,omitempty"`
Expand Down
20 changes: 16 additions & 4 deletions pkg/controller/wire.go
Expand Up @@ -231,7 +231,10 @@ func InitializeInstallCommandController(ctx context.Context, param *config.Param
download.NewDownloader,
wire.Bind(new(download.ClientAPI), new(*download.Downloader)),
),
afero.NewOsFs,
wire.NewSet(
afero.NewOsFs,
wire.Bind(new(installpackage.Cleaner), new(afero.Fs)),
),
wire.NewSet(
link.New,
wire.Bind(new(domain.Linker), new(*link.Linker)),
Expand Down Expand Up @@ -402,7 +405,10 @@ func InitializeExecCommandController(ctx context.Context, param *config.Param, h
wire.Bind(new(download.ChecksumDownloader), new(*download.ChecksumDownloaderImpl)),
),
osenv.New,
afero.NewOsFs,
wire.NewSet(
afero.NewOsFs,
wire.Bind(new(installpackage.Cleaner), new(afero.Fs)),
),
wire.NewSet(
link.New,
wire.Bind(new(domain.Linker), new(*link.Linker)),
Expand Down Expand Up @@ -460,7 +466,10 @@ func InitializeExecCommandController(ctx context.Context, param *config.Param, h
func InitializeUpdateAquaCommandController(ctx context.Context, param *config.Param, httpClient *http.Client, rt *runtime.Runtime) *updateaqua.Controller {
wire.Build(
updateaqua.New,
afero.NewOsFs,
wire.NewSet(
afero.NewOsFs,
wire.Bind(new(installpackage.Cleaner), new(afero.Fs)),
),
wire.NewSet(
github.New,
wire.Bind(new(github.RepositoriesService), new(*github.RepositoriesServiceImpl)),
Expand Down Expand Up @@ -578,7 +587,10 @@ func InitializeCopyCommandController(ctx context.Context, param *config.Param, h
wire.Bind(new(download.ChecksumDownloader), new(*download.ChecksumDownloaderImpl)),
),
osenv.New,
afero.NewOsFs,
wire.NewSet(
afero.NewOsFs,
wire.Bind(new(installpackage.Cleaner), new(afero.Fs)),
),
wire.NewSet(
link.New,
wire.Bind(new(domain.Linker), new(*link.Linker)),
Expand Down
8 changes: 4 additions & 4 deletions pkg/controller/wire_gen.go

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

37 changes: 30 additions & 7 deletions pkg/installpackage/cargo.go
Expand Up @@ -3,33 +3,56 @@ package installpackage
import (
"context"
"fmt"

"github.com/aquaproj/aqua/v2/pkg/config/registry"
"github.com/sirupsen/logrus"
)

type CargoPackageInstaller interface {
Install(ctx context.Context, crate, version, root string) error
Install(ctx context.Context, logE *logrus.Entry, crate, version, root string, opts *registry.Cargo) error
}

type MockCargoPackageInstaller struct {
Err error
}

func (mock *MockCargoPackageInstaller) Install(ctx context.Context, crate, version, root string) error {
func (mock *MockCargoPackageInstaller) Install(ctx context.Context, logE *logrus.Entry, crate, version, root string, opts *registry.Cargo) error {
return mock.Err
}

type CargoPackageInstallerImpl struct {
exec Executor
exec Executor
cleaner Cleaner
}

type Cleaner interface {
RemoveAll(name string) (err error)
}

func NewCargoPackageInstallerImpl(exec Executor) *CargoPackageInstallerImpl {
func NewCargoPackageInstallerImpl(exec Executor, cleaner Cleaner) *CargoPackageInstallerImpl {
return &CargoPackageInstallerImpl{
exec: exec,
exec: exec,
cleaner: cleaner,
}
}

func (inst *CargoPackageInstallerImpl) Install(ctx context.Context, crate, version, root string) error {
_, err := inst.exec.Exec(ctx, "cargo", "install", "--version", version, "--root", root, crate)
func (inst *CargoPackageInstallerImpl) Install(ctx context.Context, logE *logrus.Entry, crate, version, root string, opts *registry.Cargo) error {
args := []string{"install", "--version", version, "--root", root}
if opts != nil {
if opts.AllFeatures {
args = append(args, "--all-features")
} else if len(opts.Features) != 0 {
args = append(append(args, "--features"), opts.Features...)
}
}
_, err := inst.exec.Exec(ctx, "cargo", append(args, crate)...)
if err != nil {
// Clean up root
logE := logE.WithField("install_dir", root)
logE.Info("removing the install directory because the installation failed")
if err := inst.cleaner.RemoveAll(root); err != nil {
logE.WithError(err).Error("aqua tried to remove the install directory because the installation failed, but it failed")
}
return fmt.Errorf("install a crate: %w", err)
}
return nil
Expand Down
10 changes: 9 additions & 1 deletion pkg/installpackage/download.go
Expand Up @@ -198,10 +198,18 @@ func (inst *InstallerImpl) downloadGoInstall(ctx context.Context, pkg *config.Pa
}

func (inst *InstallerImpl) downloadCargo(ctx context.Context, logE *logrus.Entry, pkg *config.Package, root string) error {
cargoOpts := pkg.PackageInfo.Cargo
if cargoOpts != nil {
if cargoOpts.AllFeatures {
logE = logE.WithField("cargo_all_features", true)
} else if len(cargoOpts.Features) != 0 {
logE = logE.WithField("cargo_features", strings.Join(cargoOpts.Features, ","))
}
}
logE.Info("Installing a crate")
crate := *pkg.PackageInfo.Crate
version := pkg.Package.Version
if err := inst.cargoPackageInstaller.Install(ctx, crate, version, root); err != nil {
if err := inst.cargoPackageInstaller.Install(ctx, logE, crate, version, root, cargoOpts); err != nil {
return fmt.Errorf("cargo install: %w", err)
}
return nil
Expand Down

0 comments on commit 972e475

Please sign in to comment.