Skip to content

Commit

Permalink
feat: add visitedSpace for visitor (#512)
Browse files Browse the repository at this point in the history
Signed-off-by: zongz <[email protected]>
  • Loading branch information
zong-zhe authored Oct 31, 2024
1 parent e9b0854 commit 83bfcbc
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 11 deletions.
5 changes: 5 additions & 0 deletions pkg/visitor/test_data/test_visited_space/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "helloworld"
edition = "*"
version = "0.1.2"

Empty file.
1 change: 1 addition & 0 deletions pkg/visitor/test_data/test_visited_space/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
32 changes: 21 additions & 11 deletions pkg/visitor/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -91,6 +89,7 @@ type RemoteVisitor struct {
*PkgVisitor
EnableCache bool
CachePath string
VisitedSpace string
Downloader downloader.Downloader
InsecureSkipTLSverify bool
}
Expand All @@ -110,23 +109,34 @@ 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)
if err != nil {
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),
Expand All @@ -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
}
Expand Down
25 changes: 25 additions & 0 deletions pkg/visitor/visitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 83bfcbc

Please sign in to comment.