Skip to content

Commit

Permalink
feat: enable you to remove go_install and http packages (#3101)
Browse files Browse the repository at this point in the history
* feat: enable you to remove go_install and http packages

* fix: fix pkgPath

* fix: fix http package's pkgPath

* fix: replace base_dir with base_dirs

* refactor: split method

* fix: support multiple package types

* fix: use filepath.FromSlash

* fix: remove dirs by glob

* fix: remove BaseDirs

* refactor: split function

* refactor: remove the dependency on maps

* ci: add the integration test

* fix: fix go_install path

* fix: ignore no_asset and error_message

* fix: ignore a lint error

* ci: replace kind with terraform for test of http package
  • Loading branch information
suzuki-shunsuke authored Sep 21, 2024
1 parent 58bfbe0 commit 9eff974
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 14 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/wc-integration-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,12 @@ jobs:
env:
GITHUB_TOKEN: ${{steps.token.outputs.token}}

- run: terraform --help
- run: terrafmt --help
- name: Test rm
run: aqua rm x-motemen/ghq bats-core/bats-core
# http - terraform
# go_install - terrafmt
run: aqua rm x-motemen/ghq bats-core/bats-core terraform terrafmt

- name: Test rm -m l
run: aqua rm -m l ghcp
Expand Down
52 changes: 46 additions & 6 deletions pkg/config/registry/package_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package registry

import (
"fmt"
"net/url"
"path"
"path/filepath"
"regexp"
"strings"

"github.com/aquaproj/aqua/v2/pkg/runtime"
Expand Down Expand Up @@ -664,16 +666,54 @@ func (p *PackageInfo) defaultCmdName() string {
return path.Base(p.GetName())
}

func (p *PackageInfo) PkgPath() string {
var placeHolderTemplate = regexp.MustCompile(`{{.*?}}`)

func (p *PackageInfo) pkgPaths() []string { //nolint:cyclop
if p.NoAsset || p.ErrorMessage != "" {
return nil
}
switch p.Type {
case PkgInfoTypeGitHubArchive, PkgInfoTypeGoBuild, PkgInfoTypeGitHubContent, PkgInfoTypeGitHubRelease:
return filepath.Join(p.Type, "github.com", p.RepoOwner, p.RepoName)
if p.RepoOwner == "" || p.RepoName == "" {
return nil
}
return []string{filepath.Join(p.Type, "github.com", p.RepoOwner, p.RepoName)}
case PkgInfoTypeCargo:
return filepath.Join(p.Type, "crates.io", p.Crate)
case PkgInfoTypeGoInstall, PkgInfoTypeHTTP:
return ""
if p.Crate == "" {
return nil
}
return []string{filepath.Join(p.Type, "crates.io", p.Crate)}
case PkgInfoTypeGoInstall:
a := p.GetPath()
if a == "" {
return nil
}
return []string{filepath.Join(p.Type, filepath.FromSlash(placeHolderTemplate.ReplaceAllLiteralString(a, "*")))}
case PkgInfoTypeHTTP:
if p.URL == "" {
return nil
}
u, err := url.Parse(placeHolderTemplate.ReplaceAllLiteralString(p.URL, "*"))
if err != nil {
return nil
}
return []string{filepath.Join(p.Type, u.Host, filepath.FromSlash(u.Path))}
}
return ""
return nil
}

func (p *PackageInfo) PkgPaths() map[string]struct{} {
m := map[string]struct{}{}
for _, a := range p.pkgPaths() {
m[a] = struct{}{}
}
for _, vo := range p.VersionOverrides {
pkg := p.overrideVersion(vo)
for _, a := range pkg.pkgPaths() {
m[a] = struct{}{}
}
}
return m
}

func (p *PackageInfo) SLSASourceURI() string {
Expand Down
29 changes: 23 additions & 6 deletions pkg/controller/remove/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/aquaproj/aqua/v2/pkg/config/registry"
"github.com/aquaproj/aqua/v2/pkg/fuzzyfinder"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/suzuki-shunsuke/logrus-error/logerr"
)

Expand Down Expand Up @@ -190,18 +191,34 @@ func (c *Controller) removePackage(logE *logrus.Entry, rootDir string, pkg *regi
return gErr
}

path := pkg.PkgPath()
if path == "" {
logE.WithField("package_type", pkg.Type).Warn("this package type can't be removed")
paths := pkg.PkgPaths()
if len(paths) == 0 {
logE.WithField("package_type", pkg.Type).Warn("this package can't be removed")
return gErr
}
pkgPath := filepath.Join(rootDir, "pkgs", path)
if err := c.fs.RemoveAll(pkgPath); err != nil {
return fmt.Errorf("remove directories: %w", err)
for path := range paths {
if err := c.removePath(logE, rootDir, path); err != nil {
return err
}
}
return gErr
}

func (c *Controller) removePath(logE *logrus.Entry, rootDir string, path string) error {
pkgPath := filepath.Join(rootDir, "pkgs", path)
arr, err := afero.Glob(c.fs, pkgPath)
if err != nil {
return fmt.Errorf("find directories: %w", err)
}
for _, p := range arr {
logE.WithField("removed_path", p).Debug("removing a directory")
if err := c.fs.RemoveAll(p); err != nil {
return fmt.Errorf("remove directories: %w", err)
}
}
return nil
}

func parsePkgName(pkgName string) (string, string) {
registryName, pkgName, ok := strings.Cut(pkgName, ",")
if ok {
Expand Down
3 changes: 2 additions & 1 deletion tests/main/aqua-global.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ registries:
path: registry.yaml
packages:
- name: x-motemen/[email protected]
- name: kubernetes-sigs/kind # http package, raw format
- name: hashicorp/[email protected]
- name: kubernetes-sigs/kind # raw format
registry: standard # standard registry
version: v0.17.0 # renovate: depName=kubernetes-sigs/kind
- name: restic/[email protected]
Expand Down

0 comments on commit 9eff974

Please sign in to comment.