11package cmd
22
33import (
4+ "fmt"
45 "net/url"
56
67 "kcl-lang.io/kpm/pkg/client"
78 "kcl-lang.io/kpm/pkg/constants"
9+ "kcl-lang.io/kpm/pkg/downloader"
810 "kcl-lang.io/kpm/pkg/opt"
11+ "kcl-lang.io/kpm/pkg/utils"
912)
1013
1114func argsGet (a []string , n int ) string {
@@ -15,6 +18,105 @@ func argsGet(a []string, n int) string {
1518 return ""
1619}
1720
21+ func ParseSourceFromArgs (cli * client.KpmClient , args []string ) (* downloader.Source , error ) {
22+ source := downloader.Source {}
23+ modSpec := downloader.ModSpec {}
24+
25+ // Parse the source from the args
26+ // Parse the input like: kcl mod pull k8s:1.28 or kcl mod pull oci://ghcr.io/kcl-lang/helloworld --tag 0.1.0
27+ if len (args ) != 0 {
28+ // Iterate through the args to find the source
29+ for _ , arg := range args {
30+ // if arg is a path and exists, it is a local source
31+ if utils .DirExists (arg ) {
32+ source .Local = & downloader.Local {
33+ Path : arg ,
34+ }
35+ continue
36+ }
37+
38+ // if arg is not path, it is Modspec
39+ if err := modSpec .FromString (arg ); err == nil {
40+ // check if modspec already exists
41+ if source .ModSpec == nil {
42+ source .ModSpec = & modSpec
43+ } else {
44+ return nil , fmt .Errorf ("only one modspec is allowed" )
45+ }
46+ continue
47+ } else {
48+ modSpec = downloader.ModSpec {}
49+ }
50+
51+ // if arg is a url, set the source url
52+ if source .IsNilSource () {
53+ err := source .FromString (arg )
54+ if err != nil {
55+ return nil , err
56+ }
57+ continue
58+ }
59+
60+ if ! source .IsNilSource () {
61+ return nil , fmt .Errorf ("only one source is allowed" )
62+ }
63+ }
64+ // For the source parsed from the args, set the tag, commit, branch
65+ if source .Git != nil {
66+ source .Git .Tag = tag
67+ source .Git .Commit = commit
68+ source .Git .Branch = branch
69+ } else if source .Oci != nil {
70+ source .Oci .Tag = tag
71+ }
72+ }
73+
74+ // Parse the source from the flags
75+ // Parse the input like: kcl mod pull --oci oci://ghcr.io/kcl-lang/helloworld --tag 0.1.0
76+ if source .IsNilSource () || source .SpecOnly () {
77+ if len (git ) != 0 {
78+ source .Git = & downloader.Git {
79+ Url : git ,
80+ Tag : tag ,
81+ Commit : commit ,
82+ Branch : branch ,
83+ }
84+ } else if len (oci ) != 0 {
85+ ociUrl , err := url .Parse (oci )
86+ if err != nil {
87+ return nil , err
88+ }
89+
90+ ociUrl .Scheme = constants .OciScheme
91+ query := ociUrl .Query ()
92+ query .Add (constants .Tag , tag )
93+ ociUrl .RawQuery = query .Encode ()
94+ err = source .FromString (ociUrl .String ())
95+ if err != nil {
96+ return nil , err
97+ }
98+ } else if len (path ) != 0 {
99+ source .Local = & downloader.Local {
100+ Path : path ,
101+ }
102+ }
103+ } else if len (git ) != 0 || len (oci ) != 0 || len (path ) != 0 {
104+ return nil , fmt .Errorf ("only one source is allowed" )
105+ }
106+
107+ source .ModSpec = & modSpec
108+ // Set the default oci registry and repo if the source is spec only
109+ if source .SpecOnly () {
110+ source .Oci = & downloader.Oci {
111+ Reg : cli .GetSettings ().DefaultOciRegistry (),
112+ Repo : utils .JoinPath (cli .GetSettings ().DefaultOciRepo (), source .ModSpec .Name ),
113+ Tag : source .ModSpec .Version ,
114+ }
115+ }
116+
117+ return & source , nil
118+ }
119+
18120func ParseUrlFromArgs (cli * client.KpmClient , args []string ) (* url.URL , error ) {
19121 var sourceUrl url.URL
20122
0 commit comments