From 5e4e2bdcfde97d4ec49a8618b30ac07f90a35dfd Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Thu, 16 May 2024 23:30:09 +0530 Subject: [PATCH] feat: added rename flag in kpm add Signed-off-by: Asish Kumar --- pkg/client/client.go | 4 ++++ pkg/cmd/cmd_add.go | 8 ++++++++ pkg/opt/opt.go | 1 + pkg/package/modfile.go | 4 ++++ 4 files changed, 17 insertions(+) diff --git a/pkg/client/client.go b/pkg/client/client.go index 0732784e..f2f0bbd4 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -623,6 +623,7 @@ func (c *KpmClient) AddDepWithOpts(kclPkg *pkg.KclPkg, opt *opt.AddOptions) (*pk fmt.Sprintf("adding dependency '%s'", d.Name), c.logWriter, ) + // 2. download the dependency to the local path. err = c.AddDepToPkg(kclPkg, d) if err != nil { @@ -630,6 +631,9 @@ func (c *KpmClient) AddDepWithOpts(kclPkg *pkg.KclPkg, opt *opt.AddOptions) (*pk } // 3. update the kcl.mod and kcl.mod.lock. + if opt.NewPkgName != "" { + d.ChangePkgName(opt.NewPkgName) + } err = kclPkg.UpdateModAndLockFile() if err != nil { return nil, err diff --git a/pkg/cmd/cmd_add.go b/pkg/cmd/cmd_add.go index ec311e7d..36822a6b 100644 --- a/pkg/cmd/cmd_add.go +++ b/pkg/cmd/cmd_add.go @@ -42,6 +42,10 @@ func NewAddCmd(kpmcli *client.KpmClient) *cli.Command { Name: FLAG_NO_SUM_CHECK, Usage: "do not check the checksum of the package and update kcl.mod.lock", }, + &cli.StringFlag{ + Name: "rename", + Usage: "rename the package name in kcl.mod.lock", + }, }, Action: func(c *cli.Context) error { @@ -131,6 +135,7 @@ func onlyOnceOption(c *cli.Context, name string) (string, *reporter.KpmEvent) { // parseAddOptions will parse the user cli inputs. func parseAddOptions(c *cli.Context, kpmcli *client.KpmClient, localPath string) (*opt.AddOptions, error) { noSumCheck := c.Bool(FLAG_NO_SUM_CHECK) + newPkgName := c.String("rename") // parse from 'kpm add -git https://xxx/xxx.git -tag v0.0.1'. if c.NArg() == 0 { gitOpts, err := parseGitRegistryOptions(c) @@ -142,6 +147,7 @@ func parseAddOptions(c *cli.Context, kpmcli *client.KpmClient, localPath string) } return &opt.AddOptions{ LocalPath: localPath, + NewPkgName: newPkgName, RegistryOpts: *gitOpts, NoSumCheck: noSumCheck, }, nil @@ -155,12 +161,14 @@ func parseAddOptions(c *cli.Context, kpmcli *client.KpmClient, localPath string) } return &opt.AddOptions{ LocalPath: localPath, + NewPkgName: newPkgName, RegistryOpts: *ociReg, NoSumCheck: noSumCheck, }, nil } else { return &opt.AddOptions{ LocalPath: localPath, + NewPkgName: newPkgName, RegistryOpts: *localPkg, NoSumCheck: noSumCheck, }, nil diff --git a/pkg/opt/opt.go b/pkg/opt/opt.go index a4b928cd..8f20b3df 100644 --- a/pkg/opt/opt.go +++ b/pkg/opt/opt.go @@ -166,6 +166,7 @@ func (opts *InitOptions) Validate() error { type AddOptions struct { LocalPath string + NewPkgName string RegistryOpts RegistryOptions NoSumCheck bool } diff --git a/pkg/package/modfile.go b/pkg/package/modfile.go index 38946a04..9f5b1c23 100644 --- a/pkg/package/modfile.go +++ b/pkg/package/modfile.go @@ -266,6 +266,10 @@ func (dep *Dependency) GetDownloadPath() string { return "" } +func (dep *Dependency) ChangePkgName(NewPkgName string) { + dep.Name = NewPkgName +} + func GenSource(sourceType string, uri string, tagName string) (Source, error) { source := Source{} if sourceType == GIT {