From 9eff9747af84ae6c77164fd929dee2629b5cfb73 Mon Sep 17 00:00:00 2001 From: Shunsuke Suzuki Date: Sun, 22 Sep 2024 03:04:33 +0900 Subject: [PATCH] feat: enable you to remove go_install and http packages (#3101) * 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 --- .github/workflows/wc-integration-test.yaml | 6 ++- pkg/config/registry/package_info.go | 52 +++++++++++++++++++--- pkg/controller/remove/remove.go | 29 +++++++++--- tests/main/aqua-global.yaml | 3 +- 4 files changed, 76 insertions(+), 14 deletions(-) diff --git a/.github/workflows/wc-integration-test.yaml b/.github/workflows/wc-integration-test.yaml index e4ba183b8..080ead777 100644 --- a/.github/workflows/wc-integration-test.yaml +++ b/.github/workflows/wc-integration-test.yaml @@ -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 diff --git a/pkg/config/registry/package_info.go b/pkg/config/registry/package_info.go index 71e534ca6..89ec5bbae 100644 --- a/pkg/config/registry/package_info.go +++ b/pkg/config/registry/package_info.go @@ -2,8 +2,10 @@ package registry import ( "fmt" + "net/url" "path" "path/filepath" + "regexp" "strings" "github.com/aquaproj/aqua/v2/pkg/runtime" @@ -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 { diff --git a/pkg/controller/remove/remove.go b/pkg/controller/remove/remove.go index ef6d67e89..965f79702 100644 --- a/pkg/controller/remove/remove.go +++ b/pkg/controller/remove/remove.go @@ -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" ) @@ -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 { diff --git a/tests/main/aqua-global.yaml b/tests/main/aqua-global.yaml index ecc091fd6..948a12de4 100644 --- a/tests/main/aqua-global.yaml +++ b/tests/main/aqua-global.yaml @@ -12,7 +12,8 @@ registries: path: registry.yaml packages: - name: x-motemen/ghq@v1.3.0 -- name: kubernetes-sigs/kind # http package, raw format +- name: hashicorp/terraform@v1.9.6 +- name: kubernetes-sigs/kind # raw format registry: standard # standard registry version: v0.17.0 # renovate: depName=kubernetes-sigs/kind - name: restic/restic@v0.14.0