Skip to content

Commit

Permalink
Merge pull request kcl-lang#318 from officialasishkumar/kpm-add-rename
Browse files Browse the repository at this point in the history
added rename flag in kpm add
  • Loading branch information
Peefy committed May 17, 2024
2 parents 83ebb23 + 5e4e2bd commit dfb0080
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,13 +623,17 @@ 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 {
return nil, err
}

// 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
Expand Down
8 changes: 8 additions & 0 deletions pkg/cmd/cmd_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions pkg/opt/opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ func (opts *InitOptions) Validate() error {

type AddOptions struct {
LocalPath string
NewPkgName string
RegistryOpts RegistryOptions
NoSumCheck bool
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/package/modfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit dfb0080

Please sign in to comment.