From 8afe80d3b423d9c78bb1c56d9509edeeadce10ea Mon Sep 17 00:00:00 2001 From: zongz Date: Fri, 8 Nov 2024 14:06:28 +0800 Subject: [PATCH 1/2] feat: make 'kcl mod run' support ModSpec Signed-off-by: zongz --- cmd/kcl/commands/run.go | 18 +++++-- pkg/options/run.go | 53 ++++++++++++++------- test/e2e/test_suites/test_kcl_run_29/input | 1 + test/e2e/test_suites/test_kcl_run_29/stderr | 0 test/e2e/test_suites/test_kcl_run_29/stdout | 1 + test/e2e/test_suites/test_kcl_run_30/input | 1 + test/e2e/test_suites/test_kcl_run_30/stderr | 0 test/e2e/test_suites/test_kcl_run_30/stdout | 1 + test/e2e/test_suites/test_kcl_run_31/input | 1 + test/e2e/test_suites/test_kcl_run_31/stderr | 0 test/e2e/test_suites/test_kcl_run_31/stdout | 1 + test/e2e/test_suites/test_kcl_run_32/input | 1 + test/e2e/test_suites/test_kcl_run_32/stderr | 0 test/e2e/test_suites/test_kcl_run_32/stdout | 1 + 14 files changed, 59 insertions(+), 20 deletions(-) create mode 100644 test/e2e/test_suites/test_kcl_run_29/input create mode 100644 test/e2e/test_suites/test_kcl_run_29/stderr create mode 100644 test/e2e/test_suites/test_kcl_run_29/stdout create mode 100644 test/e2e/test_suites/test_kcl_run_30/input create mode 100644 test/e2e/test_suites/test_kcl_run_30/stderr create mode 100644 test/e2e/test_suites/test_kcl_run_30/stdout create mode 100644 test/e2e/test_suites/test_kcl_run_31/input create mode 100644 test/e2e/test_suites/test_kcl_run_31/stderr create mode 100644 test/e2e/test_suites/test_kcl_run_31/stdout create mode 100644 test/e2e/test_suites/test_kcl_run_32/input create mode 100644 test/e2e/test_suites/test_kcl_run_32/stderr create mode 100644 test/e2e/test_suites/test_kcl_run_32/stdout diff --git a/cmd/kcl/commands/run.go b/cmd/kcl/commands/run.go index f3cb698..d4ca580 100644 --- a/cmd/kcl/commands/run.go +++ b/cmd/kcl/commands/run.go @@ -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. diff --git a/pkg/options/run.go b/pkg/options/run.go index ea47b28..b5f9138 100644 --- a/pkg/options/run.go +++ b/pkg/options/run.go @@ -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 ( @@ -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. @@ -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), @@ -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 { @@ -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 } diff --git a/test/e2e/test_suites/test_kcl_run_29/input b/test/e2e/test_suites/test_kcl_run_29/input new file mode 100644 index 0000000..2172a4e --- /dev/null +++ b/test/e2e/test_suites/test_kcl_run_29/input @@ -0,0 +1 @@ +kcl run subhelloworld --oci https://ghcr.io/kcl-lang/helloworld --tag 0.1.4 \ No newline at end of file diff --git a/test/e2e/test_suites/test_kcl_run_29/stderr b/test/e2e/test_suites/test_kcl_run_29/stderr new file mode 100644 index 0000000..e69de29 diff --git a/test/e2e/test_suites/test_kcl_run_29/stdout b/test/e2e/test_suites/test_kcl_run_29/stdout new file mode 100644 index 0000000..9df0d7c --- /dev/null +++ b/test/e2e/test_suites/test_kcl_run_29/stdout @@ -0,0 +1 @@ +The_first_sub_kcl_program: Hello Sub World! \ No newline at end of file diff --git a/test/e2e/test_suites/test_kcl_run_30/input b/test/e2e/test_suites/test_kcl_run_30/input new file mode 100644 index 0000000..7a790ba --- /dev/null +++ b/test/e2e/test_suites/test_kcl_run_30/input @@ -0,0 +1 @@ +kcl run subhelloworld:0.0.1 --oci https://ghcr.io/kcl-lang/helloworld --tag 0.1.4 \ No newline at end of file diff --git a/test/e2e/test_suites/test_kcl_run_30/stderr b/test/e2e/test_suites/test_kcl_run_30/stderr new file mode 100644 index 0000000..e69de29 diff --git a/test/e2e/test_suites/test_kcl_run_30/stdout b/test/e2e/test_suites/test_kcl_run_30/stdout new file mode 100644 index 0000000..9df0d7c --- /dev/null +++ b/test/e2e/test_suites/test_kcl_run_30/stdout @@ -0,0 +1 @@ +The_first_sub_kcl_program: Hello Sub World! \ No newline at end of file diff --git a/test/e2e/test_suites/test_kcl_run_31/input b/test/e2e/test_suites/test_kcl_run_31/input new file mode 100644 index 0000000..0e25c32 --- /dev/null +++ b/test/e2e/test_suites/test_kcl_run_31/input @@ -0,0 +1 @@ +kcl run cc --git https://github.com/kcl-lang/flask-demo-kcl-manifests --commit 8308200 \ No newline at end of file diff --git a/test/e2e/test_suites/test_kcl_run_31/stderr b/test/e2e/test_suites/test_kcl_run_31/stderr new file mode 100644 index 0000000..e69de29 diff --git a/test/e2e/test_suites/test_kcl_run_31/stdout b/test/e2e/test_suites/test_kcl_run_31/stdout new file mode 100644 index 0000000..f405925 --- /dev/null +++ b/test/e2e/test_suites/test_kcl_run_31/stdout @@ -0,0 +1 @@ +The_first_kcl_program: Hello World! \ No newline at end of file diff --git a/test/e2e/test_suites/test_kcl_run_32/input b/test/e2e/test_suites/test_kcl_run_32/input new file mode 100644 index 0000000..496d0a1 --- /dev/null +++ b/test/e2e/test_suites/test_kcl_run_32/input @@ -0,0 +1 @@ +kcl run cc:0.0.1 --git https://github.com/kcl-lang/flask-demo-kcl-manifests --commit 8308200 \ No newline at end of file diff --git a/test/e2e/test_suites/test_kcl_run_32/stderr b/test/e2e/test_suites/test_kcl_run_32/stderr new file mode 100644 index 0000000..e69de29 diff --git a/test/e2e/test_suites/test_kcl_run_32/stdout b/test/e2e/test_suites/test_kcl_run_32/stdout new file mode 100644 index 0000000..d2dcf2a --- /dev/null +++ b/test/e2e/test_suites/test_kcl_run_32/stdout @@ -0,0 +1 @@ +The_first_kcl_program: Hello World! From 9915b0c4a6a89d4e5c715d3534c16c5a1f9e7cae Mon Sep 17 00:00:00 2001 From: zongz Date: Fri, 8 Nov 2024 17:00:50 +0800 Subject: [PATCH 2/2] fix: fix unit test Signed-off-by: zongz --- pkg/options/run_test.go | 4 ++-- pkg/options/testdata/run_opt/file1.k | 1 + pkg/options/testdata/run_opt/file2.k | 1 + pkg/options/testdata/run_opt/file3.k | 1 + 4 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 pkg/options/testdata/run_opt/file1.k create mode 100644 pkg/options/testdata/run_opt/file2.k create mode 100644 pkg/options/testdata/run_opt/file3.k diff --git a/pkg/options/run_test.go b/pkg/options/run_test.go index 2028ab4..d83e6d4 100644 --- a/pkg/options/run_test.go +++ b/pkg/options/run_test.go @@ -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)) diff --git a/pkg/options/testdata/run_opt/file1.k b/pkg/options/testdata/run_opt/file1.k new file mode 100644 index 0000000..e03bc79 --- /dev/null +++ b/pkg/options/testdata/run_opt/file1.k @@ -0,0 +1 @@ +file1 = 1 \ No newline at end of file diff --git a/pkg/options/testdata/run_opt/file2.k b/pkg/options/testdata/run_opt/file2.k new file mode 100644 index 0000000..da1d487 --- /dev/null +++ b/pkg/options/testdata/run_opt/file2.k @@ -0,0 +1 @@ +file2 = 2 \ No newline at end of file diff --git a/pkg/options/testdata/run_opt/file3.k b/pkg/options/testdata/run_opt/file3.k new file mode 100644 index 0000000..2ab07ef --- /dev/null +++ b/pkg/options/testdata/run_opt/file3.k @@ -0,0 +1 @@ +file3 = 3 \ No newline at end of file