From 9bf8bbc818e9985b348121647ce70a9072bceef7 Mon Sep 17 00:00:00 2001 From: zongz Date: Wed, 6 Nov 2024 18:59:04 +0800 Subject: [PATCH 1/7] feat: supports rename when add dependency Signed-off-by: zongz --- pkg/client/add.go | 28 +++---- pkg/client/add_test.go | 80 +++++++++++++++++++ .../add_with_mod_spec/rename/kcl.mod.bk | 4 + .../add_with_mod_spec/rename/kcl.mod.expect | 7 ++ .../add_with_mod_spec/rename/kcl.mod.lock.bk | 0 .../rename/kcl.mod.lock.expect | 8 ++ .../test_data/add_with_mod_spec/rename/main.k | 1 + pkg/downloader/toml.go | 26 ++++-- pkg/package/test_data/test_rename_pkg/kcl.mod | 7 ++ pkg/package/toml.go | 8 +- pkg/package/toml_test.go | 17 ++++ 11 files changed, 161 insertions(+), 25 deletions(-) create mode 100644 pkg/client/test_data/add_with_mod_spec/rename/kcl.mod.bk create mode 100644 pkg/client/test_data/add_with_mod_spec/rename/kcl.mod.expect create mode 100644 pkg/client/test_data/add_with_mod_spec/rename/kcl.mod.lock.bk create mode 100644 pkg/client/test_data/add_with_mod_spec/rename/kcl.mod.lock.expect create mode 100644 pkg/client/test_data/add_with_mod_spec/rename/main.k create mode 100644 pkg/package/test_data/test_rename_pkg/kcl.mod diff --git a/pkg/client/add.go b/pkg/client/add.go index 05ad039e..9bb88f7c 100644 --- a/pkg/client/add.go +++ b/pkg/client/add.go @@ -13,25 +13,16 @@ import ( type AddOptions struct { // Source is the source of the package to be pulled. // Including git, oci, local. - Source *downloader.Source - KclPkg *pkg.KclPkg + Source *downloader.Source + KclPkg *pkg.KclPkg + NewPkgName string } type AddOption func(*AddOptions) error -func WithAddModSpec(modSpec *downloader.ModSpec) AddOption { +func WithNewPkgName(newPkgName string) AddOption { return func(opts *AddOptions) error { - if modSpec == nil { - return fmt.Errorf("modSpec cannot be nil") - } - if opts.Source == nil { - opts.Source = &downloader.Source{ - ModSpec: modSpec, - } - } else { - opts.Source.ModSpec = modSpec - } - + opts.NewPkgName = newPkgName return nil } } @@ -146,8 +137,15 @@ func (c *KpmClient) Add(options ...AddOption) error { depSource.ModSpec = modSpec } + var depName string + if opts.NewPkgName != "" { + depName = opts.NewPkgName + } else { + depName = depPkg.ModFile.Pkg.Name + } + dep := pkg.Dependency{ - Name: depPkg.ModFile.Pkg.Name, + Name: depName, FullName: depPkg.GetPkgFullName(), Version: depPkg.ModFile.Pkg.Version, LocalFullPath: depPkg.HomePath, diff --git a/pkg/client/add_test.go b/pkg/client/add_test.go index b5f5b00b..ebaa609f 100644 --- a/pkg/client/add_test.go +++ b/pkg/client/add_test.go @@ -135,3 +135,83 @@ func testAddWithModSpec(t *testing.T) { }) } } + +func TestAddRenameWithModSpec(t *testing.T) { + testDir := getTestDir("add_with_mod_spec") + pkgPath := filepath.Join(testDir, "rename") + + modbkPath := filepath.Join(pkgPath, "kcl.mod.bk") + modPath := filepath.Join(pkgPath, "kcl.mod") + modExpect := filepath.Join(pkgPath, "kcl.mod.expect") + lockbkPath := filepath.Join(pkgPath, "kcl.mod.lock.bk") + lockPath := filepath.Join(pkgPath, "kcl.mod.lock") + lockExpect := filepath.Join(pkgPath, "kcl.mod.lock.expect") + + err := copy.Copy(modbkPath, modPath) + if err != nil { + t.Fatal(err) + } + + err = copy.Copy(lockbkPath, lockPath) + if err != nil { + t.Fatal(err) + } + + defer func() { + // remove the copied files + err := os.RemoveAll(modPath) + if err != nil { + t.Fatal(err) + } + err = os.RemoveAll(lockPath) + if err != nil { + t.Fatal(err) + } + }() + + kpmcli, err := NewKpmClient() + if err != nil { + t.Fatal(err) + } + + kpkg, err := pkg.LoadKclPkgWithOpts( + pkg.WithPath(pkgPath), + pkg.WithSettings(kpmcli.GetSettings()), + ) + + if err != nil { + t.Fatal(err) + } + + err = kpmcli.Add( + WithAddKclPkg(kpkg), + WithAddSourceUrl("oci://ghcr.io/kcl-lang/helloworld?tag=0.1.4&mod=subhelloworld:0.0.1"), + WithNewPkgName("newpkg"), + ) + + if err != nil { + t.Fatal(err) + } + + expectedMod, err := os.ReadFile(modExpect) + if err != nil { + t.Fatal(err) + } + gotMod, err := os.ReadFile(modPath) + if err != nil { + t.Fatal(err) + } + + expectedLock, err := os.ReadFile(lockExpect) + if err != nil { + t.Fatal(err) + } + + gotLock, err := os.ReadFile(lockPath) + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, utils.RmNewline(string(expectedMod)), utils.RmNewline(string(gotMod))) + assert.Equal(t, utils.RmNewline(string(expectedLock)), utils.RmNewline(string(gotLock))) +} diff --git a/pkg/client/test_data/add_with_mod_spec/rename/kcl.mod.bk b/pkg/client/test_data/add_with_mod_spec/rename/kcl.mod.bk new file mode 100644 index 00000000..abbf2dda --- /dev/null +++ b/pkg/client/test_data/add_with_mod_spec/rename/kcl.mod.bk @@ -0,0 +1,4 @@ +[package] +name = "rename" +edition = "v0.10.0" +version = "0.0.1" diff --git a/pkg/client/test_data/add_with_mod_spec/rename/kcl.mod.expect b/pkg/client/test_data/add_with_mod_spec/rename/kcl.mod.expect new file mode 100644 index 00000000..32c28baa --- /dev/null +++ b/pkg/client/test_data/add_with_mod_spec/rename/kcl.mod.expect @@ -0,0 +1,7 @@ +[package] +name = "rename" +edition = "v0.10.0" +version = "0.0.1" + +[dependencies] +newpkg = { oci = "oci://ghcr.io/kcl-lang/helloworld", tag = "0.1.4", package = "subhelloworld", version = "0.0.1" } diff --git a/pkg/client/test_data/add_with_mod_spec/rename/kcl.mod.lock.bk b/pkg/client/test_data/add_with_mod_spec/rename/kcl.mod.lock.bk new file mode 100644 index 00000000..e69de29b diff --git a/pkg/client/test_data/add_with_mod_spec/rename/kcl.mod.lock.expect b/pkg/client/test_data/add_with_mod_spec/rename/kcl.mod.lock.expect new file mode 100644 index 00000000..8be56776 --- /dev/null +++ b/pkg/client/test_data/add_with_mod_spec/rename/kcl.mod.lock.expect @@ -0,0 +1,8 @@ +[dependencies] + [dependencies.newpkg] + name = "newpkg" + full_name = "subhelloworld_0.0.1" + version = "0.0.1" + reg = "ghcr.io" + repo = "kcl-lang/helloworld" + oci_tag = "0.1.4" diff --git a/pkg/client/test_data/add_with_mod_spec/rename/main.k b/pkg/client/test_data/add_with_mod_spec/rename/main.k new file mode 100644 index 00000000..fa7048e6 --- /dev/null +++ b/pkg/client/test_data/add_with_mod_spec/rename/main.k @@ -0,0 +1 @@ +The_first_kcl_program = 'Hello World!' \ No newline at end of file diff --git a/pkg/downloader/toml.go b/pkg/downloader/toml.go index c2201382..38cf25dd 100644 --- a/pkg/downloader/toml.go +++ b/pkg/downloader/toml.go @@ -23,21 +23,26 @@ func (ps *ModSpec) MarshalTOML() string { return sb.String() } -func (source *Source) MarshalTOML() string { +func (source *Source) MarshalTOML(rename bool) string { var sb strings.Builder if source.SpecOnly() { return source.ModSpec.MarshalTOML() } else { - var pkgVersion string + var pkgSpec string var tomlStr string + if source.ModSpec != nil && len(source.ModSpec.Version) > 0 { - pkgVersion = fmt.Sprintf(", version = %q", source.ModSpec.Version) + if rename { + pkgSpec = fmt.Sprintf(", package = %q, version = %q", source.ModSpec.Name, source.ModSpec.Version) + } else { + pkgSpec = fmt.Sprintf(", version = %q", source.ModSpec.Version) + } } if source.Git != nil { tomlStr = source.Git.MarshalTOML() if len(tomlStr) != 0 { - tomlStr = fmt.Sprintf(SOURCE_PATTERN, tomlStr+pkgVersion) + tomlStr = fmt.Sprintf(SOURCE_PATTERN, tomlStr+pkgSpec) } } @@ -45,7 +50,7 @@ func (source *Source) MarshalTOML() string { tomlStr = source.Oci.MarshalTOML() if len(tomlStr) != 0 { if len(source.Oci.Reg) != 0 && len(source.Oci.Repo) != 0 { - tomlStr = fmt.Sprintf(SOURCE_PATTERN, tomlStr+pkgVersion) + tomlStr = fmt.Sprintf(SOURCE_PATTERN, tomlStr+pkgSpec) } } } @@ -53,11 +58,11 @@ func (source *Source) MarshalTOML() string { if source.Local != nil { tomlStr = source.Local.MarshalTOML() if len(tomlStr) != 0 { - tomlStr = fmt.Sprintf(SOURCE_PATTERN, tomlStr+pkgVersion) + tomlStr = fmt.Sprintf(SOURCE_PATTERN, tomlStr+pkgSpec) } } - if source.ModSpec != nil && len(source.ModSpec.Name) != 0 { + if source.ModSpec != nil && len(source.ModSpec.Name) != 0 && !rename { sb.WriteString(fmt.Sprintf(DEP_PATTERN, source.ModSpec.Name, tomlStr)) } else { sb.WriteString(tomlStr) @@ -160,14 +165,19 @@ func (source *Source) UnmarshalModTOML(data interface{}) error { source.Oci = &oci } + pSpec := ModSpec{} if v, ok := meta["version"].(string); ok { - pSpec := ModSpec{} err := pSpec.UnmarshalModTOML(v) if err != nil { return err } source.ModSpec = &pSpec } + + if v, ok := meta["package"].(string); ok { + pSpec.Name = v + source.ModSpec = &pSpec + } } _, ok = data.(string) diff --git a/pkg/package/test_data/test_rename_pkg/kcl.mod b/pkg/package/test_data/test_rename_pkg/kcl.mod new file mode 100644 index 00000000..32c28baa --- /dev/null +++ b/pkg/package/test_data/test_rename_pkg/kcl.mod @@ -0,0 +1,7 @@ +[package] +name = "rename" +edition = "v0.10.0" +version = "0.0.1" + +[dependencies] +newpkg = { oci = "oci://ghcr.io/kcl-lang/helloworld", tag = "0.1.4", package = "subhelloworld", version = "0.0.1" } diff --git a/pkg/package/toml.go b/pkg/package/toml.go index f58bcf89..57a713b7 100644 --- a/pkg/package/toml.go +++ b/pkg/package/toml.go @@ -90,9 +90,13 @@ func (dep *Dependency) MarshalTOML() string { var sb strings.Builder if dep.Source.ModSpec != nil && dep.Source.ModSpec.Version != "" && dep.Source.ModSpec.Name != "" { - sb.WriteString(dep.Source.MarshalTOML()) + if dep.Source.ModSpec.Name == dep.Name { + sb.WriteString(dep.Source.MarshalTOML(false)) + } else { + sb.WriteString(fmt.Sprintf(DEP_PATTERN, dep.Name, dep.Source.MarshalTOML(true))) + } } else { - sb.WriteString(fmt.Sprintf(DEP_PATTERN, dep.Name, dep.Source.MarshalTOML())) + sb.WriteString(fmt.Sprintf(DEP_PATTERN, dep.Name, dep.Source.MarshalTOML(false))) } return sb.String() diff --git a/pkg/package/toml_test.go b/pkg/package/toml_test.go index 545b4300..27110583 100644 --- a/pkg/package/toml_test.go +++ b/pkg/package/toml_test.go @@ -361,3 +361,20 @@ func TestInitEmptyPkg(t *testing.T) { fmt.Printf("modfile: '%q'\n", got_data) assert.Equal(t, expected_toml, got_data) } + +func TestUnMarshalRename(t *testing.T) { + modfile := ModFile{} + modfile.LoadModFile(filepath.Join(getTestDir("test_rename_pkg"), "kcl.mod")) + assert.Equal(t, modfile.Pkg.Name, "rename") + assert.Equal(t, modfile.Pkg.Version, "0.0.1") + assert.Equal(t, modfile.Pkg.Edition, "v0.10.0") + assert.Equal(t, modfile.Dependencies.Deps.Len(), 1) + assert.Equal(t, modfile.Dependencies.Deps.GetOrDefault("newpkg", TestPkgDependency).Name, "newpkg") + assert.Equal(t, modfile.Dependencies.Deps.GetOrDefault("newpkg", TestPkgDependency).FullName, "newpkg_0.0.1") + assert.Equal(t, modfile.Dependencies.Deps.GetOrDefault("newpkg", TestPkgDependency).Version, "0.0.1") + assert.Equal(t, modfile.Dependencies.Deps.GetOrDefault("newpkg", TestPkgDependency).Source.ModSpec.Name, "subhelloworld") + assert.Equal(t, modfile.Dependencies.Deps.GetOrDefault("newpkg", TestPkgDependency).Source.ModSpec.Version, "0.0.1") + assert.Equal(t, modfile.Dependencies.Deps.GetOrDefault("newpkg", TestPkgDependency).Oci.Reg, "ghcr.io") + assert.Equal(t, modfile.Dependencies.Deps.GetOrDefault("newpkg", TestPkgDependency).Oci.Repo, "kcl-lang/helloworld") + assert.Equal(t, modfile.Dependencies.Deps.GetOrDefault("newpkg", TestPkgDependency).Oci.Tag, "0.1.4") +} From fc4496d3175774f2e5f3ad2c663e86c2a644c6a9 Mon Sep 17 00:00:00 2001 From: zongz Date: Thu, 7 Nov 2024 14:29:43 +0800 Subject: [PATCH 2/7] refactor: add 'alias' to struct 'Source' Signed-off-by: zongz --- pkg/client/add.go | 19 ++++++++++++------- pkg/client/add_test.go | 2 +- pkg/downloader/source.go | 1 + pkg/downloader/toml.go | 16 ++++++++-------- pkg/package/toml.go | 17 ++++++++++------- pkg/package/toml_test.go | 1 + 6 files changed, 33 insertions(+), 23 deletions(-) diff --git a/pkg/client/add.go b/pkg/client/add.go index 9bb88f7c..1665dbe2 100644 --- a/pkg/client/add.go +++ b/pkg/client/add.go @@ -13,16 +13,21 @@ import ( type AddOptions struct { // Source is the source of the package to be pulled. // Including git, oci, local. - Source *downloader.Source - KclPkg *pkg.KclPkg - NewPkgName string + Source *downloader.Source + KclPkg *pkg.KclPkg } type AddOption func(*AddOptions) error -func WithNewPkgName(newPkgName string) AddOption { +func WithAddPkgNameAlias(nameAlias string) AddOption { return func(opts *AddOptions) error { - opts.NewPkgName = newPkgName + if opts.Source == nil { + return fmt.Errorf("source cannot be nil") + } + if opts.Source.ModSpec.IsNil() { + return fmt.Errorf("modSpec cannot be nil") + } + opts.Source.ModSpec.Alias = nameAlias return nil } } @@ -138,8 +143,8 @@ func (c *KpmClient) Add(options ...AddOption) error { } var depName string - if opts.NewPkgName != "" { - depName = opts.NewPkgName + if opts.Source.ModSpec.Alias != "" { + depName = opts.Source.ModSpec.Alias } else { depName = depPkg.ModFile.Pkg.Name } diff --git a/pkg/client/add_test.go b/pkg/client/add_test.go index ebaa609f..8b7090d1 100644 --- a/pkg/client/add_test.go +++ b/pkg/client/add_test.go @@ -186,7 +186,7 @@ func TestAddRenameWithModSpec(t *testing.T) { err = kpmcli.Add( WithAddKclPkg(kpkg), WithAddSourceUrl("oci://ghcr.io/kcl-lang/helloworld?tag=0.1.4&mod=subhelloworld:0.0.1"), - WithNewPkgName("newpkg"), + WithAddPkgNameAlias("newpkg"), ) if err != nil { diff --git a/pkg/downloader/source.go b/pkg/downloader/source.go index d32abb35..d3a33fed 100644 --- a/pkg/downloader/source.go +++ b/pkg/downloader/source.go @@ -20,6 +20,7 @@ import ( type ModSpec struct { Name string Version string + Alias string } // IsNil returns true if the ModSpec is nil. diff --git a/pkg/downloader/toml.go b/pkg/downloader/toml.go index 38cf25dd..abe37773 100644 --- a/pkg/downloader/toml.go +++ b/pkg/downloader/toml.go @@ -16,14 +16,18 @@ const SPEC_PATTERN = "%s = %q" func (ps *ModSpec) MarshalTOML() string { var sb strings.Builder if ps != nil && len(ps.Version) != 0 && len(ps.Name) != 0 { - sb.WriteString(fmt.Sprintf(SPEC_PATTERN, ps.Name, ps.Version)) + if len(ps.Alias) == 0 { + sb.WriteString(ps.Version) + } else { + sb.WriteString(fmt.Sprintf(SOURCE_PATTERN, fmt.Sprintf("package = %q, version = %q", ps.Name, ps.Version))) + } return sb.String() } return sb.String() } -func (source *Source) MarshalTOML(rename bool) string { +func (source *Source) MarshalTOML() string { var sb strings.Builder if source.SpecOnly() { return source.ModSpec.MarshalTOML() @@ -32,7 +36,7 @@ func (source *Source) MarshalTOML(rename bool) string { var tomlStr string if source.ModSpec != nil && len(source.ModSpec.Version) > 0 { - if rename { + if source.ModSpec.Alias != "" { pkgSpec = fmt.Sprintf(", package = %q, version = %q", source.ModSpec.Name, source.ModSpec.Version) } else { pkgSpec = fmt.Sprintf(", version = %q", source.ModSpec.Version) @@ -62,11 +66,7 @@ func (source *Source) MarshalTOML(rename bool) string { } } - if source.ModSpec != nil && len(source.ModSpec.Name) != 0 && !rename { - sb.WriteString(fmt.Sprintf(DEP_PATTERN, source.ModSpec.Name, tomlStr)) - } else { - sb.WriteString(tomlStr) - } + sb.WriteString(tomlStr) } return sb.String() diff --git a/pkg/package/toml.go b/pkg/package/toml.go index 57a713b7..687c9733 100644 --- a/pkg/package/toml.go +++ b/pkg/package/toml.go @@ -89,16 +89,14 @@ const DEP_PATTERN = "%s = %s" func (dep *Dependency) MarshalTOML() string { var sb strings.Builder - if dep.Source.ModSpec != nil && dep.Source.ModSpec.Version != "" && dep.Source.ModSpec.Name != "" { - if dep.Source.ModSpec.Name == dep.Name { - sb.WriteString(dep.Source.MarshalTOML(false)) - } else { - sb.WriteString(fmt.Sprintf(DEP_PATTERN, dep.Name, dep.Source.MarshalTOML(true))) + depName := dep.Name + if !dep.Source.ModSpec.IsNil() { + if dep.Source.ModSpec.Alias != "" { + depName = dep.Source.ModSpec.Alias } - } else { - sb.WriteString(fmt.Sprintf(DEP_PATTERN, dep.Name, dep.Source.MarshalTOML(false))) } + sb.WriteString(fmt.Sprintf(DEP_PATTERN, depName, dep.Source.MarshalTOML())) return sb.String() } @@ -235,6 +233,11 @@ func (deps *Dependencies) UnmarshalModTOML(data interface{}) error { if err != nil { return err } + if !dep.Source.ModSpec.IsNil() { + if dep.Source.ModSpec.Name != dep.Name { + dep.Source.ModSpec.Alias = dep.Name + } + } deps.Deps.Set(k, dep) } diff --git a/pkg/package/toml_test.go b/pkg/package/toml_test.go index 27110583..cfbd9034 100644 --- a/pkg/package/toml_test.go +++ b/pkg/package/toml_test.go @@ -374,6 +374,7 @@ func TestUnMarshalRename(t *testing.T) { assert.Equal(t, modfile.Dependencies.Deps.GetOrDefault("newpkg", TestPkgDependency).Version, "0.0.1") assert.Equal(t, modfile.Dependencies.Deps.GetOrDefault("newpkg", TestPkgDependency).Source.ModSpec.Name, "subhelloworld") assert.Equal(t, modfile.Dependencies.Deps.GetOrDefault("newpkg", TestPkgDependency).Source.ModSpec.Version, "0.0.1") + assert.Equal(t, modfile.Dependencies.Deps.GetOrDefault("newpkg", TestPkgDependency).Source.ModSpec.Alias, "newpkg") assert.Equal(t, modfile.Dependencies.Deps.GetOrDefault("newpkg", TestPkgDependency).Oci.Reg, "ghcr.io") assert.Equal(t, modfile.Dependencies.Deps.GetOrDefault("newpkg", TestPkgDependency).Oci.Repo, "kcl-lang/helloworld") assert.Equal(t, modfile.Dependencies.Deps.GetOrDefault("newpkg", TestPkgDependency).Oci.Tag, "0.1.4") From 7d2c7334832426375fee9a97b3b446f0a004da1a Mon Sep 17 00:00:00 2001 From: zongz Date: Thu, 7 Nov 2024 14:33:25 +0800 Subject: [PATCH 3/7] fix: rm useless code changes Signed-off-by: zongz --- pkg/client/add.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/pkg/client/add.go b/pkg/client/add.go index 1665dbe2..b52a374b 100644 --- a/pkg/client/add.go +++ b/pkg/client/add.go @@ -32,6 +32,23 @@ func WithAddPkgNameAlias(nameAlias string) AddOption { } } +func WithAddModSpec(modSpec *downloader.ModSpec) AddOption { + return func(opts *AddOptions) error { + if modSpec == nil { + return fmt.Errorf("modSpec cannot be nil") + } + if opts.Source == nil { + opts.Source = &downloader.Source{ + ModSpec: modSpec, + } + } else { + opts.Source.ModSpec = modSpec + } + + return nil + } +} + func WithAddSource(source *downloader.Source) AddOption { return func(opts *AddOptions) error { if source == nil { From ccb24b7cc7e84961f8df8ba54b4267401cf158e3 Mon Sep 17 00:00:00 2001 From: zongz Date: Thu, 7 Nov 2024 14:45:53 +0800 Subject: [PATCH 4/7] fix: fix test case Signed-off-by: zongz --- pkg/downloader/toml.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/downloader/toml.go b/pkg/downloader/toml.go index abe37773..e42ec578 100644 --- a/pkg/downloader/toml.go +++ b/pkg/downloader/toml.go @@ -17,7 +17,7 @@ func (ps *ModSpec) MarshalTOML() string { var sb strings.Builder if ps != nil && len(ps.Version) != 0 && len(ps.Name) != 0 { if len(ps.Alias) == 0 { - sb.WriteString(ps.Version) + sb.WriteString(fmt.Sprintf("%q", ps.Version)) } else { sb.WriteString(fmt.Sprintf(SOURCE_PATTERN, fmt.Sprintf("package = %q, version = %q", ps.Name, ps.Version))) } From f58dd717ffff2feab05feae2a6462a242719e1ce Mon Sep 17 00:00:00 2001 From: zongz Date: Thu, 7 Nov 2024 15:08:07 +0800 Subject: [PATCH 5/7] fix: rename WithAddPkgNameAlias to WithAlias Signed-off-by: zongz --- pkg/client/add.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/client/add.go b/pkg/client/add.go index b52a374b..3ba65af9 100644 --- a/pkg/client/add.go +++ b/pkg/client/add.go @@ -19,7 +19,7 @@ type AddOptions struct { type AddOption func(*AddOptions) error -func WithAddPkgNameAlias(nameAlias string) AddOption { +func WithAlias(alias string) AddOption { return func(opts *AddOptions) error { if opts.Source == nil { return fmt.Errorf("source cannot be nil") @@ -27,7 +27,7 @@ func WithAddPkgNameAlias(nameAlias string) AddOption { if opts.Source.ModSpec.IsNil() { return fmt.Errorf("modSpec cannot be nil") } - opts.Source.ModSpec.Alias = nameAlias + opts.Source.ModSpec.Alias = alias return nil } } From 0e0680b5212fc49233b0bb67d8233a63ecb12886 Mon Sep 17 00:00:00 2001 From: zongz Date: Thu, 7 Nov 2024 15:10:08 +0800 Subject: [PATCH 6/7] fix: fix typo Signed-off-by: zongz --- pkg/client/add_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/client/add_test.go b/pkg/client/add_test.go index 8b7090d1..b280f509 100644 --- a/pkg/client/add_test.go +++ b/pkg/client/add_test.go @@ -186,7 +186,7 @@ func TestAddRenameWithModSpec(t *testing.T) { err = kpmcli.Add( WithAddKclPkg(kpkg), WithAddSourceUrl("oci://ghcr.io/kcl-lang/helloworld?tag=0.1.4&mod=subhelloworld:0.0.1"), - WithAddPkgNameAlias("newpkg"), + WithAlias("newpkg"), ) if err != nil { From f2e08757635e86188dc3927ac831b853600249f7 Mon Sep 17 00:00:00 2001 From: zongz Date: Thu, 7 Nov 2024 15:21:38 +0800 Subject: [PATCH 7/7] fix: rm ModSpec in source url Signed-off-by: zongz --- pkg/client/add_test.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pkg/client/add_test.go b/pkg/client/add_test.go index b280f509..a04d9998 100644 --- a/pkg/client/add_test.go +++ b/pkg/client/add_test.go @@ -7,6 +7,7 @@ import ( "github.com/otiai10/copy" "github.com/stretchr/testify/assert" + "kcl-lang.io/kpm/pkg/downloader" pkg "kcl-lang.io/kpm/pkg/package" "kcl-lang.io/kpm/pkg/utils" ) @@ -185,7 +186,11 @@ func TestAddRenameWithModSpec(t *testing.T) { err = kpmcli.Add( WithAddKclPkg(kpkg), - WithAddSourceUrl("oci://ghcr.io/kcl-lang/helloworld?tag=0.1.4&mod=subhelloworld:0.0.1"), + WithAddSourceUrl("oci://ghcr.io/kcl-lang/helloworld?tag=0.1.4"), + WithAddModSpec(&downloader.ModSpec{ + Name: "subhelloworld", + Version: "0.0.1", + }), WithAlias("newpkg"), )