From 23f8f3a9f906179b5ac65e9fec3bc3341ee04ffc Mon Sep 17 00:00:00 2001 From: zongz Date: Wed, 30 Oct 2024 18:49:26 +0800 Subject: [PATCH] feat: add visitedSpace for visitor Signed-off-by: zongz --- .../test_data/test_visited_space/kcl.mod | 5 +++ .../test_data/test_visited_space/kcl.mod.lock | 0 .../test_data/test_visited_space/main.k | 1 + pkg/visitor/visitor.go | 32 ++++++++++++------- pkg/visitor/visitor_test.go | 25 +++++++++++++++ 5 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 pkg/visitor/test_data/test_visited_space/kcl.mod create mode 100644 pkg/visitor/test_data/test_visited_space/kcl.mod.lock create mode 100644 pkg/visitor/test_data/test_visited_space/main.k diff --git a/pkg/visitor/test_data/test_visited_space/kcl.mod b/pkg/visitor/test_data/test_visited_space/kcl.mod new file mode 100644 index 00000000..c0913a10 --- /dev/null +++ b/pkg/visitor/test_data/test_visited_space/kcl.mod @@ -0,0 +1,5 @@ +[package] +name = "helloworld" +edition = "*" +version = "0.1.2" + diff --git a/pkg/visitor/test_data/test_visited_space/kcl.mod.lock b/pkg/visitor/test_data/test_visited_space/kcl.mod.lock new file mode 100644 index 00000000..e69de29b diff --git a/pkg/visitor/test_data/test_visited_space/main.k b/pkg/visitor/test_data/test_visited_space/main.k new file mode 100644 index 00000000..fa7048e6 --- /dev/null +++ b/pkg/visitor/test_data/test_visited_space/main.k @@ -0,0 +1 @@ +The_first_kcl_program = 'Hello World!' \ No newline at end of file diff --git a/pkg/visitor/visitor.go b/pkg/visitor/visitor.go index bea5bc00..e6e23332 100644 --- a/pkg/visitor/visitor.go +++ b/pkg/visitor/visitor.go @@ -4,10 +4,8 @@ import ( "fmt" "io" "os" - "path/filepath" "github.com/google/uuid" - "kcl-lang.io/kpm/pkg/constants" "kcl-lang.io/kpm/pkg/downloader" "kcl-lang.io/kpm/pkg/opt" pkg "kcl-lang.io/kpm/pkg/package" @@ -91,6 +89,7 @@ type RemoteVisitor struct { *PkgVisitor EnableCache bool CachePath string + VisitedSpace string Downloader downloader.Downloader InsecureSkipTLSverify bool } @@ -110,13 +109,25 @@ func (rv *RemoteVisitor) Visit(s *downloader.Source, v visitFunc) error { return fmt.Errorf("source is not remote") } - tmpDir, err := os.MkdirTemp("", "") - if err != nil { - return err + var visitedSpace string + var err error + if len(rv.VisitedSpace) != 0 { + visitedSpace = rv.VisitedSpace + } else { + tmpDir, err := os.MkdirTemp("", "") + if err != nil { + return err + } + + visitedSpace = tmpDir + defer os.RemoveAll(tmpDir) } - if s.Git != nil { - tmpDir = filepath.Join(tmpDir, constants.GitScheme) + if !utils.DirExists(visitedSpace) { + err := os.MkdirAll(visitedSpace, 0755) + if err != nil { + return err + } } credCli, err := downloader.LoadCredentialFile(rv.Settings.CredentialsFile) @@ -124,9 +135,8 @@ func (rv *RemoteVisitor) Visit(s *downloader.Source, v visitFunc) error { return err } - defer os.RemoveAll(tmpDir) err = rv.Downloader.Download(*downloader.NewDownloadOptions( - downloader.WithLocalPath(tmpDir), + downloader.WithLocalPath(visitedSpace), downloader.WithSource(*s), downloader.WithLogWriter(rv.LogWriter), downloader.WithSettings(*rv.Settings), @@ -139,9 +149,9 @@ func (rv *RemoteVisitor) Visit(s *downloader.Source, v visitFunc) error { if err != nil { return err } - pkgPath := tmpDir + pkgPath := visitedSpace if !s.ModSpec.IsNil() { - pkgPath, err = utils.FindPackage(tmpDir, s.ModSpec.Name) + pkgPath, err = utils.FindPackage(visitedSpace, s.ModSpec.Name) if err != nil { return err } diff --git a/pkg/visitor/visitor_test.go b/pkg/visitor/visitor_test.go index e163c3ce..1d12a0cf 100644 --- a/pkg/visitor/visitor_test.go +++ b/pkg/visitor/visitor_test.go @@ -100,3 +100,28 @@ func TestVisitPkgRemote(t *testing.T) { assert.NilError(t, err) } } + +func TestVisitedSpace(t *testing.T) { + var buf bytes.Buffer + remotePkgVisitor := RemoteVisitor{ + PkgVisitor: &PkgVisitor{ + LogWriter: &buf, + Settings: settings.GetSettings(), + }, + VisitedSpace: getTestDir("test_visited_space"), + Downloader: &downloader.DepDownloader{}, + } + + source, err := downloader.NewSourceFromStr("oci://ghcr.io/kcl-lang/helloworld?tag=0.1.2") + if err != nil { + t.Fatal(err) + } + + err = remotePkgVisitor.Visit(source, func(pkg *pkg.KclPkg) error { + assert.Equal(t, pkg.GetPkgName(), "helloworld") + assert.Equal(t, pkg.GetPkgVersion(), "0.1.2") + assert.Equal(t, pkg.HomePath, filepath.Join(remotePkgVisitor.VisitedSpace)) + return nil + }) + assert.NilError(t, err) +}