diff --git a/pkg/cli/runner.go b/pkg/cli/runner.go index 9b20dae7a..ea4980235 100644 --- a/pkg/cli/runner.go +++ b/pkg/cli/runner.go @@ -52,6 +52,8 @@ func (runner *Runner) setParam(c *cli.Context, param *config.Param) error { return fmt.Errorf("get the current directory: %w", err) } param.PWD = wd + homeDir, _ := os.UserHomeDir() // TODO error handling + param.HomeDir = homeDir return nil } diff --git a/pkg/config/aqua/registry.go b/pkg/config/aqua/registry.go index 3d6261a37..50a414a58 100644 --- a/pkg/config/aqua/registry.go +++ b/pkg/config/aqua/registry.go @@ -1,7 +1,9 @@ package aqua import ( + "os" "path/filepath" + "strings" "github.com/sirupsen/logrus" "github.com/suzuki-shunsuke/logrus-error/logerr" @@ -80,13 +82,20 @@ func (registry *Registry) UnmarshalYAML(unmarshal func(interface{}) error) error return nil } -func (registry *Registry) GetFilePath(rootDir, cfgFilePath string) string { +func (registry *Registry) getLocalPath(homeDir, cfgFilePath string) string { + if filepath.IsAbs(registry.Path) { + return registry.Path + } + if strings.HasPrefix(registry.Path, "~"+string(os.PathSeparator)) { + return filepath.Join(homeDir, registry.Path[2:]) + } + return filepath.Join(filepath.Dir(cfgFilePath), registry.Path) +} + +func (registry *Registry) GetFilePath(rootDir, homeDir, cfgFilePath string) string { switch registry.Type { case RegistryTypeLocal: - if filepath.IsAbs(registry.Path) { - return registry.Path - } - return filepath.Join(filepath.Dir(cfgFilePath), registry.Path) + return registry.getLocalPath(homeDir, cfgFilePath) case RegistryTypeGitHubContent: return filepath.Join(rootDir, "registries", registry.Type, "github.com", registry.RepoOwner, registry.RepoName, registry.Ref, registry.Path) } diff --git a/pkg/config/aqua/registry_test.go b/pkg/config/aqua/registry_test.go index 197bae8b5..0ae8c9762 100644 --- a/pkg/config/aqua/registry_test.go +++ b/pkg/config/aqua/registry_test.go @@ -92,13 +92,14 @@ func TestRegistry_Validate(t *testing.T) { //nolint:funlen } } -func TestLocalRegistry_GetFilePath(t *testing.T) { +func TestRegistry_GetFilePath(t *testing.T) { t.Parallel() data := []struct { title string exp string registry *aqua.Registry rootDir string + homeDir string cfgFilePath string }{ { @@ -111,27 +112,17 @@ func TestLocalRegistry_GetFilePath(t *testing.T) { Type: "local", }, }, - } - for _, d := range data { - d := d - t.Run(d.title, func(t *testing.T) { - t.Parallel() - if p := d.registry.GetFilePath(d.rootDir, d.cfgFilePath); p != d.exp { - t.Fatalf("wanted %s, got %s", d.exp, p) - } - }) - } -} - -func TestRegistry_GetFilePath(t *testing.T) { - t.Parallel() - data := []struct { - title string - exp string - registry *aqua.Registry - rootDir string - cfgFilePath string - }{ + { + title: "home dir", + exp: "/home/foo/registry.yaml", + rootDir: "/root/.aqua", + cfgFilePath: "ci/aqua.yaml", + homeDir: "/home/foo", + registry: &aqua.Registry{ + Path: "~/registry.yaml", + Type: "local", + }, + }, { title: "github_content", exp: "/root/.aqua/registries/github_content/github.com/aquaproj/aqua-registry/v0.8.0/foo.yaml", @@ -149,7 +140,7 @@ func TestRegistry_GetFilePath(t *testing.T) { d := d t.Run(d.title, func(t *testing.T) { t.Parallel() - if p := d.registry.GetFilePath(d.rootDir, d.cfgFilePath); p != d.exp { + if p := d.registry.GetFilePath(d.rootDir, d.homeDir, d.cfgFilePath); p != d.exp { t.Fatalf("wanted %s, got %s", d.exp, p) } }) diff --git a/pkg/config/package.go b/pkg/config/package.go index 4f28db5ee..a24794b8d 100644 --- a/pkg/config/package.go +++ b/pkg/config/package.go @@ -171,6 +171,7 @@ type Param struct { RootDir string MaxParallelism int PWD string + HomeDir string } func (cpkg *Package) RenderAsset(rt *runtime.Runtime) (string, error) { diff --git a/pkg/install-registry/install.go b/pkg/install-registry/install.go index cab43baea..1e7700d45 100644 --- a/pkg/install-registry/install.go +++ b/pkg/install-registry/install.go @@ -95,7 +95,7 @@ const dirPermission os.FileMode = 0o775 // installRegistry installs and reads the registry file and returns the registry content. // If the registry file already exists, the installation is skipped. func (inst *installer) installRegistry(ctx context.Context, regist *aqua.Registry, cfgFilePath string, logE *logrus.Entry) (*registry.Config, error) { - registryFilePath := regist.GetFilePath(inst.param.RootDir, cfgFilePath) + registryFilePath := regist.GetFilePath(inst.param.RootDir, inst.param.HomeDir, cfgFilePath) if _, err := inst.fs.Stat(registryFilePath); err == nil { registryContent := ®istry.Config{} if err := inst.readRegistry(registryFilePath, registryContent); err != nil {