diff --git a/pkg/api/deperated.go b/pkg/api/deperated.go new file mode 100644 index 00000000..cf003fc7 --- /dev/null +++ b/pkg/api/deperated.go @@ -0,0 +1,43 @@ +package api + +import ( + "path/filepath" + + "kcl-lang.io/kcl-go/pkg/kcl" + "kcl-lang.io/kpm/pkg/client" + "kcl-lang.io/kpm/pkg/opt" +) + +// CompileWithOpt will compile the kcl program without kcl package. +// Deprecated: This method will not be maintained in the future. Use RunWithOpts instead. +func RunWithOpt(opts *opt.CompileOptions) (*kcl.KCLResultList, error) { + // The entries will override the entries in the settings file. + if opts.HasSettingsYaml() && len(opts.KFilenameList) > 0 && len(opts.Entries()) > 0 { + opts.KFilenameList = []string{} + } + if len(opts.Entries()) > 0 { + for _, entry := range opts.Entries() { + if filepath.IsAbs(entry) { + opts.Merge(kcl.WithKFilenames(entry)) + } else { + opts.Merge(kcl.WithKFilenames(filepath.Join(opts.PkgPath(), entry))) + } + } + } else if !opts.HasSettingsYaml() && len(opts.KFilenameList) == 0 { + // If no entry, no kcl files and no settings files. + opts.Merge(kcl.WithKFilenames(opts.PkgPath())) + } + opts.Merge(kcl.WithWorkDir(opts.PkgPath())) + return kcl.RunWithOpts(*opts.Option) +} + +// RunPkgWithOpt will compile the kcl package with the compile options. +// Deprecated: This method will not be maintained in the future. Use RunWithOpts instead. +func RunPkgWithOpt(opts *opt.CompileOptions) (*kcl.KCLResultList, error) { + kpmcli, err := client.NewKpmClient() + kpmcli.SetNoSumCheck(opts.NoSumCheck()) + if err != nil { + return nil, err + } + return run(kpmcli, opts) +} diff --git a/pkg/api/deperated_test.go b/pkg/api/deperated_test.go new file mode 100644 index 00000000..9c312aa8 --- /dev/null +++ b/pkg/api/deperated_test.go @@ -0,0 +1,118 @@ +package api + +import ( + "bytes" + "fmt" + "os" + "path/filepath" + "testing" + + "gotest.tools/v3/assert" + "kcl-lang.io/kcl-go/pkg/kcl" + "kcl-lang.io/kpm/pkg/opt" + "kcl-lang.io/kpm/pkg/utils" +) + +func TestGetEntries(t *testing.T) { + testPath := getTestDir("test_get_entries") + pkgPath := filepath.Join(testPath, "no_entries") + pkg, err := GetKclPackage(pkgPath) + assert.Equal(t, err, nil) + assert.Equal(t, len(pkg.GetPkgProfile().GetEntries()), 0) + + pkgPath = filepath.Join(testPath, "with_path_entries") + pkg, err = GetKclPackage(pkgPath) + assert.Equal(t, err, nil) + assert.Equal(t, len(pkg.GetPkgProfile().GetEntries()), 1) + + res, err := RunWithOpts( + opt.WithEntries(pkg.GetPkgProfile().GetEntries()), + opt.WithKclOption(kcl.WithWorkDir(pkgPath)), + ) + + assert.Equal(t, err, nil) + assert.Equal(t, res.GetRawYamlResult(), "sub: test in sub") + assert.Equal(t, res.GetRawJsonResult(), "{\"sub\": \"test in sub\"}") +} + +func TestRunWithOpts(t *testing.T) { + pkgPath := getTestDir("test_run_pkg_in_path") + opts := opt.DefaultCompileOptions() + opts.AddEntry(filepath.Join(pkgPath, "test_kcl", "main.k")) + opts.SetPkgPath(filepath.Join(pkgPath, "test_kcl")) + result, err := RunPkgWithOpt(opts) + fmt.Printf("err: %v\n", err) + assert.Equal(t, err, nil) + expected, _ := os.ReadFile(filepath.Join(pkgPath, "expected")) + assert.Equal(t, utils.RmNewline(string(result.GetRawYamlResult())), utils.RmNewline(string(expected))) + expectedJson, _ := os.ReadFile(filepath.Join(pkgPath, "expected.json")) + assert.Equal(t, utils.RmNewline(string(result.GetRawJsonResult())), utils.RmNewline(string(expectedJson))) +} + +func TestRunPkgWithOpts(t *testing.T) { + pkgPath := getTestDir("test_run_pkg_in_path") + + result, err := RunWithOpts( + opt.WithNoSumCheck(false), + opt.WithEntries([]string{filepath.Join(pkgPath, "test_kcl", "main.k")}), + opt.WithKclOption(kcl.WithWorkDir(filepath.Join(pkgPath, "test_kcl"))), + ) + + assert.Equal(t, err, nil) + expected, _ := os.ReadFile(filepath.Join(pkgPath, "expected")) + assert.Equal(t, utils.RmNewline(string(result.GetRawYamlResult())), utils.RmNewline(string(expected))) + expectedJson, _ := os.ReadFile(filepath.Join(pkgPath, "expected.json")) + assert.Equal(t, utils.RmNewline(string(result.GetRawJsonResult())), utils.RmNewline(string(expectedJson))) +} + +func TestRunWithOptsAndNoSumCheck(t *testing.T) { + pkgPath := filepath.Join(getTestDir("test_run_pkg_in_path"), "test_run_no_sum_check") + testCases := []string{"dep_git_commit", "dep_git_tag", "dep_oci"} + + for _, testCase := range testCases { + + pathMainK := filepath.Join(pkgPath, testCase, "main.k") + workDir := filepath.Join(pkgPath, testCase) + modLock := filepath.Join(workDir, "kcl.mod.lock") + expected, err := os.ReadFile(filepath.Join(pkgPath, testCase, "expected")) + assert.Equal(t, err, nil) + fmt.Printf("testCase: %v\n", testCase) + res, err := RunWithOpts( + opt.WithNoSumCheck(true), + opt.WithEntries([]string{pathMainK}), + opt.WithKclOption(kcl.WithWorkDir(workDir)), + ) + assert.Equal(t, err, nil) + assert.Equal(t, utils.DirExists(modLock), false) + assert.Equal(t, utils.RmNewline(res.GetRawYamlResult()), utils.RmNewline(string(expected))) + assert.Equal(t, err, nil) + } +} + +func TestRunWithOptsWithNoLog(t *testing.T) { + pkgPath := filepath.Join(getTestDir("test_run_pkg_in_path"), "test_run_with_no_log") + + defer func() { + _ = os.Remove(filepath.Join(pkgPath, "kcl.mod.lock")) + }() + + old := os.Stdout + r, w, _ := os.Pipe() + os.Stdout = w + + pathMainK := filepath.Join(pkgPath, "main.k") + + _, err := RunWithOpts( + opt.WithLogWriter(nil), + opt.WithEntries([]string{pathMainK}), + opt.WithKclOption(kcl.WithWorkDir(pkgPath)), + ) + assert.Equal(t, err, nil) + os.Stdout = old + w.Close() + var buf bytes.Buffer + _, err = buf.ReadFrom(r) + assert.Equal(t, err, nil) + + assert.Equal(t, buf.String(), "") +} diff --git a/pkg/api/kpm_pkg_test.go b/pkg/api/kpm_pkg_test.go index 86e37d07..bad522cb 100644 --- a/pkg/api/kpm_pkg_test.go +++ b/pkg/api/kpm_pkg_test.go @@ -6,9 +6,7 @@ import ( "testing" "gotest.tools/v3/assert" - "kcl-lang.io/kcl-go/pkg/kcl" "kcl-lang.io/kpm/pkg/client" - "kcl-lang.io/kpm/pkg/opt" ) func TestPackageApi(t *testing.T) { @@ -32,7 +30,7 @@ func TestPackageApi(t *testing.T) { assert.Equal(t, dep.Version, "1.27") assert.Equal(t, dep.Source.Registry.Oci.Reg, "ghcr.io") assert.Equal(t, dep.Source.Registry.Oci.Repo, "kcl-lang/k8s") - assert.Equal(t, dep.Source.Registry .Oci.Tag, "1.27") + assert.Equal(t, dep.Source.Registry.Oci.Tag, "1.27") assert.Equal(t, dep.GetLocalFullPath(""), filepath.Join(kcl_pkg_path, "k8s_1.27")) @@ -187,28 +185,6 @@ func TestGetSchemaTypeUnderEmptyDir(t *testing.T) { assert.Equal(t, schemas[filepath.Join(".")]["SchemaInMain"].SchemaName, "SchemaInMain") } -func TestGetEntries(t *testing.T) { - testPath := getTestDir("test_get_entries") - pkgPath := filepath.Join(testPath, "no_entries") - pkg, err := GetKclPackage(pkgPath) - assert.Equal(t, err, nil) - assert.Equal(t, len(pkg.GetPkgProfile().GetEntries()), 0) - - pkgPath = filepath.Join(testPath, "with_path_entries") - pkg, err = GetKclPackage(pkgPath) - assert.Equal(t, err, nil) - assert.Equal(t, len(pkg.GetPkgProfile().GetEntries()), 1) - - res, err := RunWithOpts( - opt.WithEntries(pkg.GetPkgProfile().GetEntries()), - opt.WithKclOption(kcl.WithWorkDir(pkgPath)), - ) - - assert.Equal(t, err, nil) - assert.Equal(t, res.GetRawYamlResult(), "sub: test in sub") - assert.Equal(t, res.GetRawJsonResult(), "{\"sub\": \"test in sub\"}") -} - func TestExportSwaggerV2Spec(t *testing.T) { pkg_path := filepath.Join(getTestDir("test_kpm_package"), "export_swagger", "aaa") pkg, err := GetKclPackage(pkg_path) diff --git a/pkg/api/kpm_run.go b/pkg/api/kpm_run.go index 2f7dc953..9e5778e9 100644 --- a/pkg/api/kpm_run.go +++ b/pkg/api/kpm_run.go @@ -65,29 +65,6 @@ func RunPkgInPath(opts *opt.CompileOptions) (string, error) { return compileResult.GetRawYamlResult(), nil } -// CompileWithOpt will compile the kcl program without kcl package. -// Deprecated: This method will not be maintained in the future. Use RunWithOpts instead. -func RunWithOpt(opts *opt.CompileOptions) (*kcl.KCLResultList, error) { - // The entries will override the entries in the settings file. - if opts.HasSettingsYaml() && len(opts.KFilenameList) > 0 && len(opts.Entries()) > 0 { - opts.KFilenameList = []string{} - } - if len(opts.Entries()) > 0 { - for _, entry := range opts.Entries() { - if filepath.IsAbs(entry) { - opts.Merge(kcl.WithKFilenames(entry)) - } else { - opts.Merge(kcl.WithKFilenames(filepath.Join(opts.PkgPath(), entry))) - } - } - } else if !opts.HasSettingsYaml() && len(opts.KFilenameList) == 0 { - // If no entry, no kcl files and no settings files. - opts.Merge(kcl.WithKFilenames(opts.PkgPath())) - } - opts.Merge(kcl.WithWorkDir(opts.PkgPath())) - return kcl.RunWithOpts(*opts.Option) -} - // RunWithOpts will compile the kcl package with the compile options. func RunWithOpts(opts ...opt.Option) (*kcl.KCLResultList, error) { mergedOpts := opt.DefaultCompileOptions() @@ -115,17 +92,6 @@ func getAbsInputPath(pkgPath string, inputPath string) (string, error) { return "", errors.EntryFileNotFound } -// RunPkgWithOpt will compile the kcl package with the compile options. -// Deprecated: This method will not be maintained in the future. Use RunWithOpts instead. -func RunPkgWithOpt(opts *opt.CompileOptions) (*kcl.KCLResultList, error) { - kpmcli, err := client.NewKpmClient() - kpmcli.SetNoSumCheck(opts.NoSumCheck()) - if err != nil { - return nil, err - } - return run(kpmcli, opts) -} - func runPkgWithOpt(opts *opt.CompileOptions) (*kcl.KCLResultList, error) { kpmcli, err := client.NewKpmClient() kpmcli.SetNoSumCheck(opts.NoSumCheck()) diff --git a/pkg/api/kpm_run_test.go b/pkg/api/kpm_run_test.go index 41a4f0e2..9a9a0605 100644 --- a/pkg/api/kpm_run_test.go +++ b/pkg/api/kpm_run_test.go @@ -1,7 +1,6 @@ package api import ( - "bytes" "fmt" "os" "path/filepath" @@ -111,20 +110,6 @@ func TestRunWithWorkdir(t *testing.T) { assert.Equal(t, result, "base: base\nmain: main") } -func TestRunWithOpts(t *testing.T) { - pkgPath := getTestDir("test_run_pkg_in_path") - opts := opt.DefaultCompileOptions() - opts.AddEntry(filepath.Join(pkgPath, "test_kcl", "main.k")) - opts.SetPkgPath(filepath.Join(pkgPath, "test_kcl")) - result, err := RunPkgWithOpt(opts) - fmt.Printf("err: %v\n", err) - assert.Equal(t, err, nil) - expected, _ := os.ReadFile(filepath.Join(pkgPath, "expected")) - assert.Equal(t, utils.RmNewline(string(result.GetRawYamlResult())), utils.RmNewline(string(expected))) - expectedJson, _ := os.ReadFile(filepath.Join(pkgPath, "expected.json")) - assert.Equal(t, utils.RmNewline(string(result.GetRawJsonResult())), utils.RmNewline(string(expectedJson))) -} - func TestRunWithSettingsOpts(t *testing.T) { pkgPath := getTestDir("test_settings") opts := opt.DefaultCompileOptions() @@ -190,74 +175,6 @@ func TestRunWithNoSumCheck(t *testing.T) { }() } -func TestRunPkgWithOpts(t *testing.T) { - pkgPath := getTestDir("test_run_pkg_in_path") - - result, err := RunWithOpts( - opt.WithNoSumCheck(false), - opt.WithEntries([]string{filepath.Join(pkgPath, "test_kcl", "main.k")}), - opt.WithKclOption(kcl.WithWorkDir(filepath.Join(pkgPath, "test_kcl"))), - ) - - assert.Equal(t, err, nil) - expected, _ := os.ReadFile(filepath.Join(pkgPath, "expected")) - assert.Equal(t, utils.RmNewline(string(result.GetRawYamlResult())), utils.RmNewline(string(expected))) - expectedJson, _ := os.ReadFile(filepath.Join(pkgPath, "expected.json")) - assert.Equal(t, utils.RmNewline(string(result.GetRawJsonResult())), utils.RmNewline(string(expectedJson))) -} - -func TestRunWithOptsAndNoSumCheck(t *testing.T) { - pkgPath := filepath.Join(getTestDir("test_run_pkg_in_path"), "test_run_no_sum_check") - testCases := []string{"dep_git_commit", "dep_git_tag", "dep_oci"} - - for _, testCase := range testCases { - - pathMainK := filepath.Join(pkgPath, testCase, "main.k") - workDir := filepath.Join(pkgPath, testCase) - modLock := filepath.Join(workDir, "kcl.mod.lock") - expected, err := os.ReadFile(filepath.Join(pkgPath, testCase, "expected")) - assert.Equal(t, err, nil) - fmt.Printf("testCase: %v\n", testCase) - res, err := RunWithOpts( - opt.WithNoSumCheck(true), - opt.WithEntries([]string{pathMainK}), - opt.WithKclOption(kcl.WithWorkDir(workDir)), - ) - assert.Equal(t, err, nil) - assert.Equal(t, utils.DirExists(modLock), false) - assert.Equal(t, utils.RmNewline(res.GetRawYamlResult()), utils.RmNewline(string(expected))) - assert.Equal(t, err, nil) - } -} - -func TestRunWithOptsWithNoLog(t *testing.T) { - pkgPath := filepath.Join(getTestDir("test_run_pkg_in_path"), "test_run_with_no_log") - - defer func() { - _ = os.Remove(filepath.Join(pkgPath, "kcl.mod.lock")) - }() - - old := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - - pathMainK := filepath.Join(pkgPath, "main.k") - - _, err := RunWithOpts( - opt.WithLogWriter(nil), - opt.WithEntries([]string{pathMainK}), - opt.WithKclOption(kcl.WithWorkDir(pkgPath)), - ) - assert.Equal(t, err, nil) - os.Stdout = old - w.Close() - var buf bytes.Buffer - _, err = buf.ReadFrom(r) - assert.Equal(t, err, nil) - - assert.Equal(t, buf.String(), "") -} - func TestStoreModAndModLockFile(t *testing.T) { testPath := getTestDir("store_mod_and_lock") diff --git a/pkg/client/client.go b/pkg/client/client.go index 5b1bd287..d575c9e0 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -490,200 +490,6 @@ func (c *KpmClient) Compile(kclPkg *pkg.KclPkg, kclvmCompiler *runner.Compiler) return kclvmCompiler.Run() } -// CompileWithOpts will compile the kcl program with the compile options. -func (c *KpmClient) CompileWithOpts(opts *opt.CompileOptions) (*kcl.KCLResultList, error) { - pkgPath, err := filepath.Abs(opts.PkgPath()) - if err != nil { - return nil, reporter.NewErrorEvent(reporter.Bug, err, "internal bugs, please contact us to fix it.") - } - - c.noSumCheck = opts.NoSumCheck() - c.logWriter = opts.LogWriter() - - kclPkg, err := c.LoadPkgFromPath(pkgPath) - if err != nil { - return nil, err - } - - kclPkg.SetVendorMode(opts.IsVendor()) - - globalPkgPath, err := env.GetAbsPkgPath() - if err != nil { - return nil, err - } - - err = kclPkg.ValidateKpmHome(globalPkgPath) - if err != (*reporter.KpmEvent)(nil) { - return nil, err - } - // add all the options from 'kcl.mod' - opts.Merge(*kclPkg.GetKclOpts()) - if len(opts.Entries()) > 0 { - // add entry from '--input' - for _, entry := range opts.Entries() { - if filepath.IsAbs(entry) { - opts.Merge(kcl.WithKFilenames(entry)) - } else { - opts.Merge(kcl.WithKFilenames(filepath.Join(opts.PkgPath(), entry))) - } - } - } else if len(kclPkg.GetEntryKclFilesFromModFile()) == 0 { - // No entries profile in kcl.mod and no file settings in the settings file - if !opts.HasSettingsYaml() { - // No settings file. - opts.Merge(kcl.WithKFilenames(opts.PkgPath())) - } else if opts.HasSettingsYaml() && len(opts.KFilenameList) == 0 { - // Has settings file but no file config in the settings files. - opts.Merge(kcl.WithKFilenames(opts.PkgPath())) - } - } - opts.Merge(kcl.WithWorkDir(opts.PkgPath())) - - // Calculate the absolute path of entry file described by '--input'. - compiler := runner.NewCompilerWithOpts(opts) - - // Call the kcl compiler. - compileResult, err := c.Compile(kclPkg, compiler) - - if err != nil { - return nil, reporter.NewErrorEvent(reporter.CompileFailed, err, "failed to compile the kcl package") - } - - return compileResult, nil -} - -// RunWithOpts will compile the kcl package with the compile options. -func (c *KpmClient) RunWithOpts(opts ...opt.Option) (*kcl.KCLResultList, error) { - mergedOpts := opt.DefaultCompileOptions() - for _, opt := range opts { - opt(mergedOpts) - } - return c.CompileWithOpts(mergedOpts) -} - -// CompilePkgWithOpts will compile the kcl package with the compile options. -func (c *KpmClient) CompilePkgWithOpts(kclPkg *pkg.KclPkg, opts *opt.CompileOptions) (*kcl.KCLResultList, error) { - opts.SetPkgPath(kclPkg.HomePath) - if len(opts.Entries()) > 0 { - // add entry from '--input' - for _, entry := range opts.Entries() { - if filepath.IsAbs(entry) { - opts.Merge(kcl.WithKFilenames(entry)) - } else { - opts.Merge(kcl.WithKFilenames(filepath.Join(opts.PkgPath(), entry))) - } - } - // add entry from 'kcl.mod' - } else if len(kclPkg.GetEntryKclFilesFromModFile()) > 0 { - opts.Merge(*kclPkg.GetKclOpts()) - } else if !opts.HasSettingsYaml() { - // no entry - opts.Merge(kcl.WithKFilenames(opts.PkgPath())) - } - opts.Merge(kcl.WithWorkDir(opts.PkgPath())) - // Calculate the absolute path of entry file described by '--input'. - compiler := runner.NewCompilerWithOpts(opts) - // Call the kcl compiler. - compileResult, err := c.Compile(kclPkg, compiler) - - if err != nil { - return nil, reporter.NewErrorEvent(reporter.CompileFailed, err, "failed to compile the kcl package") - } - - return compileResult, nil -} - -// CompileTarPkg will compile the kcl package from the tar package. -func (c *KpmClient) CompileTarPkg(tarPath string, opts *opt.CompileOptions) (*kcl.KCLResultList, error) { - absTarPath, err := utils.AbsTarPath(tarPath) - if err != nil { - return nil, err - } - // Extract the tar package to a directory with the same name. - // e.g. - // 'xxx/xxx/xxx/test.tar' will be extracted to the directory 'xxx/xxx/xxx/test'. - destDir := strings.TrimSuffix(absTarPath, filepath.Ext(absTarPath)) - err = utils.UnTarDir(absTarPath, destDir) - if err != nil { - return nil, err - } - - opts.SetPkgPath(destDir) - // The directory after extracting the tar package is taken as the root directory of the package, - // and kclvm is called to compile the kcl program under the 'destDir'. - // e.g. - // if the tar path is 'xxx/xxx/xxx/test.tar', - // the 'xxx/xxx/xxx/test' will be taken as the root path of the kcl package to compile. - return c.CompileWithOpts(opts) -} - -// CompileGitPkg will compile the kcl package from the git url. -func (c *KpmClient) CompileGitPkg(gitOpts *git.CloneOptions, compileOpts *opt.CompileOptions) (*kcl.KCLResultList, error) { - // 1. Create the temporary directory to pull the tar. - tmpDir, err := os.MkdirTemp("", "") - if err != nil { - return nil, reporter.NewErrorEvent(reporter.Bug, err, "internal bugs, please contact us to fix it.") - } - tmpDir = filepath.Join(tmpDir, constants.GitEntry) - - // clean the temp dir. - defer os.RemoveAll(tmpDir) - - // 2. clone the git repo - _, err = git.CloneWithOpts( - git.WithCommit(gitOpts.Commit), - git.WithBranch(gitOpts.Branch), - git.WithTag(gitOpts.Tag), - git.WithRepoURL(gitOpts.RepoURL), - git.WithLocalPath(tmpDir), - ) - if err != nil { - return nil, reporter.NewErrorEvent(reporter.FailedGetPkg, err, "failed to get the git repository") - } - - compileOpts.SetPkgPath(tmpDir) - - return c.CompileWithOpts(compileOpts) -} - -// CompileOciPkg will compile the kcl package from the OCI reference or url. -func (c *KpmClient) CompileOciPkg(ociSource, version string, opts *opt.CompileOptions) (*kcl.KCLResultList, error) { - ociOpts, err := c.ParseOciOptionFromString(ociSource, version) - - if err != nil { - return nil, err - } - - // 1. Create the temporary directory to pull the tar. - tmpDir, err := os.MkdirTemp("", "") - if err != nil { - return nil, reporter.NewErrorEvent(reporter.Bug, err, "internal bugs, please contact us to fix it.") - } - // clean the temp dir. - defer os.RemoveAll(tmpDir) - - localPath := ociOpts.SanitizePathWithSuffix(tmpDir) - - // 2. Pull the tar. - err = c.pullTarFromOci(localPath, ociOpts) - - if err != nil { - return nil, err - } - - // 3.Get the (*.tar) file path. - matches, err := filepath.Glob(filepath.Join(localPath, constants.KCL_PKG_TAR)) - if err != nil || len(matches) != 1 { - if err != nil { - return nil, reporter.NewErrorEvent(reporter.FailedGetPkg, err, "failed to pull kcl package") - } else { - return nil, errors.FailedPull - } - } - - return c.CompileTarPkg(matches[0], opts) -} - // createIfNotExist will create a file if it does not exist. func (c *KpmClient) createIfNotExist(filepath string, storeFunc func() error) error { reporter.ReportMsgTo(fmt.Sprintf("creating new :%s", filepath), c.GetLogWriter()) @@ -1434,86 +1240,6 @@ func (c *KpmClient) DownloadPkgFromOci(dep *downloader.Oci, localPath string) (* return pkg, nil } -// DownloadFromOci will download the dependency from the oci repository. -// Deprecated: Use the DownloadPkgFromOci instead. -func (c *KpmClient) DownloadFromOci(dep *downloader.Oci, localPath string) (string, error) { - ociClient, err := oci.NewOciClient(dep.Reg, dep.Repo, &c.settings) - if err != nil { - return "", err - } - ociClient.SetLogWriter(c.logWriter) - // Select the latest tag, if the tag, the user inputed, is empty. - var tagSelected string - if len(dep.Tag) == 0 { - tagSelected, err = ociClient.TheLatestTag() - if err != nil { - return "", err - } - - reporter.ReportMsgTo( - fmt.Sprintf("the lastest version '%s' will be added", tagSelected), - c.logWriter, - ) - - dep.Tag = tagSelected - localPath = localPath + dep.Tag - } else { - tagSelected = dep.Tag - } - - reporter.ReportMsgTo( - fmt.Sprintf("downloading '%s:%s' from '%s/%s:%s'", dep.Repo, tagSelected, dep.Reg, dep.Repo, tagSelected), - c.logWriter, - ) - - // Pull the package with the tag. - err = ociClient.Pull(localPath, tagSelected) - if err != nil { - return "", err - } - - matches, _ := filepath.Glob(filepath.Join(localPath, "*.tar")) - if matches == nil || len(matches) != 1 { - // then try to glob tgz file - matches, _ = filepath.Glob(filepath.Join(localPath, "*.tgz")) - if matches == nil || len(matches) != 1 { - return "", reporter.NewErrorEvent( - reporter.InvalidKclPkg, - err, - fmt.Sprintf("failed to find the kcl package from '%s'.", localPath), - ) - } - } - - tarPath := matches[0] - if utils.IsTar(tarPath) { - err = utils.UnTarDir(tarPath, localPath) - } else { - err = utils.ExtractTarball(tarPath, localPath) - } - if err != nil { - return "", reporter.NewErrorEvent( - reporter.FailedUntarKclPkg, - err, - fmt.Sprintf("failed to untar the kcl package from '%s' into '%s'.", tarPath, localPath), - ) - } - - // After untar the downloaded kcl package tar file, remove the tar file. - if utils.DirExists(tarPath) { - rmErr := os.Remove(tarPath) - if rmErr != nil { - return "", reporter.NewErrorEvent( - reporter.FailedUntarKclPkg, - err, - fmt.Sprintf("failed to untar the kcl package tar from '%s' into '%s'.", tarPath, localPath), - ) - } - } - - return localPath, nil -} - // PullFromOci will pull a kcl package from oci registry and unpack it. func (c *KpmClient) PullFromOci(localPath, source, tag string) error { localPath, err := filepath.Abs(localPath) diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index a60b5639..e8599599 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -24,11 +24,9 @@ import ( "github.com/otiai10/copy" "github.com/stretchr/testify/assert" "golang.org/x/mod/module" - "gopkg.in/yaml.v3" "kcl-lang.io/kcl-go/pkg/kcl" "kcl-lang.io/kpm/pkg/downloader" "kcl-lang.io/kpm/pkg/env" - "kcl-lang.io/kpm/pkg/git" "kcl-lang.io/kpm/pkg/opt" pkg "kcl-lang.io/kpm/pkg/package" "kcl-lang.io/kpm/pkg/reporter" @@ -1194,94 +1192,6 @@ func TestAddWithNoSumCheck(t *testing.T) { }() } -func TestRunWithGitPackage(t *testing.T) { - pkgPath := getTestDir("test_run_git_package") - - kpmcli, err := NewKpmClient() - assert.Equal(t, err, nil) - - opts := opt.DefaultCompileOptions() - opts.SetPkgPath(pkgPath) - - compileResult, err := kpmcli.CompileWithOpts(opts) - assert.Equal(t, err, nil) - expectedCompileResult := `{"apiVersion": "v1", "kind": "Pod", "metadata": {"name": "web-app"}, "spec": {"containers": [{"image": "nginx", "name": "main-container", "ports": [{"containerPort": 80}]}]}}` - assert.Equal(t, expectedCompileResult, compileResult.GetRawJsonResult()) - - assert.Equal(t, utils.DirExists(filepath.Join(pkgPath, "kcl.mod.lock")), true) - - defer func() { - _ = os.Remove(filepath.Join(pkgPath, "kcl.mod.lock")) - }() -} - -func TestRunWithNoSumCheck(t *testing.T) { - pkgPath := getTestDir("test_run_no_sum_check") - - kpmcli, err := NewKpmClient() - assert.Equal(t, err, nil) - - opts := opt.DefaultCompileOptions() - opts.SetNoSumCheck(true) - opts.SetPkgPath(pkgPath) - - _, err = kpmcli.CompileWithOpts(opts) - assert.Equal(t, err, nil) - assert.Equal(t, utils.DirExists(filepath.Join(pkgPath, "kcl.mod.lock")), false) - - opts = opt.DefaultCompileOptions() - opts.SetPkgPath(pkgPath) - opts.SetNoSumCheck(false) - _, err = kpmcli.CompileWithOpts(opts) - assert.Equal(t, err, nil) - assert.Equal(t, utils.DirExists(filepath.Join(pkgPath, "kcl.mod.lock")), true) - - defer func() { - _ = os.Remove(filepath.Join(pkgPath, "kcl.mod.lock")) - }() -} - -func TestRunGit(t *testing.T) { - kpmcli, err := NewKpmClient() - assert.Equal(t, err, nil) - - testPath := getTestDir("test_run_git") - - opts := opt.DefaultCompileOptions() - gitOpts := git.NewCloneOptions("https://github.com/KusionStack/catalog", "", "0.1.2", "", filepath.Join(testPath, "catalog"), nil) - defer func() { - _ = os.RemoveAll(filepath.Join(testPath, "catalog")) - }() - - testCases := []struct { - entryPath string - expectFile string - }{ - {"models/samples/hellocollaset/prod/main.k", "expect1.json"}, - {"models/samples/pgadmin/base/base.k", "expect2.json"}, - {"models/samples/wordpress/prod/main.k", "expect3.json"}, - } - - for _, tc := range testCases { - opts.SetEntries([]string{tc.entryPath}) - result, err := kpmcli.CompileGitPkg(gitOpts, opts) - assert.Equal(t, err, nil) - - fileBytes, err := os.ReadFile(filepath.Join(testPath, tc.expectFile)) - assert.Equal(t, err, nil) - - var expectObj map[string]interface{} - err = json.Unmarshal(fileBytes, &expectObj) - assert.Equal(t, err, nil) - - var gotObj map[string]interface{} - err = json.Unmarshal([]byte(result.GetRawJsonResult()), &gotObj) - assert.Equal(t, err, nil) - - assert.Equal(t, gotObj, expectObj) - } -} - func TestUpdateWithNoSumCheck(t *testing.T) { pkgPath := getTestDir("test_update_no_sum_check") kpmcli, err := NewKpmClient() @@ -1605,59 +1515,6 @@ func TestAddWithLocalPath(t *testing.T) { assert.Equal(t, gotpkg.Dependencies.Deps.GetOrDefault("dep_pkg", pkg.TestPkgDependency).Source.Local.Path, "../dep_pkg") } -func TestRunOciWithSettingsFile(t *testing.T) { - kpmcli, err := NewKpmClient() - assert.Equal(t, err, nil) - kpmcli.SetLogWriter(nil) - opts := opt.DefaultCompileOptions() - opts.SetEntries([]string{}) - opts.Merge(kcl.WithSettings(filepath.Join(".", "test_data", "test_run_oci_with_settings", "kcl.yaml"))) - opts.SetHasSettingsYaml(true) - _, err = kpmcli.CompileOciPkg("oci://ghcr.io/kcl-lang/helloworld", "", opts) - assert.Equal(t, err, nil) -} - -func TestRunGitWithLocalDep(t *testing.T) { - kpmcli, err := NewKpmClient() - assert.Equal(t, err, nil) - - testPath := getTestDir("test_run_git_with_local_dep") - defer func() { - _ = os.RemoveAll(filepath.Join(testPath, "catalog")) - }() - - testCases := []struct { - ref string - expectFile string - }{ - {"8308200", "expect1.yaml"}, - {"0b3f5ab", "expect2.yaml"}, - } - - for _, tc := range testCases { - - expectPath := filepath.Join(testPath, tc.expectFile) - opts := opt.DefaultCompileOptions() - gitOpts := git.NewCloneOptions("https://github.com/kcl-lang/flask-demo-kcl-manifests.git", tc.ref, "", "", "", nil) - - result, err := kpmcli.CompileGitPkg(gitOpts, opts) - assert.Equal(t, err, nil) - - fileBytes, err := os.ReadFile(expectPath) - assert.Equal(t, err, nil) - - var expectObj map[string]interface{} - err = yaml.Unmarshal(fileBytes, &expectObj) - assert.Equal(t, err, nil) - - var gotObj map[string]interface{} - err = yaml.Unmarshal([]byte(result.GetRawJsonResult()), &gotObj) - assert.Equal(t, err, nil) - - assert.Equal(t, gotObj, expectObj) - } -} - func TestLoadOciUrlDiffSetting(t *testing.T) { kpmcli, err := NewKpmClient() assert.Equal(t, err, nil) @@ -1716,27 +1573,6 @@ func testAddWithOciDownloader(t *testing.T) { assert.Equal(t, utils.RmNewline(string(expectmodContent)), utils.RmNewline(string(gotContent))) } -func testRunWithOciDownloader(t *testing.T) { - kpmCli, err := NewKpmClient() - path := getTestDir("test_oci_downloader") - assert.Equal(t, err, nil) - - kpmCli.DepDownloader = downloader.NewOciDownloader("linux/amd64") - - var buf bytes.Buffer - writer := io.MultiWriter(&buf, os.Stdout) - - res, err := kpmCli.RunWithOpts( - opt.WithEntries([]string{filepath.Join(path, "run_pkg", "pkg", "main.k")}), - opt.WithKclOption(kcl.WithWorkDir(filepath.Join(path, "run_pkg", "pkg"))), - opt.WithNoSumCheck(true), - opt.WithLogWriter(writer), - ) - assert.Equal(t, err, nil) - strings.Contains(buf.String(), "downloading 'zong-zhe/helloworld:0.0.3' from 'ghcr.io/zong-zhe/helloworld:0.0.3'") - assert.Equal(t, res.GetRawYamlResult(), "The_first_kcl_program: Hello World!") -} - func TestAddLocalPath(t *testing.T) { kpmCli, err := NewKpmClient() diff --git a/pkg/client/deperated.go b/pkg/client/deperated.go new file mode 100644 index 00000000..fa70c4d3 --- /dev/null +++ b/pkg/client/deperated.go @@ -0,0 +1,260 @@ +package client + +import ( + "fmt" + "net/url" + "os" + "path/filepath" + + "kcl-lang.io/kcl-go/pkg/kcl" + "kcl-lang.io/kpm/pkg/downloader" + "kcl-lang.io/kpm/pkg/git" + "kcl-lang.io/kpm/pkg/oci" + "kcl-lang.io/kpm/pkg/opt" + pkg "kcl-lang.io/kpm/pkg/package" + "kcl-lang.io/kpm/pkg/reporter" + "kcl-lang.io/kpm/pkg/utils" +) + +// CompileWithOpts will compile the kcl program with the compile options. +// Deprecated: Use `Run` instead. +func (c *KpmClient) CompileWithOpts(opts *opt.CompileOptions) (*kcl.KCLResultList, error) { + pkgPath, err := filepath.Abs(opts.PkgPath()) + if err != nil { + return nil, reporter.NewErrorEvent(reporter.Bug, err, "internal bugs, please contact us to fix it.") + } + + c.noSumCheck = opts.NoSumCheck() + c.logWriter = opts.LogWriter() + + runOpts := RunOptions{} + runOpts.Option = opts.Option + c.noSumCheck = opts.NoSumCheck() + c.logWriter = opts.LogWriter() + pathSource := downloader.Source{ + Local: &downloader.Local{ + Path: pkgPath, + }, + } + + pathSourceUrl, err := pathSource.ToString() + if err != nil { + return nil, err + } + + return c.Run( + WithRunOptions(&runOpts), + WithRunSourceUrls(append([]string{pathSourceUrl}, opts.Entries()...)), + WithVendor(opts.IsVendor()), + ) +} + +// RunWithOpts will compile the kcl package with the compile options. +// Deprecated: Use `Run` instead. +func (c *KpmClient) RunWithOpts(opts ...opt.Option) (*kcl.KCLResultList, error) { + mergedOpts := opt.DefaultCompileOptions() + for _, opt := range opts { + opt(mergedOpts) + } + return c.CompileWithOpts(mergedOpts) +} + +// CompilePkgWithOpts will compile the kcl package with the compile options. +// Deprecated: Use `Run` instead. +func (c *KpmClient) CompilePkgWithOpts(kclPkg *pkg.KclPkg, opts *opt.CompileOptions) (*kcl.KCLResultList, error) { + runOpts := RunOptions{} + runOpts.Option = opts.Option + c.noSumCheck = opts.NoSumCheck() + c.logWriter = opts.LogWriter() + pathSource := downloader.Source{ + Local: &downloader.Local{ + Path: kclPkg.HomePath, + }, + } + + pathSourceUrl, err := pathSource.ToString() + if err != nil { + return nil, err + } + + return c.Run( + WithRunOptions(&runOpts), + WithRunSourceUrls(append([]string{pathSourceUrl}, opts.Entries()...)), + WithVendor(opts.IsVendor()), + ) +} + +// CompileTarPkg will compile the kcl package from the tar package. +// Deprecated: Use `Run` instead. +func (c *KpmClient) CompileTarPkg(tarPath string, opts *opt.CompileOptions) (*kcl.KCLResultList, error) { + runOpts := RunOptions{} + runOpts.Option = opts.Option + c.noSumCheck = opts.NoSumCheck() + c.logWriter = opts.LogWriter() + pathSource := downloader.Source{ + Local: &downloader.Local{ + Path: tarPath, + }, + } + + pathSourceUrl, err := pathSource.ToString() + if err != nil { + return nil, err + } + + return c.Run( + WithRunOptions(&runOpts), + WithRunSourceUrls(append([]string{pathSourceUrl}, opts.Entries()...)), + WithVendor(opts.IsVendor()), + ) +} + +// CompileGitPkg will compile the kcl package from the git url. +// Deprecated: Use `Run` instead. +func (c *KpmClient) CompileGitPkg(gitOpts *git.CloneOptions, compileOpts *opt.CompileOptions) (*kcl.KCLResultList, error) { + runOpts := RunOptions{} + runOpts.Option = compileOpts.Option + c.noSumCheck = compileOpts.NoSumCheck() + c.logWriter = compileOpts.LogWriter() + gitSource := downloader.Source{ + Git: &downloader.Git{ + Url: gitOpts.RepoURL, + Commit: gitOpts.Commit, + Branch: gitOpts.Branch, + Tag: gitOpts.Tag, + }, + } + + gitSourceUrl, err := gitSource.ToString() + if err != nil { + return nil, err + } + + url, err := url.Parse(gitSourceUrl) + if err != nil { + return nil, err + } + + if url.Scheme != "ssh" && url.Scheme != "git" { + url.Scheme = "git" + } + + return c.Run( + WithRunOptions(&runOpts), + WithRunSourceUrls(append([]string{url.String()}, compileOpts.Entries()...)), + WithVendor(compileOpts.IsVendor()), + ) +} + +// CompileOciPkg will compile the kcl package from the OCI reference or url. +// Deprecated: Use `Run` instead. +func (c *KpmClient) CompileOciPkg(ociSource, version string, opts *opt.CompileOptions) (*kcl.KCLResultList, error) { + ociOpts, err := c.ParseOciOptionFromString(ociSource, version) + + if err != nil { + return nil, err + } + + runOpts := RunOptions{} + runOpts.Option = opts.Option + c.noSumCheck = opts.NoSumCheck() + c.logWriter = opts.LogWriter() + source := downloader.Source{ + Oci: &downloader.Oci{ + Reg: ociOpts.Reg, + Repo: ociOpts.Repo, + Tag: ociOpts.Tag, + }, + } + + ociSourceUrl, err := source.ToString() + if err != nil { + return nil, err + } + + return c.Run( + WithRunOptions(&runOpts), + WithRunSourceUrls(append([]string{ociSourceUrl}, opts.Entries()...)), + WithVendor(opts.IsVendor()), + ) +} + +// DownloadFromOci will download the dependency from the oci repository. +// Deprecated: Use the DownloadPkgFromOci instead. +func (c *KpmClient) DownloadFromOci(dep *downloader.Oci, localPath string) (string, error) { + ociClient, err := oci.NewOciClient(dep.Reg, dep.Repo, &c.settings) + if err != nil { + return "", err + } + ociClient.SetLogWriter(c.logWriter) + // Select the latest tag, if the tag, the user inputed, is empty. + var tagSelected string + if len(dep.Tag) == 0 { + tagSelected, err = ociClient.TheLatestTag() + if err != nil { + return "", err + } + + reporter.ReportMsgTo( + fmt.Sprintf("the lastest version '%s' will be added", tagSelected), + c.logWriter, + ) + + dep.Tag = tagSelected + localPath = localPath + dep.Tag + } else { + tagSelected = dep.Tag + } + + reporter.ReportMsgTo( + fmt.Sprintf("downloading '%s:%s' from '%s/%s:%s'", dep.Repo, tagSelected, dep.Reg, dep.Repo, tagSelected), + c.logWriter, + ) + + // Pull the package with the tag. + err = ociClient.Pull(localPath, tagSelected) + if err != nil { + return "", err + } + + matches, _ := filepath.Glob(filepath.Join(localPath, "*.tar")) + if matches == nil || len(matches) != 1 { + // then try to glob tgz file + matches, _ = filepath.Glob(filepath.Join(localPath, "*.tgz")) + if matches == nil || len(matches) != 1 { + return "", reporter.NewErrorEvent( + reporter.InvalidKclPkg, + err, + fmt.Sprintf("failed to find the kcl package from '%s'.", localPath), + ) + } + } + + tarPath := matches[0] + if utils.IsTar(tarPath) { + err = utils.UnTarDir(tarPath, localPath) + } else { + err = utils.ExtractTarball(tarPath, localPath) + } + if err != nil { + return "", reporter.NewErrorEvent( + reporter.FailedUntarKclPkg, + err, + fmt.Sprintf("failed to untar the kcl package from '%s' into '%s'.", tarPath, localPath), + ) + } + + // After untar the downloaded kcl package tar file, remove the tar file. + if utils.DirExists(tarPath) { + rmErr := os.Remove(tarPath) + if rmErr != nil { + return "", reporter.NewErrorEvent( + reporter.FailedUntarKclPkg, + err, + fmt.Sprintf("failed to untar the kcl package tar from '%s' into '%s'.", tarPath, localPath), + ) + } + } + + return localPath, nil +} diff --git a/pkg/client/deperated_test.go b/pkg/client/deperated_test.go new file mode 100644 index 00000000..5dfba777 --- /dev/null +++ b/pkg/client/deperated_test.go @@ -0,0 +1,162 @@ +package client + +import ( + "bytes" + "io" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "kcl-lang.io/kcl-go/pkg/kcl" + "kcl-lang.io/kpm/pkg/downloader" + "kcl-lang.io/kpm/pkg/git" + "kcl-lang.io/kpm/pkg/opt" + "kcl-lang.io/kpm/pkg/utils" +) + +func TestRunWithNoSumCheck(t *testing.T) { + pkgPath := getTestDir("test_run_no_sum_check") + + kpmcli, err := NewKpmClient() + assert.Equal(t, err, nil) + + opts := opt.DefaultCompileOptions() + opts.SetNoSumCheck(true) + opts.SetPkgPath(pkgPath) + + _, err = kpmcli.CompileWithOpts(opts) + assert.Equal(t, err, nil) + assert.Equal(t, utils.DirExists(filepath.Join(pkgPath, "kcl.mod.lock")), false) + + opts = opt.DefaultCompileOptions() + opts.SetPkgPath(pkgPath) + opts.SetNoSumCheck(false) + _, err = kpmcli.CompileWithOpts(opts) + assert.Equal(t, err, nil) + assert.Equal(t, utils.DirExists(filepath.Join(pkgPath, "kcl.mod.lock")), true) + + defer func() { + _ = os.Remove(filepath.Join(pkgPath, "kcl.mod.lock")) + }() +} + +func TestRunWithGitPackage(t *testing.T) { + pkgPath := getTestDir("test_run_git_package") + + kpmcli, err := NewKpmClient() + assert.Equal(t, err, nil) + + opts := opt.DefaultCompileOptions() + opts.SetPkgPath(pkgPath) + + compileResult, err := kpmcli.CompileWithOpts(opts) + assert.Equal(t, err, nil) + expectedCompileResult := `{"apiVersion": "v1", "kind": "Pod", "metadata": {"name": "web-app"}, "spec": {"containers": [{"image": "nginx", "name": "main-container", "ports": [{"containerPort": 80}]}]}}` + assert.Equal(t, expectedCompileResult, compileResult.GetRawJsonResult()) + + assert.Equal(t, utils.DirExists(filepath.Join(pkgPath, "kcl.mod.lock")), true) + + defer func() { + _ = os.Remove(filepath.Join(pkgPath, "kcl.mod.lock")) + }() +} + +func testRunWithOciDownloader(t *testing.T) { + kpmCli, err := NewKpmClient() + path := getTestDir("test_oci_downloader") + assert.Equal(t, err, nil) + + kpmCli.DepDownloader = downloader.NewOciDownloader("linux/amd64") + + var buf bytes.Buffer + writer := io.MultiWriter(&buf, os.Stdout) + + res, err := kpmCli.RunWithOpts( + opt.WithEntries([]string{filepath.Join(path, "run_pkg", "pkg", "main.k")}), + opt.WithKclOption(kcl.WithWorkDir(filepath.Join(path, "run_pkg", "pkg"))), + opt.WithNoSumCheck(true), + opt.WithLogWriter(writer), + ) + assert.Equal(t, err, nil) + strings.Contains(buf.String(), "downloading 'zong-zhe/helloworld:0.0.3' from 'ghcr.io/zong-zhe/helloworld:0.0.3'") + assert.Equal(t, res.GetRawYamlResult(), "The_first_kcl_program: Hello World!") +} + +func TestRunGit(t *testing.T) { + kpmcli, err := NewKpmClient() + assert.Equal(t, err, nil) + + testPath := getTestDir("test_run_git") + + opts := opt.DefaultCompileOptions() + gitOpts := git.NewCloneOptions("https://github.com/kcl-lang/flask-demo-kcl-manifests.git", "", "", "main", filepath.Join(testPath, "flask-demo-kcl-manifests"), nil) + defer func() { + _ = os.RemoveAll(filepath.Join(testPath, "flask-demo-kcl-manifests")) + }() + + result, err := kpmcli.CompileGitPkg(gitOpts, opts) + assert.Equal(t, err, nil) + + resultStr := result.GetRawYamlResult() + + expectedFilePath := filepath.Join(testPath, "expect.yaml") + bytes, err := os.ReadFile(expectedFilePath) + assert.Equal(t, err, nil) + assert.Equal(t, utils.RmNewline(string(bytes)), utils.RmNewline(string(resultStr))) +} + +func TestRunOciWithSettingsFile(t *testing.T) { + kpmcli, err := NewKpmClient() + assert.Equal(t, err, nil) + kpmcli.SetLogWriter(nil) + opts := opt.DefaultCompileOptions() + opts.SetEntries([]string{}) + opts.Merge(kcl.WithSettings(filepath.Join(".", "test_data", "test_run_oci_with_settings", "kcl.yaml"))) + opts.SetHasSettingsYaml(true) + _, err = kpmcli.CompileOciPkg("oci://ghcr.io/kcl-lang/helloworld", "", opts) + assert.Equal(t, err, nil) +} + +// TODO: failed because of https://github.com/kcl-lang/kcl/issues/1660, re-enable this test after the issue is fixed +// func TestRunGitWithLocalDep(t *testing.T) { +// kpmcli, err := NewKpmClient() +// assert.Equal(t, err, nil) + +// testPath := getTestDir("test_run_git_with_local_dep") +// defer func() { +// _ = os.RemoveAll(filepath.Join(testPath, "catalog")) +// }() + +// testCases := []struct { +// ref string +// expectFile string +// }{ +// {"8308200", "expect1.yaml"}, +// {"0b3f5ab", "expect2.yaml"}, +// } + +// for _, tc := range testCases { + +// expectPath := filepath.Join(testPath, tc.expectFile) +// opts := opt.DefaultCompileOptions() +// gitOpts := git.NewCloneOptions("https://github.com/kcl-lang/flask-demo-kcl-manifests.git", tc.ref, "", "", "", nil) + +// result, err := kpmcli.CompileGitPkg(gitOpts, opts) +// assert.Equal(t, err, nil) + +// fileBytes, err := os.ReadFile(expectPath) +// assert.Equal(t, err, nil) + +// var expectObj map[string]interface{} +// err = yaml.Unmarshal(fileBytes, &expectObj) +// assert.Equal(t, err, nil) + +// var gotObj map[string]interface{} +// err = yaml.Unmarshal([]byte(result.GetRawJsonResult()), &gotObj) +// assert.Equal(t, err, nil) + +// assert.Equal(t, gotObj, expectObj) +// } +// } diff --git a/pkg/client/run.go b/pkg/client/run.go index b7765331..e7352db8 100644 --- a/pkg/client/run.go +++ b/pkg/client/run.go @@ -89,6 +89,14 @@ type RunOptions struct { type RunOption func(*RunOptions) error +// Use the another RunOptions to override the current RunOptions. +func WithRunOptions(runOpts *RunOptions) RunOption { + return func(ro *RunOptions) error { + *ro = *runOpts + return nil + } +} + func WithLogger(l io.Writer) RunOption { return func(ro *RunOptions) error { if ro.Option == nil { @@ -383,6 +391,11 @@ func (o *RunOptions) applyCompileOptions(source downloader.Source, kclPkg *pkg.K if !pkgSource.IsLocalPath() { return false } + + if source.IsLocalTarPath() || source.IsLocalTgzPath() { + return true + } + sourcePath := pkgSource.Path if !filepath.IsAbs(sourcePath) && !utils.IsModRelativePath(sourcePath) { sourcePath = filepath.Join(workDir, sourcePath) @@ -499,7 +512,7 @@ func (o *RunOptions) getPkgSource() (*downloader.Source, error) { return nil, err } - if pkgSource.IsPackaged() || source.IsPackaged() { + if pkgSource.IsPackaged() && source.IsPackaged() { return nil, reporter.NewErrorEvent( reporter.CompileFailed, fmt.Errorf("cannot compile multiple packages %s at the same time", []string{rootSourceStr, sourceStr}), diff --git a/pkg/client/test_data/test_run_git/expect.yaml b/pkg/client/test_data/test_run_git/expect.yaml new file mode 100644 index 00000000..6b9a030d --- /dev/null +++ b/pkg/client/test_data/test_run_git/expect.yaml @@ -0,0 +1,39 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: flask-demo + labels: + app: flask-demo + namespace: default +spec: + replicas: 1 + selector: + matchLabels: + app: flask-demo + template: + metadata: + labels: + app: flask-demo + spec: + containers: + - name: flaskdemo + image: kcllang/flask_demo:8d31498e765ff67a2fa9933d4adffe067544b2fe + ports: + - protocol: TCP + containerPort: 5000 +--- +apiVersion: v1 +kind: Service +metadata: + name: flask-demo + labels: + app: flask-demo + namespace: default +spec: + type: NodePort + selector: + app: flask-demo + ports: + - port: 5000 + protocol: TCP + targetPort: 5000 diff --git a/pkg/client/test_data/test_run_git/expect1.json b/pkg/client/test_data/test_run_git/expect1.json deleted file mode 100644 index 3a15d55f..00000000 --- a/pkg/client/test_data/test_run_git/expect1.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "hellocollaset": { - "workload": { - "containers": { - "nginx": { - "image": "nginx:v2" - } - } - } - } -} \ No newline at end of file diff --git a/pkg/client/test_data/test_run_git/expect2.json b/pkg/client/test_data/test_run_git/expect2.json deleted file mode 100644 index e30f8590..00000000 --- a/pkg/client/test_data/test_run_git/expect2.json +++ /dev/null @@ -1,42 +0,0 @@ -{ - "pgadmin": { - "workload": { - "containers": { - "pgadmin": { - "image": "dpage/pgadmin4:latest", - "env": { - "PGADMIN_DEFAULT_EMAIL": "admin@admin.com", - "PGADMIN_DEFAULT_PASSWORD": "secret://pgadmin-secret/pgadmin-default-password", - "PGADMIN_PORT": "80" - }, - "resources": { - "cpu": "500m", - "memory": "512Mi" - } - } - }, - "secrets": { - "pgadmin-secret": { - "type": "opaque", - "data": { - "pgadmin-default-password": "*******" - } - } - }, - "replicas": 1, - "ports": [ - { - "port": 80, - "protocol": "TCP", - "public": false - } - ] - }, - "database": { - "pgadmin": { - "type": "cloud", - "version": "14.0" - } - } - } -} \ No newline at end of file diff --git a/pkg/client/test_data/test_run_git/expect3.json b/pkg/client/test_data/test_run_git/expect3.json deleted file mode 100644 index 6273fc2c..00000000 --- a/pkg/client/test_data/test_run_git/expect3.json +++ /dev/null @@ -1,35 +0,0 @@ -{ - "wordpress": { - "workload": { - "containers": { - "wordpress": { - "image": "wordpress:6.3", - "env": { - "WORDPRESS_DB_HOST": "$(KUSION_DB_HOST_WORDPRESS)", - "WORDPRESS_DB_USER": "$(KUSION_DB_USERNAME_WORDPRESS)", - "WORDPRESS_DB_PASSWORD": "$(KUSION_DB_PASSWORD_WORDPRESS)", - "WORDPRESS_DB_NAME": "mysql" - }, - "resources": { - "cpu": "500m", - "memory": "512Mi" - } - } - }, - "replicas": 1, - "ports": [ - { - "port": 80, - "protocol": "TCP", - "public": false - } - ] - }, - "database": { - "wordpress": { - "type": "cloud", - "version": "8.0" - } - } - } -} \ No newline at end of file diff --git a/test/e2e/test_suites/kpm/exec_inside_pkg/run_with_not_exist_input/test_suite.stderr b/test/e2e/test_suites/kpm/exec_inside_pkg/run_with_not_exist_input/test_suite.stderr index 9a07df13..b4023139 100644 --- a/test/e2e/test_suites/kpm/exec_inside_pkg/run_with_not_exist_input/test_suite.stderr +++ b/test/e2e/test_suites/kpm/exec_inside_pkg/run_with_not_exist_input/test_suite.stderr @@ -1,2 +1 @@ -failed to compile the kcl package -Cannot find the kcl file, please check the file path \ No newline at end of file +Cannot find the kcl file, please check the file path /test_kcl/no_exist \ No newline at end of file diff --git a/test/e2e/test_suites/kpm/exec_outside_pkg/run_oci_with_invalid_ref/test_suite.stdout b/test/e2e/test_suites/kpm/exec_outside_pkg/run_oci_with_invalid_ref/test_suite.stdout index 95c3d648..ebbe8187 100644 --- a/test/e2e/test_suites/kpm/exec_outside_pkg/run_oci_with_invalid_ref/test_suite.stdout +++ b/test/e2e/test_suites/kpm/exec_outside_pkg/run_oci_with_invalid_ref/test_suite.stdout @@ -1 +1 @@ -pulling 'test/invalid_oci_repo:invalid_tag' from 'localhost:5001/test/invalid_oci_repo' \ No newline at end of file +downloading 'test/invalid_oci_repo:invalid_tag' from 'localhost:5001/test/invalid_oci_repo:invalid_tag' \ No newline at end of file diff --git a/test/e2e/test_suites/kpm/exec_outside_pkg/run_tar_with_noargs_input/test_suite.env b/test/e2e/test_suites/kpm/exec_outside_pkg/run_tar_with_noargs_input/test_suite.env deleted file mode 100644 index 4c789529..00000000 --- a/test/e2e/test_suites/kpm/exec_outside_pkg/run_tar_with_noargs_input/test_suite.env +++ /dev/null @@ -1,2 +0,0 @@ -KPM_HOME="" -KCLVM_VENDOR_HOME="" \ No newline at end of file diff --git a/test/e2e/test_suites/kpm/exec_outside_pkg/run_tar_with_noargs_input/test_suite.input b/test/e2e/test_suites/kpm/exec_outside_pkg/run_tar_with_noargs_input/test_suite.input deleted file mode 100644 index 9c505d4e..00000000 --- a/test/e2e/test_suites/kpm/exec_outside_pkg/run_tar_with_noargs_input/test_suite.input +++ /dev/null @@ -1 +0,0 @@ -kpm run --input kcl1.tar \ No newline at end of file diff --git a/test/e2e/test_suites/kpm/exec_outside_pkg/run_tar_with_noargs_input/test_suite.stderr b/test/e2e/test_suites/kpm/exec_outside_pkg/run_tar_with_noargs_input/test_suite.stderr deleted file mode 100644 index b2b94e4c..00000000 --- a/test/e2e/test_suites/kpm/exec_outside_pkg/run_tar_with_noargs_input/test_suite.stderr +++ /dev/null @@ -1,2 +0,0 @@ -could not load 'kcl.mod' in '' -open /kcl.mod: no such file or directory \ No newline at end of file diff --git a/test/e2e/test_suites/kpm/exec_outside_pkg/run_tar_with_noargs_input/test_suite.stdout b/test/e2e/test_suites/kpm/exec_outside_pkg/run_tar_with_noargs_input/test_suite.stdout deleted file mode 100644 index e69de29b..00000000 diff --git a/test/e2e/test_suites/kpm/exec_outside_pkg/run_tar_with_not_exist_input/test_suite.env b/test/e2e/test_suites/kpm/exec_outside_pkg/run_tar_with_not_exist_input/test_suite.env deleted file mode 100644 index 4c789529..00000000 --- a/test/e2e/test_suites/kpm/exec_outside_pkg/run_tar_with_not_exist_input/test_suite.env +++ /dev/null @@ -1,2 +0,0 @@ -KPM_HOME="" -KCLVM_VENDOR_HOME="" \ No newline at end of file diff --git a/test/e2e/test_suites/kpm/exec_outside_pkg/run_tar_with_not_exist_input/test_suite.input b/test/e2e/test_suites/kpm/exec_outside_pkg/run_tar_with_not_exist_input/test_suite.input deleted file mode 100644 index 376b3e57..00000000 --- a/test/e2e/test_suites/kpm/exec_outside_pkg/run_tar_with_not_exist_input/test_suite.input +++ /dev/null @@ -1 +0,0 @@ -kpm run --input no_exist --vendor kcl1.tar \ No newline at end of file diff --git a/test/e2e/test_suites/kpm/exec_outside_pkg/run_tar_with_not_exist_input/test_suite.stderr b/test/e2e/test_suites/kpm/exec_outside_pkg/run_tar_with_not_exist_input/test_suite.stderr deleted file mode 100644 index 9a07df13..00000000 --- a/test/e2e/test_suites/kpm/exec_outside_pkg/run_tar_with_not_exist_input/test_suite.stderr +++ /dev/null @@ -1,2 +0,0 @@ -failed to compile the kcl package -Cannot find the kcl file, please check the file path \ No newline at end of file diff --git a/test/e2e/test_suites/kpm/exec_outside_pkg/run_tar_with_not_exist_input/test_suite.stdout b/test/e2e/test_suites/kpm/exec_outside_pkg/run_tar_with_not_exist_input/test_suite.stdout deleted file mode 100644 index e69de29b..00000000 diff --git a/test/e2e/test_suites/kpm/exec_outside_pkg/run_tar_with_not_exist_tar/test_suite.stderr b/test/e2e/test_suites/kpm/exec_outside_pkg/run_tar_with_not_exist_tar/test_suite.stderr index a512306d..81442217 100644 --- a/test/e2e/test_suites/kpm/exec_outside_pkg/run_tar_with_not_exist_tar/test_suite.stderr +++ b/test/e2e/test_suites/kpm/exec_outside_pkg/run_tar_with_not_exist_tar/test_suite.stderr @@ -1 +1,2 @@ -the kcl package tar path is not found +failed to open 'no_exist.tar' +open no_exist.tar: no such file or directory \ No newline at end of file diff --git a/test/e2e/test_suites/kpm/exec_outside_pkg/run_with_no_args/test_suite.env b/test/e2e/test_suites/kpm/exec_outside_pkg/run_with_no_args/test_suite.env deleted file mode 100644 index 4c789529..00000000 --- a/test/e2e/test_suites/kpm/exec_outside_pkg/run_with_no_args/test_suite.env +++ /dev/null @@ -1,2 +0,0 @@ -KPM_HOME="" -KCLVM_VENDOR_HOME="" \ No newline at end of file diff --git a/test/e2e/test_suites/kpm/exec_outside_pkg/run_with_no_args/test_suite.input b/test/e2e/test_suites/kpm/exec_outside_pkg/run_with_no_args/test_suite.input deleted file mode 100644 index b02e55a1..00000000 --- a/test/e2e/test_suites/kpm/exec_outside_pkg/run_with_no_args/test_suite.input +++ /dev/null @@ -1 +0,0 @@ -kpm run \ No newline at end of file diff --git a/test/e2e/test_suites/kpm/exec_outside_pkg/run_with_no_args/test_suite.stderr b/test/e2e/test_suites/kpm/exec_outside_pkg/run_with_no_args/test_suite.stderr deleted file mode 100644 index b2b94e4c..00000000 --- a/test/e2e/test_suites/kpm/exec_outside_pkg/run_with_no_args/test_suite.stderr +++ /dev/null @@ -1,2 +0,0 @@ -could not load 'kcl.mod' in '' -open /kcl.mod: no such file or directory \ No newline at end of file diff --git a/test/e2e/test_suites/kpm/exec_outside_pkg/run_with_no_args/test_suite.stdout b/test/e2e/test_suites/kpm/exec_outside_pkg/run_with_no_args/test_suite.stdout deleted file mode 100644 index e69de29b..00000000 diff --git a/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_duplication_deps/test_suite.stderr b/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_duplication_deps/test_suite.stderr index 9d39656e..1d45c3cf 100644 --- a/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_duplication_deps/test_suite.stderr +++ b/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_duplication_deps/test_suite.stderr @@ -1,4 +1,3 @@ -failed to compile the kcl package because '-' in the original dependency names is replaced with '_' please check your dependencies with '-' or '_' in dependency name dependency name conflict, 'dep_with_line' already exists \ No newline at end of file