Skip to content

Commit

Permalink
refactor: add 'alias' to struct 'Source'
Browse files Browse the repository at this point in the history
Signed-off-by: zongz <[email protected]>
  • Loading branch information
zong-zhe committed Nov 7, 2024
1 parent 9bf8bbc commit fc4496d
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 23 deletions.
19 changes: 12 additions & 7 deletions pkg/client/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions pkg/downloader/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
type ModSpec struct {
Name string
Version string
Alias string
}

// IsNil returns true if the ModSpec is nil.
Expand Down
16 changes: 8 additions & 8 deletions pkg/downloader/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
17 changes: 10 additions & 7 deletions pkg/package/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down Expand Up @@ -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)
}

Expand Down
1 change: 1 addition & 0 deletions pkg/package/toml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit fc4496d

Please sign in to comment.