Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make 'kcl mod run' support ModSpec #165

Merged
merged 2 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions cmd/kcl/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,32 @@ For example, 'kcl run path/to/kcl.k' will run the file named path/to/kcl.k
# Run multiple files
kcl run path/to/kcl1.k path/to/kcl2.k

# Run OCI packages
# Run OCI modules
kcl run oci://ghcr.io/kcl-lang/helloworld --tag 0.1.0

# Run remote Git repo
kcl run git://github.com/kcl-lang/flask-demo-kcl-manifests --commit ade147b

# Run OCI packages by flag
# Run OCI modules by flag
kcl run --oci https://ghcr.io/kcl-lang/helloworld --tag 0.1.0

# Run remote module from Git with branch repo by flag
kcl run --git https://github.com/kcl-lang/flask-demo-kcl-manifests --branch main

# Run remote module from Git with branch repo by flag with ssh url
kcl run --git ssh://github.com/kcl-lang/flask-demo-kcl-manifests --branch main`
kcl run --git ssh://github.com/kcl-lang/flask-demo-kcl-manifests --branch main

# Run OCI submodule by flag
kcl run subhelloworld --oci https://ghcr.io/kcl-lang/helloworld --tag 0.1.4

# Run OCI submodule with version by flag
kcl run subhelloworld:0.0.1 --oci https://ghcr.io/kcl-lang/helloworld --tag 0.1.4

# Run Git submodule by flag
kcl run cc --git https://github.com/kcl-lang/flask-demo-kcl-manifests --commit 8308200

# Run Git submodule by flag
kcl run cc:0.0.1 --git https://github.com/kcl-lang/flask-demo-kcl-manifests --commit 8308200`
)

// NewRunCmd returns the run command.
Expand Down
53 changes: 36 additions & 17 deletions pkg/options/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ import (
"kcl-lang.io/kcl-go/pkg/tools/gen"
"kcl-lang.io/kpm/pkg/client"
"kcl-lang.io/kpm/pkg/constants"
"kcl-lang.io/kpm/pkg/downloader"
"kcl-lang.io/kpm/pkg/opt"
pkg "kcl-lang.io/kpm/pkg/package"
"kcl-lang.io/kpm/pkg/runner"
"kcl-lang.io/kpm/pkg/utils"
)

const (
Expand Down Expand Up @@ -79,6 +81,8 @@ type RunOptions struct {
Format string
// Writer is used to output the run result. Default is os.Stdout.
Writer io.Writer
// ModSpec is the module spec for the KCL module.
ModSpec *downloader.ModSpec
}

// NewRunOptions returns a new instance of RunOptions with default values.
Expand Down Expand Up @@ -125,7 +129,8 @@ func (o *RunOptions) Run() error {
o.Entries[i] = entry
}
}
result, err = cli.Run(

opts := []client.RunOption{
client.WithRunSourceUrls(o.Entries),
client.WithSettingFiles(o.Settings),
client.WithArguments(o.Arguments),
Expand All @@ -140,7 +145,13 @@ func (o *RunOptions) Run() error {
client.WithStrictRange(o.StrictRangeCheck),
client.WithCompileOnly(o.CompileOnly),
client.WithLogger(os.Stdout),
)
}

if o.ModSpec != nil {
opts = append(opts, client.WithRunModSpec(o.ModSpec))
}

result, err = cli.Run(opts...)

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

for _, arg := range args {
argUrl, err := url.Parse(arg)
if err != nil {
return err
}
query := argUrl.Query()
if o.Tag != "" {
query.Set("tag", o.Tag)
}
if o.Commit != "" {
query.Set("commit", o.Commit)
}
if o.Branch != "" {
query.Set("branch", o.Branch)

modSpec := downloader.ModSpec{}
err := modSpec.FromString(arg)
// If the arg is a directory or is not a Mod Spec, parse it as other source
if utils.DirExists(arg) || err != nil {
argUrl, err := url.Parse(arg)
if err != nil {
return err
}
query := argUrl.Query()
if o.Tag != "" {
query.Set("tag", o.Tag)
}
if o.Commit != "" {
query.Set("commit", o.Commit)
}
if o.Branch != "" {
query.Set("branch", o.Branch)
}
argUrl.RawQuery = query.Encode()
o.Entries = append(o.Entries, argUrl.String())
} else {
o.ModSpec = &modSpec
}
argUrl.RawQuery = query.Encode()
o.Entries = append(o.Entries, argUrl.String())
}
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/options/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ spec:

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

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

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

if len(options.Entries) != len(expectedEntries) {
t.Fatalf("unexpected number of entries:\nexpected: %d\ngot: %d", len(expectedEntries), len(options.Entries))
Expand Down
1 change: 1 addition & 0 deletions pkg/options/testdata/run_opt/file1.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
file1 = 1
1 change: 1 addition & 0 deletions pkg/options/testdata/run_opt/file2.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
file2 = 2
1 change: 1 addition & 0 deletions pkg/options/testdata/run_opt/file3.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
file3 = 3
1 change: 1 addition & 0 deletions test/e2e/test_suites/test_kcl_run_29/input
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kcl run subhelloworld --oci https://ghcr.io/kcl-lang/helloworld --tag 0.1.4
Empty file.
1 change: 1 addition & 0 deletions test/e2e/test_suites/test_kcl_run_29/stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_sub_kcl_program: Hello Sub World!
1 change: 1 addition & 0 deletions test/e2e/test_suites/test_kcl_run_30/input
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kcl run subhelloworld:0.0.1 --oci https://ghcr.io/kcl-lang/helloworld --tag 0.1.4
Empty file.
1 change: 1 addition & 0 deletions test/e2e/test_suites/test_kcl_run_30/stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_sub_kcl_program: Hello Sub World!
1 change: 1 addition & 0 deletions test/e2e/test_suites/test_kcl_run_31/input
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kcl run cc --git https://github.com/kcl-lang/flask-demo-kcl-manifests --commit 8308200
Empty file.
1 change: 1 addition & 0 deletions test/e2e/test_suites/test_kcl_run_31/stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program: Hello World!
1 change: 1 addition & 0 deletions test/e2e/test_suites/test_kcl_run_32/input
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kcl run cc:0.0.1 --git https://github.com/kcl-lang/flask-demo-kcl-manifests --commit 8308200
Empty file.
1 change: 1 addition & 0 deletions test/e2e/test_suites/test_kcl_run_32/stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program: Hello World!
Loading