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 ocidownloader to pull oci package #305

Merged
merged 8 commits into from
Apr 28, 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
44 changes: 39 additions & 5 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"oras.land/oras-go/v2"

"kcl-lang.io/kpm/pkg/constants"
"kcl-lang.io/kpm/pkg/downloader"
"kcl-lang.io/kpm/pkg/env"
"kcl-lang.io/kpm/pkg/errors"
"kcl-lang.io/kpm/pkg/git"
Expand All @@ -34,6 +35,8 @@ import (
type KpmClient struct {
// The writer of the log.
logWriter io.Writer
// The downloader of the dependencies.
DepDownloader *downloader.DepDownloader
// The home path of kpm for global configuration file and kcl package storage path.
homePath string
// The settings of kpm loaded from the global configuration file.
Expand All @@ -56,9 +59,10 @@ func NewKpmClient() (*KpmClient, error) {
}

return &KpmClient{
logWriter: os.Stdout,
settings: *settings,
homePath: homePath,
logWriter: os.Stdout,
settings: *settings,
homePath: homePath,
DepDownloader: &downloader.DepDownloader{},
}, nil
}

Expand Down Expand Up @@ -429,6 +433,15 @@ func (c *KpmClient) CompileWithOpts(opts *opt.CompileOptions) (*kcl.KCLResultLis
return compileResult, nil
}

// RunWithOpts will compile the kcl package with the compile options.
func (c *KpmClient) RunWithOpts(opts ...opt.Option) (*kcl.KCLResultList, error) {
mergedOpts := opt.DefaultCompileOptions()
for _, opt := range opts {
opt(mergedOpts)
}
return c.CompileWithOpts(mergedOpts)
}

// CompilePkgWithOpts will compile the kcl package with the compile options.
func (c *KpmClient) CompilePkgWithOpts(kclPkg *pkg.KclPkg, opts *opt.CompileOptions) (*kcl.KCLResultList, error) {
opts.SetPkgPath(kclPkg.HomePath)
Expand Down Expand Up @@ -836,11 +849,32 @@ func (c *KpmClient) Download(dep *pkg.Dependency, homePath, localPath string) (*
}

if dep.Source.Oci != nil {
kpkg, err := c.DownloadPkgFromOci(dep.Source.Oci, 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
}
dep.FromKclPkg(kpkg)
dpkg, err := pkg.FindFirstKclPkgFrom(localPath)
if err != nil {
return nil, err
}
dep.FromKclPkg(dpkg)

if dep.LocalFullPath == "" {
dep.LocalFullPath = localPath
}

if localPath != dep.LocalFullPath {
err = os.Rename(localPath, dep.LocalFullPath)
if err != nil {
return nil, err
}
}

// Creating symbolic links in a global cache is not an optimal solution.
// This allows kclvm to locate the package by default.
// This feature is unstable and will be removed soon.
Expand Down
54 changes: 53 additions & 1 deletion pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"kcl-lang.io/kcl-go/pkg/kcl"
"kcl-lang.io/kpm/pkg/downloader"
"kcl-lang.io/kpm/pkg/env"
"kcl-lang.io/kpm/pkg/git"
"kcl-lang.io/kpm/pkg/opt"
Expand Down Expand Up @@ -143,7 +144,7 @@ func TestDownloadLatestOci(t *testing.T) {
assert.Equal(t, dep.Source.Oci.Reg, "ghcr.io")
assert.Equal(t, dep.Source.Oci.Repo, "kcl-lang/helloworld")
assert.Equal(t, dep.Source.Oci.Tag, "0.1.1")
assert.Equal(t, dep.LocalFullPath, testPath+"0.1.1")
assert.Equal(t, dep.LocalFullPath, testPath)
assert.Equal(t, err, nil)

// Check whether the tar downloaded by `kpm add` has been deleted.
Expand Down Expand Up @@ -1437,3 +1438,54 @@ func TestLoadOciUrlDiffSetting(t *testing.T) {
assert.Equal(t, pkg.ModFile.Deps["oci_pkg"].Oci.Tag, "0.0.1")
assert.Equal(t, err, nil)
}

func TestAddWithOciDownloader(t *testing.T) {
kpmCli, err := NewKpmClient()
path := getTestDir("test_oci_downloader")
assert.Equal(t, err, nil)

kpmCli.DepDownloader = downloader.NewOciDownloader("linux/amd64")
kpkg, err := kpmCli.LoadPkgFromPath(filepath.Join(path, "add_dep", "pkg"))
assert.Equal(t, err, nil)
dep := pkg.Dependency{
Name: "helloworld",
FullName: "helloworld_0.0.3",
Source: pkg.Source{
Oci: &pkg.Oci{
Reg: "ghcr.io",
Repo: "zong-zhe/helloworld",
Tag: "0.0.3",
},
},
}
kpkg.HomePath = filepath.Join(path, "add_dep", "pkg")
err = kpmCli.AddDepToPkg(kpkg, &dep)
assert.Equal(t, err, nil)
kpkg.NoSumCheck = false
err = kpkg.UpdateModAndLockFile()
assert.Equal(t, err, nil)

expectmod := filepath.Join(path, "add_dep", "pkg", "except")
expectmodContent, err := os.ReadFile(expectmod)
assert.Equal(t, err, nil)
gotContent, err := os.ReadFile(filepath.Join(path, "add_dep", "pkg", "kcl.mod"))
assert.Equal(t, err, nil)
assert.Equal(t, utils.RmNewline(string(expectmodContent)), utils.RmNewline(string(gotContent)))
}

func TestRunWithOciDownloader(t *testing.T) {
kpmCli, err := NewKpmClient()
path := getTestDir("test_oci_downloader")
assert.Equal(t, err, nil)

kpmCli.DepDownloader = downloader.NewOciDownloader("linux/amd64")

res, err := kpmCli.RunWithOpts(
opt.WithEntries([]string{filepath.Join(path, "run_pkg", "pkg", "main.k")}),
opt.WithKclOption(kcl.WithWorkDir(filepath.Join(path, "run_pkg", "pkg"))),
opt.WithNoSumCheck(true),
opt.WithLogWriter(nil),
)
assert.Equal(t, err, nil)
assert.Equal(t, res.GetRawYamlResult(), "The_first_kcl_program: Hello World!")
}
7 changes: 7 additions & 0 deletions pkg/client/test_data/test_oci_downloader/add_dep/pkg/except
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "pkg"
edition = "v0.8.0"
version = "0.0.1"

[dependencies]
helloworld = { oci = "oci://ghcr.io/zong-zhe/helloworld", tag = "0.0.3" }
7 changes: 7 additions & 0 deletions pkg/client/test_data/test_oci_downloader/add_dep/pkg/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "pkg"
edition = "v0.8.0"
version = "0.0.1"

[dependencies]
helloworld = { oci = "oci://ghcr.io/zong-zhe/helloworld", tag = "0.0.3" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[dependencies]
[dependencies.helloworld]
name = "helloworld"
full_name = "helloworld_0.0.3"
version = "0.0.3"
sum = "OS3OHaC8HE5Z/4KEWJbS8xyHrYVSvXqHYxwy0BGc7nM="
reg = "ghcr.io"
repo = "zong-zhe/helloworld"
oci_tag = "0.0.3"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
7 changes: 7 additions & 0 deletions pkg/client/test_data/test_oci_downloader/run_pkg/pkg/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "pkg"
edition = "v0.8.0"
version = "0.0.1"

[dependencies]
helloworld = { oci = "oci://ghcr.io/zong-zhe/helloworld", tag = "0.0.3" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[dependencies]
[dependencies.helloworld]
name = "helloworld"
full_name = "helloworld_0.0.3"
version = "0.0.3"
sum = "OS3OHaC8HE5Z/4KEWJbS8xyHrYVSvXqHYxwy0BGc7nM="
reg = "ghcr.io"
repo = "zong-zhe/helloworld"
oci_tag = "0.0.3"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
2 changes: 1 addition & 1 deletion pkg/cmd/cmd_push.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023 The KCL Authors. All rights reserved.
// Deprecated: The entire contents of this file will be deprecated.
// Deprecated: The entire contents of this file will be deprecated.
// Please use the kcl cli - https://github.com/kcl-lang/cli.

package cmd
Expand Down
1 change: 1 addition & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
DEFAULT_KCL_OCI_MANIFEST_SUM = "org.kcllang.package.sum"
DEFAULT_CREATE_OCI_MANIFEST_TIME = "org.opencontainers.image.created"
URL_PATH_SEPARATOR = "/"
LATEST = "latest"

// The pattern of the external package argument.
EXTERNAL_PKGS_ARG_PATTERN = "%s=%s"
Expand Down
Loading
Loading