Skip to content

Commit

Permalink
fix: fix test ci
Browse files Browse the repository at this point in the history
Signed-off-by: zongz <zongzhe1024@163.com>
  • Loading branch information
zong-zhe committed Dec 4, 2024
1 parent fb2ceba commit 202e2b6
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 69 deletions.
9 changes: 5 additions & 4 deletions pkg/client/deperated.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func (c *KpmClient) CompileOciPkg(ociSource, version string, opts *opt.CompileOp
source := downloader.Source{
Oci: &downloader.Oci{
Reg: ociOpts.Reg,
Repo: ociOpts.Repo,
Repo: utils.JoinPath(ociOpts.Repo, ociOpts.Ref),
Tag: ociOpts.Tag,
},
}
Expand Down Expand Up @@ -1338,7 +1338,7 @@ func (c *KpmClient) pullTarFromOci(localPath string, ociOpts *opt.OciOptions) er
return reporter.NewErrorEvent(reporter.Bug, err)
}

repoPath := utils.JoinPath(ociOpts.Reg, ociOpts.Repo)
repoPath := utils.JoinPath(ociOpts.Reg, ociOpts.Repo, ociOpts.Ref)
cred, err := c.GetCredentials(ociOpts.Reg)
if err != nil {
return err
Expand Down Expand Up @@ -1373,9 +1373,10 @@ func (c *KpmClient) pullTarFromOci(localPath string, ociOpts *opt.OciOptions) er

ociOpts.Tag = tagSelected

full_repo := utils.JoinPath(ociOpts.Reg, ociOpts.Repo)
full_repo := utils.JoinPath(ociOpts.Reg, ociOpts.Repo, ociOpts.Ref)
full_ref := utils.JoinPath(ociOpts.Repo, ociOpts.Ref)
reporter.ReportMsgTo(
fmt.Sprintf("pulling '%s:%s' from '%s'", ociOpts.Repo, tagSelected, full_repo),
fmt.Sprintf("pulling '%s:%s' from '%s'", full_ref, tagSelected, full_repo),
c.logWriter,
)

Expand Down
12 changes: 6 additions & 6 deletions pkg/client/issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"kcl-lang.io/kpm/pkg/downloader"
"kcl-lang.io/kpm/pkg/features"
"kcl-lang.io/kpm/pkg/mock"
"kcl-lang.io/kpm/pkg/opt"
pkg "kcl-lang.io/kpm/pkg/package"
"kcl-lang.io/kpm/pkg/reporter"
"kcl-lang.io/kpm/pkg/utils"
Expand Down Expand Up @@ -486,11 +485,12 @@ func TestKclIssue1768(t *testing.T) {

err = kpmcli.Push(
WithPushModPath(pushedModPath),
WithPushOciOptions(&opt.OciOptions{
Reg: "localhost:5001",
Repo: "test",
Ref: "oci_pushed_mod",
Tag: "v9.9.9",
WithPushSource(downloader.Source{
Oci: &downloader.Oci{
Reg: "localhost:5001",
Repo: "test/oci_pushed_mod",
Tag: "v9.9.9",
},
}),
)

Expand Down
76 changes: 40 additions & 36 deletions pkg/client/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"

"kcl-lang.io/kpm/pkg/downloader"
"kcl-lang.io/kpm/pkg/errors"
"kcl-lang.io/kpm/pkg/oci"
"kcl-lang.io/kpm/pkg/opt"
Expand All @@ -19,28 +20,25 @@ type PushOptions struct {
// repository is set to the default repository `kcl-lang` in the settings
// reference is set to the package name in `kcl.mod`
// tag is set to the package version in `kcl.mod`
OciOpts *opt.OciOptions
Source downloader.Source
ModPath string
VendorMode bool
}

type PushOption func(*PushOptions) error

// WithPushVendorMode sets the vendor mode for the Push method.
func WithPushVendorMode(vendorMode bool) PushOption {
// WithPushSource sets the source for the Push method.
func WithPushSource(source downloader.Source) PushOption {
return func(opts *PushOptions) error {
opts.VendorMode = vendorMode
opts.Source = source
return nil
}
}

// WithPushOciOptions sets the oci options for the Push method.
func WithPushOciOptions(ociOpts *opt.OciOptions) PushOption {
// WithPushVendorMode sets the vendor mode for the Push method.
func WithPushVendorMode(vendorMode bool) PushOption {
return func(opts *PushOptions) error {
if ociOpts == nil {
return fmt.Errorf("ociOpts cannot be nil")
}
opts.OciOpts = ociOpts
opts.VendorMode = vendorMode
return nil
}
}
Expand All @@ -57,30 +55,17 @@ func WithPushModPath(modPath string) PushOption {
}

// fillDefaultPushOptions will fill the default values for the PushOptions.
func (c *KpmClient) fillDefaultPushOptions(opts *PushOptions, kMod *pkg.KclPkg) {
if opts.OciOpts == nil {
opts.OciOpts = &opt.OciOptions{
Reg: c.GetSettings().DefaultOciRegistry(),
Repo: c.GetSettings().DefaultOciRepo(),
Ref: kMod.ModFile.Pkg.Name,
Tag: kMod.ModFile.Pkg.Version,
}
} else {
if opts.OciOpts.Reg == "" {
opts.OciOpts.Reg = c.GetSettings().DefaultOciRegistry()
}

if opts.OciOpts.Repo == "" {
opts.OciOpts.Repo = c.GetSettings().DefaultOciRepo()
}
func (c *KpmClient) fillDefaultPushOptions(ociOpt *opt.OciOptions, kMod *pkg.KclPkg) {
if ociOpt.Reg == "" {
ociOpt.Reg = c.GetSettings().DefaultOciRegistry()
}

if opts.OciOpts.Ref == "" {
opts.OciOpts.Ref = kMod.ModFile.Pkg.Name
}
if ociOpt.Repo == "" {
ociOpt.Repo = c.GetSettings().DefaultOciRepo()
}

if opts.OciOpts.Tag == "" {
opts.OciOpts.Tag = kMod.ModFile.Pkg.Version
}
if ociOpt.Tag == "" {
ociOpt.Tag = kMod.ModFile.Pkg.Version
}
}

Expand All @@ -102,8 +87,28 @@ func (c *KpmClient) Push(opts ...PushOption) error {
return err
}

c.fillDefaultPushOptions(pushOpts, kMod)
ociOpts := pushOpts.OciOpts
source := pushOpts.Source
ociUrl, err := source.ToString()
if err != nil {
return err
}
if source.Oci == nil {
return fmt.Errorf("'%s' is not an oci source, only support oci source", ociUrl)
}

var tag string
if source.Oci.Tag == "" {
tag = kMod.ModFile.Pkg.Version
} else {
tag = source.Oci.Tag
}

ociOpts, err := c.ParseOciOptionFromString(ociUrl, tag)
if err != nil {
return err
}
c.fillDefaultPushOptions(ociOpts, kMod)

ociOpts.Annotations, err = kMod.GenOciManifestFromPkg()
if err != nil {
return err
Expand All @@ -130,8 +135,7 @@ func (c *KpmClient) Push(opts ...PushOption) error {

// PushToOci will push a kcl package to oci registry.
func (c *KpmClient) pushToOci(localPath string, ociOpts *opt.OciOptions) error {
repoPath := utils.JoinPath(ociOpts.Reg, ociOpts.Repo)
repoPath = utils.JoinPath(repoPath, ociOpts.Ref)
repoPath := utils.JoinPath(ociOpts.Reg, ociOpts.Repo, ociOpts.Ref)
cred, err := c.GetCredentials(ociOpts.Reg)
if err != nil {
return err
Expand Down
13 changes: 8 additions & 5 deletions pkg/client/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/stretchr/testify/assert"
"kcl-lang.io/kpm/pkg/downloader"
"kcl-lang.io/kpm/pkg/mock"
"kcl-lang.io/kpm/pkg/opt"
pkg "kcl-lang.io/kpm/pkg/package"
"kcl-lang.io/kpm/pkg/reporter"
)
Expand Down Expand Up @@ -41,10 +40,14 @@ func TestPush(t *testing.T) {

err = kpmcli.Push(
WithPushModPath(pushedModPath),
WithPushOciOptions(&opt.OciOptions{
Reg: "localhost:5001",
Repo: "test",
}),
WithPushSource(
downloader.Source{
Oci: &downloader.Oci{
Reg: "localhost:5001",
Repo: "test/push_0",
},
},
),
)

if err != (*reporter.KpmEvent)(nil) {
Expand Down
17 changes: 10 additions & 7 deletions pkg/cmd/cmd_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/urfave/cli/v2"
"kcl-lang.io/kpm/pkg/client"
"kcl-lang.io/kpm/pkg/downloader"
"kcl-lang.io/kpm/pkg/errors"
"kcl-lang.io/kpm/pkg/oci"
"kcl-lang.io/kpm/pkg/opt"
Expand Down Expand Up @@ -165,16 +166,18 @@ func pushPackage(ociUrl string, kclPkg *pkg.KclPkg, vendorMode bool, kpmcli *cli
)
}

ociOpts.Annotations, err = kclPkg.GenOciManifestFromPkg()
if err != nil {
return err
}

reporter.ReportMsgTo(fmt.Sprintf("package '%s' will be pushed", kclPkg.GetPkgName()), kpmcli.GetLogWriter())
// Push it.
err = kpmcli.Push(
client.WithPushModPath(kclPkg.HomePath),
client.WithPushOciOptions(ociOpts),
client.WithPushSource(
downloader.Source{
Oci: &downloader.Oci{
Reg: ociOpts.Reg,
Repo: utils.JoinPath(ociOpts.Repo, ociOpts.Ref),
Tag: ociOpts.Tag,
},
},
),
client.WithPushVendorMode(vendorMode),
)
if err != (*reporter.KpmEvent)(nil) {
Expand Down
14 changes: 12 additions & 2 deletions pkg/opt/opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"net/url"
"os"
gpath "path"
"path/filepath"
"strings"

Expand Down Expand Up @@ -467,9 +468,18 @@ func ParseOciUrl(ociUrl string) (*OciOptions, *reporter.KpmEvent) {
return nil, reporter.NewEvent(reporter.UrlSchemeNotOci)
}

repo, ref := gpath.Split(u.Path)
repo = strings.TrimPrefix(strings.TrimSuffix(repo, "/"), "/")

if repo == "" {
repo = strings.TrimPrefix(strings.TrimSuffix(u.Path, "/"), "/")
ref = ""
}

return &OciOptions{
Reg: u.Host,
Repo: u.Path,
Repo: repo,
Ref: ref,
}, nil
}

Expand All @@ -491,7 +501,7 @@ func (oci *OciOptions) AddStoragePathSuffix(pathPrefix string) string {

// SanitizePathSuffix will take 'Registry/Repo/Tag' as a path suffix and sanitize it.
func (oci *OciOptions) SanitizePathWithSuffix(pathPrefix string) string {
return path.SanitizePath(filepath.Join(filepath.Join(filepath.Join(pathPrefix, oci.Reg), oci.Repo), oci.Tag))
return path.SanitizePath(filepath.Join(pathPrefix, oci.Reg, oci.Repo, oci.Ref, oci.Tag))
}

type OciManifestOptions struct {
Expand Down
19 changes: 15 additions & 4 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,10 +552,21 @@ func RmNewline(s string) string {
}

// JoinPath will join the 'elem' to the 'base' with '/'.
func JoinPath(base, elem string) string {
base = strings.TrimSuffix(base, "/")
elem = strings.TrimPrefix(elem, "/")
return base + "/" + elem
func JoinPath(elems ...string) string {
var result string
for i, elem := range elems {
if elem == "" {
continue
}
elem = strings.TrimPrefix(elem, "/")
elem = strings.TrimSuffix(elem, "/")
if i == 0 {
result = elem
} else {
result = result + "/" + elem
}
}
return result
}

// IsUrl will check whether the string 'str' is a url.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
start to pull 'oci://localhost:5001/test/k8s'
the latest version '1.31.2' will be pulled
pulling '/test/k8s:1.31.2' from 'localhost:5001/test/k8s'
pulling 'test/k8s:1.31.2' from 'localhost:5001/test/k8s'
pulled 'oci://localhost:5001/test/k8s' in '<workspace>/localhost:5001/test/k8s/1.31.2' successfully
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
start to pull 'oci://localhost:5001/test/k8s' with tag '1.14'
pulling '/test/k8s:1.14' from 'localhost:5001/test/k8s'
pulling 'test/k8s:1.14' from 'localhost:5001/test/k8s'
pulled 'oci://localhost:5001/test/k8s' in '<workspace>/localhost:5001/test/k8s/1.14' successfully
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
start to pull 'oci://localhost:5001/test/helloworld'
the latest version '0.1.2' will be pulled
pulling '/test/helloworld:0.1.2' from 'localhost:5001/test/helloworld'
pulling 'test/helloworld:0.1.2' from 'localhost:5001/test/helloworld'
pulled 'oci://localhost:5001/test/helloworld' in '<workspace>/localhost:5001/test/helloworld/0.1.2' successfully
Original file line number Diff line number Diff line change
@@ -1 +1 @@
repository 'invalid_url/' not found
repository 'invalid_url' not found
Original file line number Diff line number Diff line change
@@ -1 +1 @@
repository 'invalid_url/' not found
repository 'invalid_url' not found

0 comments on commit 202e2b6

Please sign in to comment.