@@ -18,10 +18,10 @@ import (
1818const (
1919 modAddDesc = `This command adds new dependency
2020`
21- modAddExample = ` # Add the module dependency named "k8s"
21+ modAddExample = ` # Add the module dependency named "k8s" from the default OCI registry
2222 kcl mod add k8s
2323
24- # Add the module dependency named "k8s" with the version "1.28"
24+ # Add the module dependency named "k8s" with the version "1.28" from the default OCI registry
2525 kcl mod add k8s:1.28
2626
2727 # Add the module dependency from the GitHub by git url
@@ -33,10 +33,13 @@ const (
3333 # Add the module dependency from the local file system by file url
3434 kcl mod add /path/to/another_module
3535
36- # Add the module dependency from the GitHub by flag
36+ # Add the module dependency from the GitHub by the tag flag
3737 kcl mod add --git https://github.com/kcl-lang/konfig --tag v0.4.0
3838
39- # Add the module dependency from the OCI Registry by flag
39+ # Add the sub module dependency named "helloworld" from the Git repo by the tag flag
40+ kcl mod add helloworld --git https://github.com/kcl-lang/modules --tag v0.1.0
41+
42+ # Add the module dependency from the OCI registry named "" by the tag flag
4043 kcl mod add --oci https://ghcr.io/kcl-lang/helloworld --tag 0.1.0`
4144)
4245
@@ -58,6 +61,7 @@ func NewModAddCmd(cli *client.KpmClient) *cobra.Command {
5861 cmd .Flags ().StringVar (& tag , "tag" , "" , "git or oci repository tag" )
5962 cmd .Flags ().StringVar (& commit , "commit" , "" , "git repository commit" )
6063 cmd .Flags ().StringVar (& branch , "branch" , "" , "git repository branch" )
64+ cmd .Flags ().StringVar (& path , "path" , "" , "filesystem path to local dependency to add" )
6165 cmd .Flags ().StringVar (& rename , "rename" , "" , "rename the dependency" )
6266 cmd .Flags ().BoolVar (& noSumCheck , "no_sum_check" , false , "do not check the checksum of the package and update kcl.mod.lock" )
6367
@@ -133,73 +137,80 @@ func ModAdd(cli *client.KpmClient, args []string) error {
133137// parseAddOptions will parse the user cli inputs.
134138func parseAddOptions (cli * client.KpmClient , localPath string , args []string ) (* opt.AddOptions , error ) {
135139 // parse the CLI command with the following style
140+ // kcl mod add <package>
141+ // kcl mod add <package>:<version>
142+ // kcl mod add /path/to/xxx
143+ // kcl mod add https://xxx/xxx --tag 0.0.1
144+ // kcl mod add oci://xxx/xxx --tag 0.0.1
145+ //
136146 // kcl mod add --git https://xxx/xxx --tag 0.0.1
147+ // kcl mod add <sub_package> --git https://xxx/xxx --tag 0.0.1
137148 // kcl mod add --oci https://xxx/xxx --tag 0.0.1
149+ // kcl mod add <sub_package> --oci https://xxx/xxx --tag 0.0.1
138150 // kcl mod add --path /path/to/xxx
139- if len (args ) == 0 {
140- if len (git ) != 0 {
141- gitUrl , err := url .Parse (git )
142- if err != nil {
143- return nil , err
144- }
145- gitOpt := opt .NewGitOptionsFromUrl (gitUrl )
146- if gitOpt == nil {
147- return nil , fmt .Errorf ("invalid git url '%s'" , git )
148- }
149-
150- gitOpt .Tag = tag
151- gitOpt .Commit = commit
152- gitOpt .Branch = branch
153-
154- return & opt.AddOptions {
155- LocalPath : localPath ,
156- RegistryOpts : opt.RegistryOptions {Git : gitOpt },
157- NoSumCheck : noSumCheck ,
158- NewPkgName : rename ,
159- }, nil
160- } else if len (oci ) != 0 {
161- ociUrl , err := url .Parse (oci )
162- if err != nil {
163- return nil , err
164- }
165- ociOpt := opt .NewOciOptionsFromUrl (ociUrl )
166- if ociOpt == nil {
167- return nil , fmt .Errorf ("invalid oci url '%s'" , oci )
168- }
169- ociOpt .Tag = tag
170-
171- return & opt.AddOptions {
172- LocalPath : localPath ,
173- RegistryOpts : opt.RegistryOptions {Oci : ociOpt },
174- NoSumCheck : noSumCheck ,
175- NewPkgName : rename ,
176- }, nil
177- } else if len (path ) != 0 {
178- pathUrl , err := url .Parse (path )
179- if err != nil {
180- return nil , err
181- }
182-
183- pathOpt , err := opt .NewLocalOptionsFromUrl (pathUrl )
184- if err != (* reporter .KpmEvent )(nil ) {
185- return nil , err
186- }
187-
188- return & opt.AddOptions {
189- LocalPath : localPath ,
190- RegistryOpts : opt.RegistryOptions {Local : pathOpt },
191- NoSumCheck : noSumCheck ,
192- NewPkgName : rename ,
193- }, nil
151+ // kcl mod add <sub_package> --path /path/to/xxx
152+ if len (git ) != 0 {
153+ gitUrl , err := url .Parse (git )
154+ if err != nil {
155+ return nil , err
156+ }
157+ gitOpts := opt .NewGitOptionsFromUrl (gitUrl )
158+ if gitOpts == nil {
159+ return nil , fmt .Errorf ("invalid git url '%s'" , git )
194160 }
161+ gitOpts .Tag = tag
162+ gitOpts .Commit = commit
163+ gitOpts .Branch = branch
164+ // Git sub package.
165+ if len (args ) > 0 {
166+ gitOpts .Package = args [len (args )- 1 ]
167+ }
168+ return & opt.AddOptions {
169+ LocalPath : localPath ,
170+ RegistryOpts : opt.RegistryOptions {Git : gitOpts },
171+ NoSumCheck : noSumCheck ,
172+ NewPkgName : rename ,
173+ }, nil
174+ } else if len (oci ) != 0 {
175+ ociUrl , err := url .Parse (oci )
176+ if err != nil {
177+ return nil , err
178+ }
179+ ociOpts := opt .NewOciOptionsFromUrl (ociUrl )
180+ if ociOpts == nil {
181+ return nil , fmt .Errorf ("invalid oci url '%s'" , oci )
182+ }
183+ ociOpts .Tag = tag
184+ // OCI sub package
185+ if len (args ) > 0 {
186+ ociOpts .Package = args [len (args )- 1 ]
187+ }
188+ return & opt.AddOptions {
189+ LocalPath : localPath ,
190+ RegistryOpts : opt.RegistryOptions {Oci : ociOpts },
191+ NoSumCheck : noSumCheck ,
192+ NewPkgName : rename ,
193+ }, nil
194+ } else if len (path ) != 0 {
195+ pathUrl , err := url .Parse (path )
196+ if err != nil {
197+ return nil , err
198+ }
199+ pathOpts , err := opt .NewLocalOptionsFromUrl (pathUrl )
200+ if err != (* reporter .KpmEvent )(nil ) {
201+ return nil , err
202+ }
203+ // Local path sub package
204+ if len (args ) > 0 {
205+ pathOpts .Package = args [len (args )- 1 ]
206+ }
207+ return & opt.AddOptions {
208+ LocalPath : localPath ,
209+ RegistryOpts : opt.RegistryOptions {Local : pathOpts },
210+ NoSumCheck : noSumCheck ,
211+ NewPkgName : rename ,
212+ }, nil
195213 } else {
196- // parse the CLI command with the following style
197- // kcl mod add k8s
198- // kcl mod add k8s:0.0.1
199- // kcl mod add /path/to/xxx
200- // kcl mod add https://xxx/xxx --tag 0.0.1
201- // kcl mod add oci://xxx/xxx --tag 0.0.1
202-
203214 localPkg , err := parseLocalPathOptions (args )
204215 pkgSource := argsGet (args , 0 )
205216 if err != (* reporter .KpmEvent )(nil ) {
@@ -232,8 +243,6 @@ func parseAddOptions(cli *client.KpmClient, localPath string, args []string) (*o
232243 }, nil
233244 }
234245 }
235-
236- return nil , fmt .Errorf ("invalid add options" )
237246}
238247
239248// parseLocalPathOptions will parse the local path information from user cli inputs.
0 commit comments