From e5a2392c20c28bd0371a5aefabfe1885eab6df43 Mon Sep 17 00:00:00 2001 From: peefy Date: Sat, 23 Dec 2023 15:30:01 +0800 Subject: [PATCH] fix: kcl run oci source entry Signed-off-by: peefy --- examples/source/makefile | 2 ++ examples/source/oci/makefile | 5 +++++ examples/test.ps1 | 2 +- examples/test.sh | 2 +- go.mod | 2 +- go.sum | 4 ++-- pkg/options/run.go | 25 +++++++++++++++++++++---- 7 files changed, 33 insertions(+), 9 deletions(-) create mode 100644 examples/source/makefile create mode 100644 examples/source/oci/makefile diff --git a/examples/source/makefile b/examples/source/makefile new file mode 100644 index 0000000..9c8397d --- /dev/null +++ b/examples/source/makefile @@ -0,0 +1,2 @@ +test: + make -C oci diff --git a/examples/source/oci/makefile b/examples/source/oci/makefile new file mode 100644 index 0000000..888ba2e --- /dev/null +++ b/examples/source/oci/makefile @@ -0,0 +1,5 @@ +run: + kcl run oci://ghcr.io/kcl-lang/helloworld + +test: + make run diff --git a/examples/test.ps1 b/examples/test.ps1 index 2d19360..a9e1d74 100755 --- a/examples/test.ps1 +++ b/examples/test.ps1 @@ -5,7 +5,7 @@ $ErrorActionPreference = "Stop" $pwd = Split-Path -Parent $MyInvocation.MyCommand.Path -$paths = @("configuration", "validation", "abstraction", "definition", "konfig", "mutation", "data-integration", "automation", "package-management", "kubernetes", "codelab", "server") +$paths = @("configuration", "validation", "abstraction", "definition", "konfig", "mutation", "data-integration", "automation", "package-management", "kubernetes", "codelab", "server", "source") foreach ($path in $paths) { Write-Host "Testing $path ..." Set-Location -Path "$pwd\$path" diff --git a/examples/test.sh b/examples/test.sh index 243db4c..3a9b41e 100755 --- a/examples/test.sh +++ b/examples/test.sh @@ -12,7 +12,7 @@ pwd=$( pwd ) -for path in "configuration" "validation" "abstraction" "definition" "konfig" "mutation" "data-integration" "automation" "package-management" "kubernetes" "codelab" "server"; do +for path in "configuration" "validation" "abstraction" "definition" "konfig" "mutation" "data-integration" "automation" "package-management" "kubernetes" "codelab" "server" "source"; do echo "\033[1mTesting $path ...\033[0m" if (cd $pwd/$path && make test); then echo "\033[32mTest SUCCESSED - $path\033[0m\n" diff --git a/go.mod b/go.mod index a268224..e77f6a0 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( kcl-lang.io/kcl-go v0.7.1 kcl-lang.io/kcl-openapi v0.5.5 kcl-lang.io/kcl-playground v0.5.1-0.20230919072953-347ab8959295 - kcl-lang.io/kpm v0.4.4 + kcl-lang.io/kpm v0.4.6 ) require ( diff --git a/go.sum b/go.sum index 35254b6..246b9cf 100644 --- a/go.sum +++ b/go.sum @@ -1290,8 +1290,8 @@ kcl-lang.io/kcl-openapi v0.5.5 h1:6LSbiy53nczagm7Ohgdy1DVgQ+5ffMihsOUY7PGkbh0= kcl-lang.io/kcl-openapi v0.5.5/go.mod h1:Ai9mFztCVKkRSFabczO/r5hCNdqaNtAc2ZIRxTeV0Mk= kcl-lang.io/kcl-playground v0.5.1-0.20230919072953-347ab8959295 h1:RUY3w6jNAB6GoMo27CO1oS4C4gjyWdMXfOTNpTEnkjs= kcl-lang.io/kcl-playground v0.5.1-0.20230919072953-347ab8959295/go.mod h1:3aMvkPRLMZeORiQ/sHxXQCopwr8L1MMSSXMPC+hxwPA= -kcl-lang.io/kpm v0.4.4 h1:3WoYQEN0AQsFTIyleDRLLzP2l8XAJ7cn2UuADPgRMjs= -kcl-lang.io/kpm v0.4.4/go.mod h1:Yu8uUz7R1NeiP8vJrLQytGVYo+EewJi4EU+rfhYiiYc= +kcl-lang.io/kpm v0.4.6 h1:yaQdRQMqByaxyUb3uEp27zI7xxcsnp1iIcgqU9hFVqI= +kcl-lang.io/kpm v0.4.6/go.mod h1:Ba2ijUHQR/sMdB4S7982vOrajly4LdgshrqXd2SXBog= kcl-lang.io/lib v0.7.3 h1:YsKBo5jrICQ3fJobywkB9dFfVIW/ixCAkqmYQ5Z2lqQ= kcl-lang.io/lib v0.7.3/go.mod h1:ubsalGXxJaa5II/EsHmsI/tL2EluYHIcW+BwzQPt+uY= oras.land/oras-go v1.2.3 h1:v8PJl+gEAntI1pJ/LCrDgsuk+1PKVavVEPsYIHFE5uY= diff --git a/pkg/options/run.go b/pkg/options/run.go index 33909af..c8545f8 100644 --- a/pkg/options/run.go +++ b/pkg/options/run.go @@ -76,15 +76,26 @@ func NewRunOptions() *RunOptions { func (o *RunOptions) Run() error { var result *kcl.KCLResultList var err error - opts := CompileOptionFromCli(o) cli, err := client.NewKpmClient() - if o.Quiet { - cli.SetLogWriter(nil) + if err != nil { + return err } + // acquire the lock of the package cache. + err = cli.AcquirePackageCacheLock() if err != nil { return err } - pwd, err := os.Getwd() + defer func() { + // release the lock of the package cache after the function returns. + releaseErr := cli.ReleasePackageCacheLock() + if releaseErr != nil && err == nil { + err = releaseErr + } + }() + opts := CompileOptionFromCli(o) + if o.Quiet { + cli.SetLogWriter(nil) + } if err != nil { return err } @@ -92,6 +103,10 @@ func (o *RunOptions) Run() error { if errEvent != nil { return errEvent } + pwd, err := os.Getwd() + if err != nil { + return err + } if entry.IsEmpty() { // kcl compiles the current package under '$pwd'. if _, e := api.GetKclPackage(pwd); e == nil { @@ -137,9 +152,11 @@ func (o *RunOptions) Run() error { result, err = cli.CompileWithOpts(opts) } else if entry.IsTar() { // kcl compiles the package from the kcl package tar. + opts.SetEntries([]string{}) result, err = cli.CompileTarPkg(entry.PackageSource(), opts) } else if entry.IsUrl() { // kcl compiles the package from the OCI reference or url. + opts.SetEntries([]string{}) result, err = cli.CompileOciPkg(entry.PackageSource(), o.Tag, opts) } else { // If there is only kcl file without kcl package (kcl.mod)