Skip to content

Commit a178462

Browse files
committed
feat: unify pull cli
Signed-off-by: zongz <[email protected]>
1 parent a74fb44 commit a178462

File tree

18 files changed

+175
-19
lines changed

18 files changed

+175
-19
lines changed

cmd/kcl/commands/args.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,104 @@
11
package cmd
22

3+
import (
4+
"net/url"
5+
6+
"kcl-lang.io/kpm/pkg/client"
7+
"kcl-lang.io/kpm/pkg/constants"
8+
"kcl-lang.io/kpm/pkg/opt"
9+
)
10+
311
func argsGet(a []string, n int) string {
412
if len(a) > n {
513
return a[n]
614
}
715
return ""
816
}
17+
18+
func ParseUrlFromArgs(cli *client.KpmClient, args []string) (*url.URL, error) {
19+
var sourceUrl url.URL
20+
21+
if len(args) == 0 {
22+
if len(git) != 0 {
23+
gitUrl, err := url.Parse(git)
24+
if err != nil {
25+
return nil, err
26+
}
27+
28+
gitUrl.Scheme = constants.GitScheme
29+
query := gitUrl.Query()
30+
if tag != "" {
31+
query.Add(constants.Tag, tag)
32+
}
33+
if commit != "" {
34+
query.Add(constants.GitCommit, commit)
35+
}
36+
if branch != "" {
37+
query.Add(constants.GitBranch, branch)
38+
}
39+
gitUrl.RawQuery = query.Encode()
40+
sourceUrl = *gitUrl
41+
} else if len(oci) != 0 {
42+
ociUrl, err := url.Parse(oci)
43+
if err != nil {
44+
return nil, err
45+
}
46+
47+
ociUrl.Scheme = constants.OciScheme
48+
query := ociUrl.Query()
49+
query.Add(constants.Tag, tag)
50+
ociUrl.RawQuery = query.Encode()
51+
sourceUrl = *ociUrl
52+
}
53+
} else {
54+
url, err := url.Parse(args[0])
55+
if err != nil {
56+
return nil, err
57+
}
58+
query := url.Query()
59+
url.Opaque = ""
60+
regOpts, err := opt.NewRegistryOptionsFrom(args[0], cli.GetSettings())
61+
if err != nil {
62+
return nil, err
63+
}
64+
65+
if regOpts.Git != nil {
66+
if url.Scheme != constants.GitScheme && url.Scheme != constants.SshScheme {
67+
url.Scheme = constants.GitScheme
68+
}
69+
if tag != "" {
70+
query.Add(constants.Tag, tag)
71+
}
72+
if commit != "" {
73+
query.Add(constants.GitCommit, commit)
74+
}
75+
if branch != "" {
76+
query.Add(constants.GitBranch, branch)
77+
}
78+
} else if regOpts.Oci != nil {
79+
url.Scheme = constants.OciScheme
80+
url.Host = regOpts.Oci.Reg
81+
url.Path = regOpts.Oci.Repo
82+
if regOpts.Oci.Tag != "" {
83+
query.Add(constants.Tag, regOpts.Oci.Tag)
84+
}
85+
if tag != "" {
86+
query.Add(constants.Tag, tag)
87+
}
88+
} else if regOpts.Registry != nil {
89+
url.Scheme = constants.DefaultOciScheme
90+
url.Host = regOpts.Registry.Reg
91+
url.Path = regOpts.Registry.Repo
92+
if regOpts.Registry.Tag != "" {
93+
query.Add(constants.Tag, regOpts.Registry.Tag)
94+
}
95+
if tag != "" {
96+
query.Add(constants.Tag, tag)
97+
}
98+
}
99+
100+
url.RawQuery = query.Encode()
101+
sourceUrl = *url
102+
}
103+
return &sourceUrl, nil
104+
}

cmd/kcl/commands/mod_pull.go

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,29 @@ package cmd
33
import (
44
"github.com/spf13/cobra"
55
"kcl-lang.io/kpm/pkg/client"
6+
"kcl-lang.io/kpm/pkg/downloader"
67
)
78

89
const (
910
modPullDesc = `This command pulls kcl modules from the registry.
1011
`
1112
modPullExample = ` # Pull the the module named "k8s" to the local path from the registry
12-
kcl mod pull k8s`
13+
kcl mod pull k8s
14+
15+
# Pull the module dependency named "k8s" with the version "1.28"
16+
kcl mod add k8s:1.28
17+
18+
# Pull the module from the GitHub by git url
19+
kcl mod pull git://github.com/kcl-lang/konfig --tag v0.4.0
20+
21+
# Pull the module from the OCI Registry by oci url
22+
kcl mod pull oci://ghcr.io/kcl-lang/helloworld --tag v0.1.0
23+
24+
# Pull the module from the GitHub by flag
25+
kcl mod pull --git https://github.com/kcl-lang/konfig --tag v0.4.0
26+
27+
# Pull the module from the OCI Registry by flag
28+
kcl mod pull --oci https://ghcr.io/kcl-lang/helloworld --tag 0.1.0`
1329
)
1430

1531
// NewModPullCmd returns the mod pull command.
@@ -20,14 +36,39 @@ func NewModPullCmd(cli *client.KpmClient) *cobra.Command {
2036
Long: modPullDesc,
2137
Example: modPullExample,
2238
RunE: func(_ *cobra.Command, args []string) error {
23-
source := argsGet(args, 0)
2439
localPath := argsGet(args, 1)
25-
return cli.PullFromOci(localPath, source, tag)
40+
return pull(cli, args, localPath)
2641
},
2742
SilenceUsage: true,
2843
}
2944

30-
cmd.Flags().StringVar(&tag, "tag", "", "git repository tag")
45+
cmd.Flags().StringVar(&git, "git", "", "git repository url")
46+
cmd.Flags().StringVar(&oci, "oci", "", "oci repository url")
47+
cmd.Flags().StringVar(&tag, "tag", "", "git or oci repository tag")
48+
cmd.Flags().StringVar(&commit, "commit", "", "git repository commit")
49+
cmd.Flags().StringVar(&branch, "branch", "", "git repository branch")
3150

3251
return cmd
3352
}
53+
54+
func pull(cli *client.KpmClient, args []string, localPath string) error {
55+
sourceUrl, err := ParseUrlFromArgs(cli, args)
56+
if err != nil {
57+
return err
58+
}
59+
source, err := downloader.NewSourceFromStr(sourceUrl.String())
60+
if err != nil {
61+
return err
62+
}
63+
64+
_, err = cli.Pull(
65+
client.WithPullSource(source),
66+
client.WithLocalPath(localPath),
67+
)
68+
69+
if err != nil {
70+
return err
71+
}
72+
73+
return nil
74+
}

cmd/kcl/commands/mod_push.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func pushPackage(ociUrl string, kclPkg *pkg.KclPkg, vendorMode bool, cli *client
169169
"only support url scheme 'oci://'.",
170170
)
171171
}
172-
ociOpts.Annotations, err = kpmoci.GenOciManifestFromPkg(kclPkg)
172+
ociOpts.Annotations, err = kclPkg.GenOciManifestFromPkg()
173173
if err != nil {
174174
return err
175175
}

go.mod

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ require (
3030
github.com/containers/ocicrypt v1.1.10 // indirect
3131
github.com/containers/storage v1.54.0 // indirect
3232
github.com/distribution/reference v0.6.0 // indirect
33+
github.com/elliotchance/orderedmap/v2 v2.2.0 // indirect
3334
github.com/emicklei/proto v1.13.2 // indirect
3435
github.com/felixge/httpsnoop v1.0.4 // indirect
3536
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
@@ -208,3 +209,8 @@ require (
208209
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
209210
sigs.k8s.io/yaml v1.3.0 // indirect
210211
)
212+
213+
replace (
214+
kcl-lang.io/kpm => /Users/zongz/Workspace/kusionstack/kpm
215+
kcl-lang.io/kpm v0.9.0-rc.2 => kcl-lang.io/kpm v0.9.0-rc.2.0.20240704123051-efc7b9c78ae8
216+
)

go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,8 @@ github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25Kn
373373
github.com/elazarl/goproxy v0.0.0-20180725130230-947c36da3153/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc=
374374
github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a h1:mATvB/9r/3gvcejNsXKSkQ6lcIaNec2nyfOdlTBR2lU=
375375
github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a/go.mod h1:Ro8st/ElPeALwNFlcTpWmkr6IoMFfkjXAvTHpevnDsM=
376+
github.com/elliotchance/orderedmap/v2 v2.2.0 h1:7/2iwO98kYT4XkOjA9mBEIwvi4KpGB4cyHeOFOnj4Vk=
377+
github.com/elliotchance/orderedmap/v2 v2.2.0/go.mod h1:85lZyVbpGaGvHvnKa7Qhx7zncAdBIBq6u56Hb1PRU5Q=
376378
github.com/emicklei/go-restful v0.0.0-20170410110728-ff4f55a20633/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs=
377379
github.com/emicklei/go-restful v2.9.5+incompatible/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs=
378380
github.com/emicklei/proto v1.13.2 h1:z/etSFO3uyXeuEsVPzfl56WNgzcvIr42aQazXaQmFZY=
@@ -1689,8 +1691,8 @@ kcl-lang.io/kcl-openapi v0.6.3 h1:6Br0IBaNshRvBfmaH22Zv1miVEt6FWlmh2v/wBLb194=
16891691
kcl-lang.io/kcl-openapi v0.6.3/go.mod h1:Ai9mFztCVKkRSFabczO/r5hCNdqaNtAc2ZIRxTeV0Mk=
16901692
kcl-lang.io/kcl-playground v0.5.1 h1:MKQQUHgt4+2QyU2NVwa73oksOaBJGDi4keGoggA0MiU=
16911693
kcl-lang.io/kcl-playground v0.5.1/go.mod h1:IFmnlw7m011ccX8OidMUfnnN2u/TWdtQGxyABRTbmow=
1692-
kcl-lang.io/kpm v0.9.0-rc.2 h1:0v76+Tk0XnWawXHLoK+XuGTp+7QxD+vNUCzteIgNE0w=
1693-
kcl-lang.io/kpm v0.9.0-rc.2/go.mod h1:mwEU/1fURutTNaO1SeGpoFh1iMf+Cc6SYyDAuIQHGNg=
1694+
kcl-lang.io/kpm v0.9.0-rc.2.0.20240704123051-efc7b9c78ae8 h1:HXGw7kK5b4xiTiinA1R10sEOSYWqPvrCFKmZdTbFAyw=
1695+
kcl-lang.io/kpm v0.9.0-rc.2.0.20240704123051-efc7b9c78ae8/go.mod h1:+8dz5o9l1DEzEYdfRetnAXhrfqKdzTQb3s99DubbHhk=
16941696
kcl-lang.io/lib v0.9.0-rc.2 h1:1FcWnHKvxe5TGtEwTtd3CTg6V8aw4VdEWtYvQEp15Gk=
16951697
kcl-lang.io/lib v0.9.0-rc.2/go.mod h1:tu+tzwGgHLzYZSIxUG/ntipStrxZd6OvutWYPTxS7cs=
16961698
oras.land/oras-go v1.2.5 h1:XpYuAwAb0DfQsunIyMfeET92emK8km3W4yEzZvUbsTo=
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
start to pull 'helloworld'
2-
the lastest version '0.1.1' will be pulled
3-
pulling 'test/helloworld:0.1.1' from 'localhost:5001/test/helloworld'
4-
pulled 'helloworld' in '<workspace>/test_space/localhost:5001/test/helloworld' successfully
1+
start to pull oci://localhost:5001/test/helloworld
2+
the lastest version '0.1.1' will be downloaded
3+
downloading 'test/helloworld:0.1.1' from 'localhost:5001/test/helloworld:0.1.1'
4+
pulled helloworld 0.1.1 successfully
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
start to pull 'helloworld:0.1.1'
2-
pulling 'test/helloworld:0.1.1' from 'localhost:5001/test/helloworld'
3-
pulled 'helloworld:0.1.1' in '<workspace>/test_space/localhost:5001/test/helloworld/0.1.1' successfully
1+
start to pull oci://localhost:5001/test/helloworld?tag=0.1.1
2+
downloading 'test/helloworld:0.1.1' from 'localhost:5001/test/helloworld:0.1.1'
3+
pulled helloworld 0.1.1 successfully
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
kcl mod pull oci://localhost:5001/test/helloworld:0.1.1
1+
kcl mod pull oci://localhost:5001/test/helloworld --tag 0.1.1
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
start to pull 'oci://localhost:5001/test/helloworld:0.1.1'
2-
the lastest version '0.1.1' will be pulled
3-
pulling '/test/helloworld:0.1.1:0.1.1' from 'localhost:5001/test/helloworld:0.1.1'
4-
pulled 'oci://localhost:5001/test/helloworld:0.1.1' in '<workspace>/test_space/localhost:5001/test/helloworld:0.1.1' successfully
1+
start to pull oci://localhost:5001/test/helloworld?tag=0.1.1
2+
downloading 'test/helloworld:0.1.1' from 'localhost:5001/test/helloworld:0.1.1'
3+
pulled helloworld 0.1.1 successfully
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
kcl mod pull git://github.com/kcl-lang/flask-demo-kcl-manifests --commit ade147b

test/e2e/test_suites/test_kcl_mod_pull_2/stderr

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
start to pull https://github.com/kcl-lang/flask-demo-kcl-manifests?commit=ade147b
2+
cloning 'https://github.com/kcl-lang/flask-demo-kcl-manifests' with commit 'ade147b'
3+
pulled flask_manifests 0.0.1 successfully
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
kcl mod pull --git https://github.com/kcl-lang/flask-demo-kcl-manifests --branch main

test/e2e/test_suites/test_kcl_mod_pull_3/stderr

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
start to pull https://github.com/kcl-lang/flask-demo-kcl-manifests?branch=main
2+
cloning 'https://github.com/kcl-lang/flask-demo-kcl-manifests'
3+
pulled flask_manifests 0.0.1 successfully
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
kcl mod pull --oci http://localhost:5001/test/helloworld --tag 0.1.1

test/e2e/test_suites/test_kcl_mod_pull_4/stderr

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
start to pull oci://localhost:5001/test/helloworld?tag=0.1.1
2+
downloading 'test/helloworld:0.1.1' from 'localhost:5001/test/helloworld:0.1.1'
3+
pulled helloworld 0.1.1 successfully

0 commit comments

Comments
 (0)