Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add visitedSpace for visitor #512

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}