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(vacuum): add vacuum management for cleaning expired packages #3442

Closed
wants to merge 73 commits into from
Closed
Show file tree
Hide file tree
Changes from 67 commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
bb507ac
chore(lint): refacto setParam to remove complexity
NikitaCOEUR Jan 3, 2025
cf7cc90
chore(deps): add go-humanize and bbolt dependencies
NikitaCOEUR Jan 7, 2025
c867e8d
feat(vacuum): implement StoreQueue and Controller for vacuum management
NikitaCOEUR Jan 7, 2025
90afe12
test(vacuum): add unit tests for vacuum controller
NikitaCOEUR Jan 7, 2025
491a793
feat(vacuum): define AQUA_VACUUM_DAYS env var
NikitaCOEUR Jan 7, 2025
a74ad33
feat(cli): add vacuum command for managing expired packages
NikitaCOEUR Jan 7, 2025
36950ef
feat(installer): add vacuum controller integration
NikitaCOEUR Jan 7, 2025
61308d6
feat(controller): integrate vacuum controller into exec controller
NikitaCOEUR Jan 7, 2025
c5858fd
feat(controller): add vacuum command controller initialization
NikitaCOEUR Jan 7, 2025
6a0d661
chore(wire): cmdx wire to regenerate it
NikitaCOEUR Jan 7, 2025
cf97223
test(exec): remove unused function
NikitaCOEUR Jan 8, 2025
62066f4
chore(vacuum): make newStoreQueue private function
NikitaCOEUR Jan 8, 2025
046ab6e
chore(install): defer close vacuum to ensure async finalized
NikitaCOEUR Jan 8, 2025
0162444
chore(vacuum): use int instead of pointer
NikitaCOEUR Jan 10, 2025
77bcc0e
chore(vacuum): refactor CLI commands for vacuum operations
NikitaCOEUR Jan 10, 2025
f09bf45
chore(vacuum): use public functions instead of mode
NikitaCOEUR Jan 10, 2025
7290f55
chore(vacuum): refactor vacuum closing logic and update controller in…
NikitaCOEUR Jan 10, 2025
b5a7651
chore(controller): update wire_gen.go via cmdx wire
NikitaCOEUR Jan 10, 2025
2cb4eb0
chore(mod): update go.mod with go mod tidy
NikitaCOEUR Jan 10, 2025
84636e4
tests(vacuum): fix lint tests
NikitaCOEUR Jan 10, 2025
24c573f
tests(vacuum): remove unused param
NikitaCOEUR Jan 10, 2025
a8b1d21
tests(vacuum): remove unused const
NikitaCOEUR Jan 10, 2025
11ff335
chore(cli): remove default vacuum days setting
NikitaCOEUR Jan 11, 2025
470a28e
docs(vacuum): update command descriptions for clarity
NikitaCOEUR Jan 11, 2025
6a18879
fix(controller): use filepath.Join for constructing database file path
NikitaCOEUR Jan 11, 2025
f394fe9
fix(controller): improve error logging during database operation retries
NikitaCOEUR Jan 11, 2025
d9e093e
fix(controller): simplify error handling in database update and view …
NikitaCOEUR Jan 11, 2025
db19074
fix(controller): use filepath.Join for database file path constructio…
NikitaCOEUR Jan 11, 2025
4325fbd
fix(controller): enhance error handling in TestKeepDBOpen and update …
NikitaCOEUR Jan 11, 2025
a67cb3a
fix(controller): improve error logging in package decoding and simpli…
NikitaCOEUR Jan 11, 2025
8b0b7cd
fix(controller): enhance error logging for package removal and improv…
NikitaCOEUR Jan 11, 2025
2001127
fix(controller): update logging fields for package storage to improve…
NikitaCOEUR Jan 11, 2025
81df2bd
fix(controller): simplify error messages in package display and encod…
NikitaCOEUR Jan 11, 2025
d8ae573
fix(controller): remove parent directory cleanup to keep aqua simple
NikitaCOEUR Jan 11, 2025
1b71a57
chore(lint): fix lint error
NikitaCOEUR Jan 11, 2025
d2000a8
test(controller): add unit tests for package entry encoding and decoding
NikitaCOEUR Jan 11, 2025
e1d47fb
test(workflow): add integration test for vacuum run command
NikitaCOEUR Jan 11, 2025
fc4ead0
chore(vacuum): fix lint tests
NikitaCOEUR Jan 11, 2025
abd0bf4
ci(vacuum): run vacuum command before update aqua
NikitaCOEUR Jan 11, 2025
07fcc26
fix: replace time.Sleep with timer.Wait to support cancel
suzuki-shunsuke Jan 11, 2025
94e7725
ci(workflow): add test aqua command with vacuum enabled
NikitaCOEUR Jan 11, 2025
fa79338
fix: replace time.Sleep with timer.Wait for cancel
suzuki-shunsuke Jan 11, 2025
a66f099
fix: handle an error of timer.Wait
suzuki-shunsuke Jan 11, 2025
fdd9ff0
tests(vacuum): set specific logger for each test
NikitaCOEUR Jan 11, 2025
99f6456
tests(vacuum): replace assert and require with testing and go-cmp
NikitaCOEUR Jan 11, 2025
457e699
tests(vacuum): fix lints
NikitaCOEUR Jan 11, 2025
1b55c9d
tests(vacuum): add sleep to ensure logs catched before end of test
NikitaCOEUR Jan 11, 2025
087e6f2
chore(mod): execute go mod tidy
NikitaCOEUR Jan 11, 2025
ee5e23c
test(vacuum): add packages to let all async log on fail appears
NikitaCOEUR Jan 11, 2025
eb86893
test(vacuum): replace afero.TempDir with t.TempDir
NikitaCOEUR Jan 12, 2025
7721951
refactor: minimize the scope of err variables
suzuki-shunsuke Jan 20, 2025
4fa6410
refactor: replace contains with strings.HasPrefix
suzuki-shunsuke Jan 20, 2025
ec0a987
refactor: remove generatePackageKey
suzuki-shunsuke Jan 20, 2025
eda0509
fix: fix pkgPath
suzuki-shunsuke Jan 20, 2025
24e1d7e
refactor: refactor
suzuki-shunsuke Jan 20, 2025
a6353c1
fix: skip updating DB when a package install is skipped
suzuki-shunsuke Jan 21, 2025
1a3f886
refactor: separate code about DB
suzuki-shunsuke Jan 21, 2025
ffbd7e0
refactor: rename a method
suzuki-shunsuke Jan 21, 2025
6e65072
refactor: separate code about DB
suzuki-shunsuke Jan 21, 2025
596a6a4
fix: fix lint errors
suzuki-shunsuke Jan 21, 2025
3070a5d
fix: fix compile errors
suzuki-shunsuke Jan 21, 2025
2fb9ef4
refactor: refactor
suzuki-shunsuke Jan 21, 2025
3c24a2a
refactor: refactor
suzuki-shunsuke Jan 21, 2025
c48052c
refactor: close database in cli packages
suzuki-shunsuke Jan 21, 2025
0f035e1
fix: stop updating timestamp twice
suzuki-shunsuke Jan 21, 2025
b875c9c
fix: suppress a lint error
suzuki-shunsuke Jan 21, 2025
45981e4
refactor: separate code
suzuki-shunsuke Jan 21, 2025
4771a90
fix(exec): fix a bug that DB isn't closed
suzuki-shunsuke Jan 21, 2025
f815a6a
style: sort methods
suzuki-shunsuke Jan 21, 2025
8eb89a0
fix: remove package type from db
suzuki-shunsuke Jan 21, 2025
feb29c2
refactor: rename a field
suzuki-shunsuke Jan 21, 2025
0dadc30
refactor: remove an unused field
suzuki-shunsuke Jan 21, 2025
337188f
refactor: refactor
suzuki-shunsuke Jan 21, 2025
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
16 changes: 16 additions & 0 deletions .github/workflows/wc-integration-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,25 @@ jobs:
- run: aqua list
working-directory: /tmp

# Test if commands succeed with vacuum enabled
- name: Test aqua install with AQUA_VACUUM_DAYS
env:
AQUA_VACUUM_DAYS: 1
run: aqua install
- name: Test aqua exec with AQUA_VACUUM_DAYS
env:
AQUA_VACUUM_DAYS: 1
run: cmdx -v
- name: Test vacuum Run command
env:
AQUA_VACUUM_DAYS: 1
run: aqua vacuum run

# Test update-aqua command
- name: Test update-aqua
run: aqua update-aqua


integration-test-cargo:
timeout-minutes: 30
runs-on: ubuntu-latest
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ require (
github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dsnet/compress v0.0.2-0.20230904184137-39efe44ab707 // indirect
github.com/dustin/go-humanize v1.0.1
github.com/gdamore/encoding v1.0.0 // indirect
github.com/gdamore/tcell/v2 v2.6.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
Expand Down Expand Up @@ -82,6 +83,7 @@ require (
github.com/therootcompany/xz v1.0.1 // indirect
github.com/ulikunitz/xz v0.5.12 // indirect
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
go.etcd.io/bbolt v1.3.11
go4.org v0.0.0-20230225012048-214862532bf5 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/sync v0.9.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/dsnet/compress v0.0.2-0.20230904184137-39efe44ab707 h1:2tV76y6Q9BB+NEBasnqvs7e49aEBFI8ejC89PSnWH+4=
github.com/dsnet/compress v0.0.2-0.20230904184137-39efe44ab707/go.mod h1:qssHWj60/X5sZFNxpG4HBPDHVqxNm4DfnCKgrbZOT+s=
github.com/dsnet/golib v0.0.0-20171103203638-1ea166775780/go.mod h1:Lj+Z9rebOhdfkVLjJ8T6VcRQv3SXugXy999NBtR9aFY=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/expr-lang/expr v1.16.9 h1:WUAzmR0JNI9JCiF0/ewwHB1gmcGw5wW7nWt8gc6PpCI=
Expand Down Expand Up @@ -255,6 +257,8 @@ github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBi
github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZqKjWU=
github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.etcd.io/bbolt v1.3.11 h1:yGEzV1wPz2yVCLsD8ZAiGHhHVlczyC9d1rP43/VCRJ0=
go.etcd.io/bbolt v1.3.11/go.mod h1:dksAq7YMXoljX0xu6VF5DMZGbhYYoLUalEiSySYAS4I=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
Expand Down
7 changes: 5 additions & 2 deletions pkg/cli/exec/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ func (i *command) action(c *cli.Context) error {
}
defer profiler.Stop()

logE := i.r.LogE

param := &config.Param{}
if err := util.SetParam(c, i.r.LogE, "exec", param, i.r.LDFlags); err != nil {
if err := util.SetParam(c, logE, "exec", param, i.r.LDFlags); err != nil {
return fmt.Errorf("parse the command line arguments: %w", err)
}
ctrl, err := controller.InitializeExecCommandController(c.Context, param, http.DefaultClient, i.r.Runtime)
Expand All @@ -54,5 +56,6 @@ func (i *command) action(c *cli.Context) error {
if err != nil {
return fmt.Errorf("parse args: %w", err)
}
return ctrl.Exec(c.Context, i.r.LogE, param, exeName, args...) //nolint:wrapcheck
defer ctrl.CloseVacuum(logE)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that I don't think this works here, because aqua exec can use: https://github.com/aquaproj/aqua/blob/main/pkg/controller/exec/exec.go#L126.
And this (on UNIX) uses the "Exec" function of syscall_unix, which, when executed, replaces the calling process with the called process.
This cuts short the rest of the code, and the functions called in defer are not played. Finally, asynchronous processes are not necessarily terminated either...

Copy link
Member

@suzuki-shunsuke suzuki-shunsuke Jan 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see. Thank you.
I've confirmed defer function isn't executed.

package main

import (
	"log"
	"os"

	"golang.org/x/sys/unix"
)

func main() {
	if err := core(); err != nil {
		log.Fatal(err)
	}
}

func core() error {
	defer func() {
		log.Println("defer")
		if err := os.WriteFile("test.txt", []byte(`hello`), 0o644); err != nil {
			log.Print(err)
		}
	}()
	return exe()
}

func exe() error {
	return unix.Exec("/usr/bin/curl", []string{"curl", "--version"}, os.Environ())
}
$ go run main.go
curl 8.7.1 (x86_64-apple-darwin24.0) libcurl/8.7.1 (SecureTransport) LibreSSL/3.3.6 zlib/1.2.12 nghttp2/1.63.0
Release-Date: 2024-03-27
Protocols: dict file ftp ftps gopher gophers http https imap imaps ipfs ipns ldap ldaps mqtt pop3 pop3s rtsp smb smbs smtp smtps telnet tftp
Features: alt-svc AsynchDNS GSS-API HSTS HTTP2 HTTPS-proxy IPv6 Kerberos Largefile libz MultiSSL NTLM SPNEGO SSL threadsafe UnixSockets

$ ls # test.txt isn't created.
go.mod  go.sum  main.go

https://pkg.go.dev/golang.org/x/sys/unix#Exec

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return ctrl.Exec(c.Context, logE, param, exeName, args...) //nolint:wrapcheck
}
5 changes: 4 additions & 1 deletion pkg/cli/install/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,16 @@ func (i *command) action(c *cli.Context) error {
}
defer profiler.Stop()

logE := i.r.LogE

param := &config.Param{}
if err := util.SetParam(c, i.r.LogE, "install", param, i.r.LDFlags); err != nil {
if err := util.SetParam(c, logE, "install", param, i.r.LDFlags); err != nil {
return fmt.Errorf("parse the command line arguments: %w", err)
}
ctrl, err := controller.InitializeInstallCommandController(c.Context, param, http.DefaultClient, i.r.Runtime)
if err != nil {
return fmt.Errorf("initialize a InstallController: %w", err)
}
defer ctrl.CloseVacuum(logE)
return ctrl.Install(c.Context, i.r.LogE, param) //nolint:wrapcheck
}
2 changes: 2 additions & 0 deletions pkg/cli/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/aquaproj/aqua/v2/pkg/cli/update"
"github.com/aquaproj/aqua/v2/pkg/cli/updateaqua"
"github.com/aquaproj/aqua/v2/pkg/cli/util"
"github.com/aquaproj/aqua/v2/pkg/cli/vacuum"
"github.com/aquaproj/aqua/v2/pkg/cli/version"
"github.com/aquaproj/aqua/v2/pkg/cli/which"
"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -105,6 +106,7 @@ func Run(ctx context.Context, param *util.Param, args ...string) error { //nolin
upc.New,
remove.New,
update.New,
vacuum.New,
),
}

Expand Down
43 changes: 37 additions & 6 deletions pkg/cli/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,24 @@ type LDFlags struct {
Date string
}

func SetParam(c *cli.Context, logE *logrus.Entry, commandName string, param *config.Param, ldFlags *LDFlags) error { //nolint:funlen,cyclop
func SetParam(c *cli.Context, logE *logrus.Entry, commandName string, param *config.Param, ldFlags *LDFlags) error {
wd, err := os.Getwd()
if err != nil {
return fmt.Errorf("get the current directory: %w", err)
}
param.Args = c.Args().Slice()
if logLevel := c.String("log-level"); logLevel != "" {
param.LogLevel = logLevel
setBasicParams(c, logE, commandName, param, wd, ldFlags)
setLogParams(c, param, logE)
if err := setEnvParams(param); err != nil {
return fmt.Errorf("error during setting params from Env vars: %w", err)
}
if err := setChecksumParams(param); err != nil {
return fmt.Errorf("error during setting params from checksum params: %w", err)
}
return nil
}

func setBasicParams(c *cli.Context, logE *logrus.Entry, commandName string, param *config.Param, wd string, ldFlags *LDFlags) {
param.ConfigFilePath = c.String("config")
param.Dest = c.String("o")
param.OutTestData = c.String("out-testdata")
Expand All @@ -66,14 +75,11 @@ func SetParam(c *cli.Context, logE *logrus.Entry, commandName string, param *con
if cmd := c.String("cmd"); cmd != "" {
param.Commands = strings.Split(cmd, ",")
}
param.LogColor = os.Getenv("AQUA_LOG_COLOR")
param.AQUAVersion = ldFlags.Version
param.AquaCommitHash = ldFlags.Commit
param.RootDir = config.GetRootDir(osenv.New())
homeDir, _ := os.UserHomeDir()
param.HomeDir = homeDir
log.SetLevel(param.LogLevel, logE)
log.SetColor(param.LogColor, logE)
param.MaxParallelism = config.GetMaxParallelism(os.Getenv("AQUA_MAX_PARALLELISM"), logE)
param.GlobalConfigFilePaths = finder.ParseGlobalConfigFilePaths(wd, os.Getenv("AQUA_GLOBAL_CONFIG"))
param.Deep = c.Bool("deep")
Expand All @@ -84,7 +90,18 @@ func SetParam(c *cli.Context, logE *logrus.Entry, commandName string, param *con
param.ProgressBar = os.Getenv("AQUA_PROGRESS_BAR") == "true"
param.Tags = parseTags(strings.Split(c.String("tags"), ","))
param.ExcludedTags = parseTags(strings.Split(c.String("exclude-tags"), ","))
}

func setLogParams(c *cli.Context, param *config.Param, logE *logrus.Entry) {
if logLevel := c.String("log-level"); logLevel != "" {
param.LogLevel = logLevel
}
param.LogColor = os.Getenv("AQUA_LOG_COLOR")
log.SetLevel(param.LogLevel, logE)
log.SetColor(param.LogColor, logE)
}

func setEnvParams(param *config.Param) error { //nolint:cyclop
if a := os.Getenv("AQUA_DISABLE_LAZY_INSTALL"); a != "" {
disableLazyInstall, err := strconv.ParseBool(a)
if err != nil {
Expand All @@ -108,6 +125,20 @@ func SetParam(c *cli.Context, logE *logrus.Entry, commandName string, param *con
}
}
}
if a := os.Getenv("AQUA_VACUUM_DAYS"); a != "" {
vacuumDays, err := strconv.Atoi(a)
if err != nil {
return fmt.Errorf("parse the environment variable AQUA_VACUUM_DAYS as an integer: %w", err)
}
if vacuumDays <= 0 {
NikitaCOEUR marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("the environment variable AQUA_VACUUM_DAYS must be a positive integer: %d", vacuumDays)
}
param.VacuumDays = vacuumDays
}
return nil
}

func setChecksumParams(param *config.Param) error {
if a := os.Getenv("AQUA_CHECKSUM"); a != "" {
chksm, err := strconv.ParseBool(a)
if err != nil {
Expand Down
111 changes: 111 additions & 0 deletions pkg/cli/vacuum/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package vacuum

import (
"errors"
"fmt"
"net/http"

"github.com/aquaproj/aqua/v2/pkg/cli/profile"
"github.com/aquaproj/aqua/v2/pkg/cli/util"
"github.com/aquaproj/aqua/v2/pkg/config"
"github.com/aquaproj/aqua/v2/pkg/controller"
"github.com/suzuki-shunsuke/logrus-error/logerr"
"github.com/urfave/cli/v2"
)

const description = `Perform vacuuming tasks.

Enable vacuuming by setting the AQUA_VACUUM_DAYS environment variable to a value greater than 0.
This command removes versions of packages that have not been used for the specified number of days.

You can list all packages managed by the vacuum system or only expired packages.

# Show all packages managed by the vacuum system
$ aqua vacuum show

# Show only expired packages
$ aqua vacuum show --expired
$ aqua vacuum show -e

# Run vacuum cleaning
$ aqua vacuum run
`

type command struct {
r *util.Param
}

func New(r *util.Param) *cli.Command {
i := &command{
r: r,
}
return &cli.Command{
Name: "vacuum",
Usage: "Operate vacuuming tasks (If AQUA_VACUUM_DAYS is set)",
Aliases: []string{"v"},
Description: description,
Subcommands: []*cli.Command{
{
Name: "show",
Aliases: []string{"s"},
Usage: "Show packages managed by vacuum system",
Action: i.action,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "expired",
Usage: "Show only expired packages",
Aliases: []string{"e"},
},
},
},
{
Name: "run",
Aliases: []string{"r"},
Usage: "Run vacuum cleaning",
Action: i.action,
},
},
}
}

func (i *command) action(c *cli.Context) error {
profiler, err := profile.Start(c)
if err != nil {
return fmt.Errorf("start CPU Profile or tracing: %w", err)
}
defer profiler.Stop()

logE := i.r.LogE

param := &config.Param{}
if err := util.SetParam(c, logE, "vacuum", param, i.r.LDFlags); err != nil {
return fmt.Errorf("parse the command line arguments: %w", err)
}

if param.VacuumDays == 0 {
return errors.New("vacuum is not enabled, please set the AQUA_VACUUM_DAYS environment variable")
}
suzuki-shunsuke marked this conversation as resolved.
Show resolved Hide resolved

ctrl := controller.InitializeVacuumCommandController(c.Context, param, http.DefaultClient, i.r.Runtime)

defer func() {
if err := ctrl.Close(logE); err != nil {
logerr.WithError(logE, err).Error("close vacuum controller")
}
}()

if c.Command.Name == "show" {
if err := ctrl.ListPackages(c.Context, logE, c.Bool("expired")); err != nil {
return fmt.Errorf("show packages: %w", err)
}
return nil
}

if c.Command.Name == "run" {
if err := ctrl.Vacuum(c.Context, logE); err != nil {
return fmt.Errorf("run: %w", err)
}
}

return nil
}
21 changes: 11 additions & 10 deletions pkg/config/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ func (p *Package) ExePath(rootDir string, file *registry.File, rt *runtime.Runti
return filepath.Join(rootDir, "pkgs", pkgInfo.Type, "github.com", pkgInfo.RepoOwner, pkgInfo.RepoName, p.Package.Version, "bin", file.Name), nil
}

pkgPath, err := p.PkgPath(rootDir, rt)
pkgPath, err := p.PkgPath(rt)
if err != nil {
return "", err
}
fileSrc, err := p.fileSrc(file, rt)
if err != nil {
return "", fmt.Errorf("get a file path: %w", err)
}
return filepath.Join(pkgPath, fileSrc), nil
return filepath.Join(rootDir, pkgPath, fileSrc), nil
}

func (p *Package) RenderAsset(rt *runtime.Runtime) (string, error) {
Expand Down Expand Up @@ -80,7 +80,7 @@ func (p *Package) RenderPath() (string, error) {
return p.RenderTemplateString(pkgInfo.GetPath(), &runtime.Runtime{})
}

func (p *Package) PkgPath(rootDir string, rt *runtime.Runtime) (string, error) { //nolint:cyclop
func (p *Package) PkgPath(rt *runtime.Runtime) (string, error) { //nolint:cyclop
pkgInfo := p.PackageInfo
pkg := p.Package
assetName, err := p.RenderAsset(rt)
Expand All @@ -89,23 +89,23 @@ func (p *Package) PkgPath(rootDir string, rt *runtime.Runtime) (string, error) {
}
switch pkgInfo.Type {
case PkgInfoTypeGitHubArchive:
return filepath.Join(rootDir, "pkgs", pkgInfo.Type, "github.com", pkgInfo.RepoOwner, pkgInfo.RepoName, pkg.Version), nil
return filepath.Join("pkgs", pkgInfo.Type, "github.com", pkgInfo.RepoOwner, pkgInfo.RepoName, pkg.Version), nil
case PkgInfoTypeGoBuild:
return filepath.Join(rootDir, "pkgs", pkgInfo.Type, "github.com", pkgInfo.RepoOwner, pkgInfo.RepoName, pkg.Version, "src"), nil
return filepath.Join("pkgs", pkgInfo.Type, "github.com", pkgInfo.RepoOwner, pkgInfo.RepoName, pkg.Version, "src"), nil
case PkgInfoTypeGoInstall:
p, err := p.RenderPath()
if err != nil {
return "", fmt.Errorf("render Go Module Path: %w", err)
}
return filepath.Join(rootDir, "pkgs", pkgInfo.Type, p, pkg.Version, "bin"), nil
return filepath.Join("pkgs", pkgInfo.Type, p, pkg.Version, "bin"), nil
case PkgInfoTypeCargo:
registry := "crates.io"
return filepath.Join(rootDir, "pkgs", pkgInfo.Type, registry, pkgInfo.Crate, strings.TrimPrefix(pkg.Version, "v")), nil
return filepath.Join("pkgs", pkgInfo.Type, registry, pkgInfo.Crate, strings.TrimPrefix(pkg.Version, "v")), nil
case PkgInfoTypeGitHubContent, PkgInfoTypeGitHubRelease:
if pkgInfo.RepoOwner == "aquaproj" && (pkgInfo.RepoName == "aqua" || pkgInfo.RepoName == "aqua-proxy") {
return filepath.Join(rootDir, "internal", "pkgs", pkgInfo.Type, "github.com", pkgInfo.RepoOwner, pkgInfo.RepoName, pkg.Version, assetName), nil
return filepath.Join("internal", "pkgs", pkgInfo.Type, "github.com", pkgInfo.RepoOwner, pkgInfo.RepoName, pkg.Version, assetName), nil
}
return filepath.Join(rootDir, "pkgs", pkgInfo.Type, "github.com", pkgInfo.RepoOwner, pkgInfo.RepoName, pkg.Version, assetName), nil
return filepath.Join("pkgs", pkgInfo.Type, "github.com", pkgInfo.RepoOwner, pkgInfo.RepoName, pkg.Version, assetName), nil
case PkgInfoTypeHTTP:
uS, err := p.RenderURL(rt)
if err != nil {
Expand All @@ -115,7 +115,7 @@ func (p *Package) PkgPath(rootDir string, rt *runtime.Runtime) (string, error) {
if err != nil {
return "", fmt.Errorf("parse the URL: %w", err)
}
return filepath.Join(rootDir, "pkgs", pkgInfo.Type, u.Host, u.Path), nil
return filepath.Join("pkgs", pkgInfo.Type, u.Host, u.Path), nil
}
return "", nil
}
Expand Down Expand Up @@ -294,6 +294,7 @@ type Param struct {
Installed bool
PolicyConfigFilePaths []string
Commands []string
VacuumDays int
}

func appendExt(s, format string) string {
Expand Down
Loading
Loading