Skip to content

Commit 20d46fa

Browse files
authored
Merge pull request #163 from kcl-lang/pull-mod-spec
feat: 'kcl mod pull' supports ModSpec
2 parents c567635 + d27dfba commit 20d46fa

File tree

14 files changed

+152
-21
lines changed

14 files changed

+152
-21
lines changed

.github/workflows/ci.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ jobs:
3030
GO111MODULE: on
3131
run: |
3232
make cover
33+
34+
- name: Running e2e tests with coverage
35+
env:
36+
GO111MODULE: on
37+
run: |
38+
make e2e
3339
- name: Send coverage
3440
uses: shogo82148/actions-goveralls@v1
3541
with:

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,5 @@ _kcl_test.k
3737

3838
# e2e test cases dir
3939
scripts/e2e/pkg_in_reg/*
40-
scripts/e2e/registry_auth/*
40+
scripts/e2e/registry_auth/*
41+
scripts/registry_auth/*

cmd/kcl/commands/args.go

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

33
import (
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

1114
func 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+
18120
func ParseUrlFromArgs(cli *client.KpmClient, args []string) (*url.URL, error) {
19121
var sourceUrl url.URL
20122

cmd/kcl/commands/mod_pull.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package cmd
33
import (
44
"github.com/spf13/cobra"
55
"kcl-lang.io/kpm/pkg/client"
6-
"kcl-lang.io/kpm/pkg/downloader"
76
)
87

98
const (
@@ -28,7 +27,19 @@ const (
2827
kcl mod pull --git ssh://github.com/kcl-lang/konfig --tag v0.4.0
2928
3029
# Pull the module from the OCI Registry by flag
31-
kcl mod pull --oci https://ghcr.io/kcl-lang/helloworld --tag 0.1.0`
30+
kcl mod pull --oci https://ghcr.io/kcl-lang/helloworld --tag 0.1.0
31+
32+
# Pull the module from the OCI Registry by flag and specify the module spce
33+
kcl mod pull subhelloworld --oci https://ghcr.io/kcl-lang/helloworld --tag 0.1.4
34+
35+
# Pull the module from the OCI Registry by flag and specify the module spce with version
36+
kcl mod pull subhelloworld:0.0.1 --oci https://ghcr.io/kcl-lang/helloworld --tag 0.1.4
37+
38+
# Pull the module from the Git Repo by flag and specify the module spce
39+
kcl mod pull cc --git git://github.com/kcl-lang/flask-demo-kcl-manifests.git --commit 8308200
40+
41+
# Pull the module from the Git Repo by flag and specify the module spce with version
42+
kcl mod pull cc:0.0.1 --git git://github.com/kcl-lang/flask-demo-kcl-manifests.git --commit 8308200`
3243
)
3344

3445
// NewModPullCmd returns the mod pull command.
@@ -56,11 +67,7 @@ func NewModPullCmd(cli *client.KpmClient) *cobra.Command {
5667
}
5768

5869
func pull(cli *client.KpmClient, args []string, localPath string) error {
59-
sourceUrl, err := ParseUrlFromArgs(cli, args)
60-
if err != nil {
61-
return err
62-
}
63-
source, err := downloader.NewSourceFromStr(sourceUrl.String())
70+
source, err := ParseSourceFromArgs(cli, args)
6471
if err != nil {
6572
return err
6673
}

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ require (
1313
kcl-lang.io/kcl-go v0.10.8
1414
kcl-lang.io/kcl-openapi v0.10.0
1515
kcl-lang.io/kcl-plugin v0.6.0
16-
kcl-lang.io/kpm v0.10.0
16+
kcl-lang.io/kpm v0.10.1-0.20241107094147-b05e196b2c05
1717
)
1818

1919
require (
@@ -33,6 +33,7 @@ require (
3333
github.com/containers/ocicrypt v1.2.0 // indirect
3434
github.com/containers/storage v1.55.0 // indirect
3535
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
36+
github.com/dchest/siphash v1.2.3 // indirect
3637
github.com/distribution/reference v0.6.0 // indirect
3738
github.com/ebitengine/purego v0.7.1 // indirect
3839
github.com/elliotchance/orderedmap/v2 v2.4.0 // indirect
@@ -47,6 +48,7 @@ require (
4748
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
4849
github.com/hashicorp/go-getter v1.7.6 // indirect
4950
github.com/hashicorp/go-safetemp v1.0.0 // indirect
51+
github.com/jinzhu/copier v0.4.0 // indirect
5052
github.com/jmespath/go-jmespath v0.4.0 // indirect
5153
github.com/julienschmidt/httprouter v1.3.0 // indirect
5254
github.com/kubescape/go-git-url v0.0.30 // indirect

go.sum

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
336336
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
337337
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
338338
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
339+
github.com/dchest/siphash v1.2.3 h1:QXwFc8cFOR2dSa/gE6o/HokBMWtLUaNDVd+22aKHeEA=
340+
github.com/dchest/siphash v1.2.3/go.mod h1:0NvQU092bT0ipiFN++/rXm69QG9tVxLAlQHIXMPAkHc=
339341
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
340342
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
341343
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
@@ -664,6 +666,8 @@ github.com/invopop/yaml v0.3.1 h1:f0+ZpmhfBSS4MhG+4HYseMdJhoeeopbSKbq5Rpeelso=
664666
github.com/invopop/yaml v0.3.1/go.mod h1:PMOp3nn4/12yEZUFfmOuNHJsZToEEOwoWsT+D81KkeA=
665667
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
666668
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
669+
github.com/jinzhu/copier v0.4.0 h1:w3ciUoD19shMCRargcpm0cm91ytaBhDvuRpz1ODO/U8=
670+
github.com/jinzhu/copier v0.4.0/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg=
667671
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
668672
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
669673
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
@@ -1698,8 +1702,8 @@ kcl-lang.io/kcl-openapi v0.10.0 h1:yetZMSnn/HHaMcfiLt1P2zhCF06O33jxkjtHrm08VR8=
16981702
kcl-lang.io/kcl-openapi v0.10.0/go.mod h1:kGCf0AZygrZyB+xpmMtiC3FYoiV/1rCLXuAq2QtuLf8=
16991703
kcl-lang.io/kcl-plugin v0.6.0 h1:rBdoqKDPdOtojeOHCFnXoB/I7ltFjV61r0KkfOcL5sE=
17001704
kcl-lang.io/kcl-plugin v0.6.0/go.mod h1:LoIouleHYRKAvFcdW30yUlhsMYH2W9zD5Ji1XHfbht4=
1701-
kcl-lang.io/kpm v0.10.0 h1:VnsJ5IS8YSvgXYnItLdaJp/1tTrSSmThzmNCWmhm5Bg=
1702-
kcl-lang.io/kpm v0.10.0/go.mod h1:MhQh9kewILcUlhuhVBksxWVZfgdwkpkX6xFj1pxF0QM=
1705+
kcl-lang.io/kpm v0.10.1-0.20241107094147-b05e196b2c05 h1:J+Leim2cR1h6KQ0DhurhrrctgVnLrhCC/wsgJWhcBw8=
1706+
kcl-lang.io/kpm v0.10.1-0.20241107094147-b05e196b2c05/go.mod h1:1ndoNvUQdYNgoiQHIkGGRUQIWLU8BSslIbJhk9B5Kco=
17031707
kcl-lang.io/lib v0.10.8 h1:/Mhko6fngIstvdx9dAS3H6N1utogkWfoARVj643l5nU=
17041708
kcl-lang.io/lib v0.10.8/go.mod h1:0Dw/MQwRMjLDksxl4JerGBn/ueaxRyCCKBCCwQwJ1MI=
17051709
oras.land/oras-go v1.2.6 h1:z8cmxQXBU8yZ4mkytWqXfo6tZcamPwjsuxYU81xJ8Lk=

scripts/e2e/pull_pkg.sh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@ cd ./scripts/e2e/pkg_in_reg/
1313

1414

1515
# Check if file exists
16-
if [ ! -d "./oci/ghcr.io/kcl-lang/k8s/1.28" ]; then
16+
if [ ! -d "./oci/ghcr.io/kcl-lang/k8s/1.28/k8s/1.28" ]; then
1717
$current_dir/bin/kcl mod pull k8s:1.28
1818
fi
1919

20-
if [ ! -d "./oci/ghcr.io/kcl-lang/helloworld/0.1.1" ]; then
20+
if [ ! -d "./oci/ghcr.io/kcl-lang/k8s/1.31.2/k8s/1.31.2" ]; then
21+
$current_dir/bin/kcl mod pull k8s:1.31.2
22+
fi
23+
24+
if [ ! -d "./oci/ghcr.io/kcl-lang/helloworld/0.1.1/helloworld/0.1.1" ]; then
2125
$current_dir/bin/kcl mod pull helloworld:0.1.1
2226
fi
2327

scripts/e2e/push_pkg.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@ echo $current_dir
1111

1212
$current_dir/bin/kcl registry login -u test -p 1234 localhost:5001
1313

14-
cd ./scripts/e2e/pkg_in_reg/oci/ghcr.io/kcl-lang/k8s/1.28
14+
cd ./scripts/e2e/pkg_in_reg/oci/ghcr.io/kcl-lang/k8s/1.28/k8s/1.28
15+
$current_dir/bin/kcl mod push
16+
17+
cd "$current_dir"
18+
19+
cd ./scripts/e2e/pkg_in_reg/oci/ghcr.io/kcl-lang/k8s/1.31.2/k8s/1.31.2
1520
$current_dir/bin/kcl mod push
1621

1722
cd "$current_dir"
1823

1924
# Push the package helloworld/0.1.1 to the registry
20-
cd ./scripts/e2e/pkg_in_reg/oci/ghcr.io/kcl-lang/helloworld/0.1.1
25+
cd ./scripts/e2e/pkg_in_reg/oci/ghcr.io/kcl-lang/helloworld/0.1.1/helloworld/0.1.1
2126
$current_dir/bin/kcl mod push
2227

2328
cd "$current_dir"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
The_first_kcl_program: Hello World!
1+
sub: SUB
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
The_first_kcl_program: Hello World!
1+
The_sub_kcl_program: Hello Sub World!

0 commit comments

Comments
 (0)