Skip to content

Commit 927fd46

Browse files
authored
Merge pull request #165 from kcl-lang/run-mod-spec
feat: make 'kcl mod run' support ModSpec
2 parents 5fb9c9a + 9915b0c commit 927fd46

File tree

18 files changed

+64
-22
lines changed

18 files changed

+64
-22
lines changed

cmd/kcl/commands/run.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,32 @@ For example, 'kcl run path/to/kcl.k' will run the file named path/to/kcl.k
2727
# Run multiple files
2828
kcl run path/to/kcl1.k path/to/kcl2.k
2929
30-
# Run OCI packages
30+
# Run OCI modules
3131
kcl run oci://ghcr.io/kcl-lang/helloworld --tag 0.1.0
3232
3333
# Run remote Git repo
3434
kcl run git://github.com/kcl-lang/flask-demo-kcl-manifests --commit ade147b
3535
36-
# Run OCI packages by flag
36+
# Run OCI modules by flag
3737
kcl run --oci https://ghcr.io/kcl-lang/helloworld --tag 0.1.0
3838
3939
# Run remote module from Git with branch repo by flag
4040
kcl run --git https://github.com/kcl-lang/flask-demo-kcl-manifests --branch main
4141
4242
# Run remote module from Git with branch repo by flag with ssh url
43-
kcl run --git ssh://github.com/kcl-lang/flask-demo-kcl-manifests --branch main`
43+
kcl run --git ssh://github.com/kcl-lang/flask-demo-kcl-manifests --branch main
44+
45+
# Run OCI submodule by flag
46+
kcl run subhelloworld --oci https://ghcr.io/kcl-lang/helloworld --tag 0.1.4
47+
48+
# Run OCI submodule with version by flag
49+
kcl run subhelloworld:0.0.1 --oci https://ghcr.io/kcl-lang/helloworld --tag 0.1.4
50+
51+
# Run Git submodule by flag
52+
kcl run cc --git https://github.com/kcl-lang/flask-demo-kcl-manifests --commit 8308200
53+
54+
# Run Git submodule by flag
55+
kcl run cc:0.0.1 --git https://github.com/kcl-lang/flask-demo-kcl-manifests --commit 8308200`
4456
)
4557

4658
// NewRunCmd returns the run command.

pkg/options/run.go

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ import (
2020
"kcl-lang.io/kcl-go/pkg/tools/gen"
2121
"kcl-lang.io/kpm/pkg/client"
2222
"kcl-lang.io/kpm/pkg/constants"
23+
"kcl-lang.io/kpm/pkg/downloader"
2324
"kcl-lang.io/kpm/pkg/opt"
2425
pkg "kcl-lang.io/kpm/pkg/package"
2526
"kcl-lang.io/kpm/pkg/runner"
27+
"kcl-lang.io/kpm/pkg/utils"
2628
)
2729

2830
const (
@@ -79,6 +81,8 @@ type RunOptions struct {
7981
Format string
8082
// Writer is used to output the run result. Default is os.Stdout.
8183
Writer io.Writer
84+
// ModSpec is the module spec for the KCL module.
85+
ModSpec *downloader.ModSpec
8286
}
8387

8488
// NewRunOptions returns a new instance of RunOptions with default values.
@@ -125,7 +129,8 @@ func (o *RunOptions) Run() error {
125129
o.Entries[i] = entry
126130
}
127131
}
128-
result, err = cli.Run(
132+
133+
opts := []client.RunOption{
129134
client.WithRunSourceUrls(o.Entries),
130135
client.WithSettingFiles(o.Settings),
131136
client.WithArguments(o.Arguments),
@@ -140,7 +145,13 @@ func (o *RunOptions) Run() error {
140145
client.WithStrictRange(o.StrictRangeCheck),
141146
client.WithCompileOnly(o.CompileOnly),
142147
client.WithLogger(os.Stdout),
143-
)
148+
}
149+
150+
if o.ModSpec != nil {
151+
opts = append(opts, client.WithRunModSpec(o.ModSpec))
152+
}
153+
154+
result, err = cli.Run(opts...)
144155

145156
if err != nil {
146157
if o.NoStyle {
@@ -196,22 +207,30 @@ func (o *RunOptions) Complete(args []string) error {
196207
}
197208

198209
for _, arg := range args {
199-
argUrl, err := url.Parse(arg)
200-
if err != nil {
201-
return err
202-
}
203-
query := argUrl.Query()
204-
if o.Tag != "" {
205-
query.Set("tag", o.Tag)
206-
}
207-
if o.Commit != "" {
208-
query.Set("commit", o.Commit)
209-
}
210-
if o.Branch != "" {
211-
query.Set("branch", o.Branch)
210+
211+
modSpec := downloader.ModSpec{}
212+
err := modSpec.FromString(arg)
213+
// If the arg is a directory or is not a Mod Spec, parse it as other source
214+
if utils.DirExists(arg) || err != nil {
215+
argUrl, err := url.Parse(arg)
216+
if err != nil {
217+
return err
218+
}
219+
query := argUrl.Query()
220+
if o.Tag != "" {
221+
query.Set("tag", o.Tag)
222+
}
223+
if o.Commit != "" {
224+
query.Set("commit", o.Commit)
225+
}
226+
if o.Branch != "" {
227+
query.Set("branch", o.Branch)
228+
}
229+
argUrl.RawQuery = query.Encode()
230+
o.Entries = append(o.Entries, argUrl.String())
231+
} else {
232+
o.ModSpec = &modSpec
212233
}
213-
argUrl.RawQuery = query.Encode()
214-
o.Entries = append(o.Entries, argUrl.String())
215234
}
216235
return nil
217236
}

pkg/options/run_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,14 @@ spec:
108108

109109
func TestRunOptions_Complete(t *testing.T) {
110110
options := NewRunOptions()
111-
args := []string{"file1.k", "file2.k", "file3.k"}
111+
args := []string{"./testdata/run_opt/file1.k", "./testdata/run_opt/file2.k", "./testdata/run_opt/file3.k"}
112112

113113
err := options.Complete(args)
114114
if err != nil {
115115
t.Errorf("RunOptions.Complete() failed: %v", err)
116116
}
117117

118-
expectedEntries := []string{"file1.k", "file2.k", "file3.k"}
118+
expectedEntries := []string{"./testdata/run_opt/file1.k", "./testdata/run_opt/file2.k", "./testdata/run_opt/file3.k"}
119119

120120
if len(options.Entries) != len(expectedEntries) {
121121
t.Fatalf("unexpected number of entries:\nexpected: %d\ngot: %d", len(expectedEntries), len(options.Entries))

pkg/options/testdata/run_opt/file1.k

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
file1 = 1

pkg/options/testdata/run_opt/file2.k

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
file2 = 2

pkg/options/testdata/run_opt/file3.k

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
file3 = 3
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
kcl run subhelloworld --oci https://ghcr.io/kcl-lang/helloworld --tag 0.1.4

test/e2e/test_suites/test_kcl_run_29/stderr

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The_first_sub_kcl_program: Hello Sub World!
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
kcl run subhelloworld:0.0.1 --oci https://ghcr.io/kcl-lang/helloworld --tag 0.1.4

test/e2e/test_suites/test_kcl_run_30/stderr

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The_first_sub_kcl_program: Hello Sub World!
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
kcl run cc --git https://github.com/kcl-lang/flask-demo-kcl-manifests --commit 8308200

test/e2e/test_suites/test_kcl_run_31/stderr

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The_first_kcl_program: Hello World!
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
kcl run cc:0.0.1 --git https://github.com/kcl-lang/flask-demo-kcl-manifests --commit 8308200

test/e2e/test_suites/test_kcl_run_32/stderr

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The_first_kcl_program: Hello World!

0 commit comments

Comments
 (0)