Skip to content

Commit

Permalink
feat: replace "~" to home directory in local registry path
Browse files Browse the repository at this point in the history
  • Loading branch information
suzuki-shunsuke committed Jun 24, 2022
1 parent 7828808 commit f588e01
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 29 deletions.
2 changes: 2 additions & 0 deletions pkg/cli/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
19 changes: 14 additions & 5 deletions pkg/config/aqua/registry.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package aqua

import (
"os"
"path/filepath"
"strings"

"github.com/sirupsen/logrus"
"github.com/suzuki-shunsuke/logrus-error/logerr"
Expand Down Expand Up @@ -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)
}
Expand Down
37 changes: 14 additions & 23 deletions pkg/config/aqua/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}{
{
Expand All @@ -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",
Expand All @@ -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)
}
})
Expand Down
1 change: 1 addition & 0 deletions pkg/config/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ type Param struct {
RootDir string
MaxParallelism int
PWD string
HomeDir string
}

func (cpkg *Package) RenderAsset(rt *runtime.Runtime) (string, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/install-registry/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := &registry.Config{}
if err := inst.readRegistry(registryFilePath, registryContent); err != nil {
Expand Down

0 comments on commit f588e01

Please sign in to comment.