Skip to content

Commit

Permalink
Merge pull request kcl-lang#354 from officialasishkumar/refactor-dowl…
Browse files Browse the repository at this point in the history
…oading-git-repo-via-gitDownloader

Refactor dowloading git repo via git downloader
  • Loading branch information
Peefy authored May 30, 2024
2 parents 7dd3dc5 + e496a6f commit 7a35562
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
7 changes: 6 additions & 1 deletion pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,12 @@ func (c *KpmClient) AcquireTheLatestOciVersion(ociSource pkg.Oci) (string, error
// Download will download the dependency to the local path.
func (c *KpmClient) Download(dep *pkg.Dependency, homePath, localPath string) (*pkg.Dependency, error) {
if dep.Source.Git != nil {
_, err := c.DownloadFromGit(dep.Source.Git, localPath)
err := c.DepDownloader.Download(*downloader.NewDownloadOptions(
downloader.WithLocalPath(localPath),
downloader.WithSource(dep.Source),
downloader.WithLogWriter(c.logWriter),
downloader.WithSettings(c.settings),
))
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func initTestDir(subDir string) string {
return testDir
}

// TestDownloadGit test download from oci registry.
// TestDownloadOci test download from oci registry.
func TestDownloadOci(t *testing.T) {
testPath := filepath.Join(getTestDir("download"), "helloworld_0.1.2")
err := os.MkdirAll(testPath, 0755)
Expand Down
4 changes: 1 addition & 3 deletions pkg/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import (
"errors"
"fmt"
"io"
"path/filepath"

v1 "github.com/opencontainers/image-spec/specs-go/v1"
"kcl-lang.io/kpm/pkg/constants"
"kcl-lang.io/kpm/pkg/git"
"kcl-lang.io/kpm/pkg/oci"
pkg "kcl-lang.io/kpm/pkg/package"
Expand Down Expand Up @@ -170,7 +168,7 @@ func (d *GitDownloader) Download(opts DownloadOptions) error {
git.WithBranch(gitSource.Branch),
git.WithTag(gitSource.Tag),
git.WithRepoURL(gitSource.Url),
git.WithLocalPath(filepath.Join(opts.LocalPath, constants.GitEntry)),
git.WithLocalPath(opts.LocalPath),
)

if err != nil {
Expand Down
34 changes: 30 additions & 4 deletions pkg/downloader/downloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ func getTestDir(subDir string) string {
}

func TestOciDownloader(t *testing.T) {
path := getTestDir("test_oci")
path_oci := getTestDir("test_oci")
if err := os.MkdirAll(path_oci, os.ModePerm); err != nil {
t.Fatal(err)
}

defer func() {
_ = os.RemoveAll(path)
_ = os.RemoveAll(path_oci)
}()

ociDownloader := OciDownloader{
Expand All @@ -39,9 +42,32 @@ func TestOciDownloader(t *testing.T) {
Tag: "0.0.3",
},
}),
WithLocalPath(path),
WithLocalPath(path_oci),
))

assert.Equal(t, err, nil)
assert.Equal(t, true, utils.DirExists(filepath.Join(path_oci, "artifact.tgz")))

path_git := getTestDir("test_git")
if err := os.MkdirAll(path_oci, os.ModePerm); err != nil {
t.Fatal(err)
}

defer func() {
_ = os.RemoveAll(path_git)
}()

gitDownloader := GitDownloader{}

err = gitDownloader.Download(*NewDownloadOptions(
WithSource(pkg.Source{
Git: &pkg.Git{
Url: "https://github.com/kcl-lang/flask-demo-kcl-manifests.git",
Commit: "ade147b",
},
}),
WithLocalPath(path_git),
))

assert.Equal(t, err, nil)
assert.Equal(t, true, utils.DirExists(filepath.Join(path, "artifact.tgz")))
}

0 comments on commit 7a35562

Please sign in to comment.