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: enable you to remove go_install and http packages #3101

Merged
merged 16 commits into from
Sep 21, 2024
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
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