From 32ddefb5a265a6da41875ece20b700c72ff6318c Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Wed, 14 Aug 2024 19:46:47 +0530 Subject: [PATCH 1/4] sparse checkout feature Signed-off-by: Asish Kumar --- pkg/cmd/cmd_flags.go | 1 + pkg/cmd/cmd_run.go | 9 +++++++++ pkg/opt/opt.go | 5 +++++ pkg/runner/runner.go | 5 +++++ pkg/utils/utils.go | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 52 insertions(+) diff --git a/pkg/cmd/cmd_flags.go b/pkg/cmd/cmd_flags.go index 47ed65cb..45eef40e 100644 --- a/pkg/cmd/cmd_flags.go +++ b/pkg/cmd/cmd_flags.go @@ -17,6 +17,7 @@ const FLAG_DISABLE_NONE = "disable_none" const FLAG_ARGUMENT = "argument" const FLAG_OVERRIDES = "overrides" const FLAG_SORT_KEYS = "sort_keys" +const FLAG_PACKAGE = "package" const FLAG_QUIET = "quiet" const FLAG_NO_SUM_CHECK = "no_sum_check" diff --git a/pkg/cmd/cmd_run.go b/pkg/cmd/cmd_run.go index 4564e5d5..4b1a19b5 100644 --- a/pkg/cmd/cmd_run.go +++ b/pkg/cmd/cmd_run.go @@ -81,6 +81,12 @@ func NewRunCmd(kpmcli *client.KpmClient) *cli.Command { Aliases: []string{"k"}, Usage: "sort result keys", }, + + // KCL arg: --package + &cli.StringFlag{ + Name: FLAG_PACKAGE, + Usage: "specify the package name", + }, }, Action: func(c *cli.Context) error { return KpmRun(c, kpmcli) @@ -169,6 +175,9 @@ func CompileOptionFromCli(c *cli.Context) *opt.CompileOptions { // --vendor opts.SetVendor(c.Bool(FLAG_VENDOR)) + // --package + opts.SetPackage(c.String(FLAG_PACKAGE)) + // --setting, -Y settingsOpt := c.StringSlice(FLAG_SETTING) if len(settingsOpt) != 0 { diff --git a/pkg/opt/opt.go b/pkg/opt/opt.go index 9c863ca4..d636a377 100644 --- a/pkg/opt/opt.go +++ b/pkg/opt/opt.go @@ -127,6 +127,11 @@ func (opts *CompileOptions) SetVendor(isVendor bool) { opts.isVendor = isVendor } +// SetPackage will set the 'package' flag. +func (opts *CompileOptions) SetPackage(pkg string) { + opts.Package = pkg +} + // IsVendor will return the 'isVendor' flag. func (opts *CompileOptions) IsVendor() bool { return opts.isVendor diff --git a/pkg/runner/runner.go b/pkg/runner/runner.go index 6d0d42a4..0def4533 100644 --- a/pkg/runner/runner.go +++ b/pkg/runner/runner.go @@ -6,6 +6,7 @@ import ( "kcl-lang.io/kcl-go/pkg/kcl" "kcl-lang.io/kcl-go/scripts" "kcl-lang.io/kpm/pkg/opt" + "kcl-lang.io/kpm/pkg/utils" ) // The pattern of the external package argument. @@ -43,6 +44,10 @@ func (compiler *Compiler) AddKclOption(opt kcl.Option) *Compiler { // AddDep will add a file path to the dependency list. func (compiler *Compiler) AddDepPath(depName string, depPath string) *Compiler { + pkg := compiler.opts.Option.Package + if pkg != "" { + depPath, _ = utils.FindFolder(depPath, pkg) + } compiler.opts.Merge(kcl.WithExternalPkgs(fmt.Sprintf(EXTERNAL_PKGS_ARG_PATTERN, depName, depPath))) return compiler } diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 14bc9b06..37592739 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -599,3 +599,35 @@ func AbsTarPath(tarPath string) (string, error) { return absTarPath, nil } + +// FindFolder will find the folder 'targetFolder' under the 'root' directory recursively. +func FindFolder(root, targetFolder string) (string, error) { + var result string + + err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + if info.IsDir() && strings.EqualFold(info.Name(), targetFolder) { + absPath, err := filepath.Abs(path) + if err != nil { + return err + } + result = absPath + return filepath.SkipAll + } + + return nil + }) + + if err != nil { + return "", err + } + + if result == "" { + return "", fmt.Errorf("folder '%s' not found", targetFolder) + } + + return result, nil +} From 88ba1441b6852f29971ab1379cf9c2af418fef83 Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Fri, 16 Aug 2024 00:03:59 +0530 Subject: [PATCH 2/4] define subpackage internally Signed-off-by: Asish Kumar --- pkg/client/client.go | 13 +++++++++++++ pkg/cmd/cmd_run.go | 5 ++--- pkg/opt/opt.go | 5 ----- pkg/runner/runner.go | 5 ----- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/pkg/client/client.go b/pkg/client/client.go index ea3bd0fd..07b3b3f2 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -52,6 +52,8 @@ type KpmClient struct { settings settings.Settings // The flag of whether to check the checksum of the package and update kcl.mod.lock. noSumCheck bool + // The subpackage within a directory + subPackage string } // NewKpmClient will create a new kpm client with default settings. @@ -500,6 +502,9 @@ func (c *KpmClient) Compile(kclPkg *pkg.KclPkg, kclvmCompiler *runner.Compiler) if !filepath.IsAbs(dPath) { dPath = filepath.Join(c.homePath, dPath) } + if c.subPackage != "" { + dPath, _ = utils.FindFolder(dPath, c.subPackage) + } kclvmCompiler.AddDepPath(dName, dPath) } @@ -1877,6 +1882,14 @@ func (c *KpmClient) DownloadDeps(deps *pkg.Dependencies, lockDeps *pkg.Dependenc return &newDeps, nil } +func (c *KpmClient) SetSubPackage (pkgName string) { + c.subPackage = pkgName +} + +func (c *KpmClient) GetSubPackage () string { + return c.subPackage +} + // pullTarFromOci will pull a kcl package tar file from oci registry. func (c *KpmClient) pullTarFromOci(localPath string, ociOpts *opt.OciOptions) error { absPullPath, err := filepath.Abs(localPath) diff --git a/pkg/cmd/cmd_run.go b/pkg/cmd/cmd_run.go index 4b1a19b5..61cdee7e 100644 --- a/pkg/cmd/cmd_run.go +++ b/pkg/cmd/cmd_run.go @@ -109,6 +109,8 @@ func KpmRun(c *cli.Context, kpmcli *client.KpmClient) error { } }() + kpmcli.SetSubPackage(c.String(FLAG_PACKAGE)) + kclOpts := CompileOptionFromCli(c) kclOpts.SetNoSumCheck(c.Bool(FLAG_NO_SUM_CHECK)) runEntry, errEvent := runner.FindRunEntryFrom(c.Args().Slice()) @@ -175,9 +177,6 @@ func CompileOptionFromCli(c *cli.Context) *opt.CompileOptions { // --vendor opts.SetVendor(c.Bool(FLAG_VENDOR)) - // --package - opts.SetPackage(c.String(FLAG_PACKAGE)) - // --setting, -Y settingsOpt := c.StringSlice(FLAG_SETTING) if len(settingsOpt) != 0 { diff --git a/pkg/opt/opt.go b/pkg/opt/opt.go index d636a377..9c863ca4 100644 --- a/pkg/opt/opt.go +++ b/pkg/opt/opt.go @@ -127,11 +127,6 @@ func (opts *CompileOptions) SetVendor(isVendor bool) { opts.isVendor = isVendor } -// SetPackage will set the 'package' flag. -func (opts *CompileOptions) SetPackage(pkg string) { - opts.Package = pkg -} - // IsVendor will return the 'isVendor' flag. func (opts *CompileOptions) IsVendor() bool { return opts.isVendor diff --git a/pkg/runner/runner.go b/pkg/runner/runner.go index 0def4533..6d0d42a4 100644 --- a/pkg/runner/runner.go +++ b/pkg/runner/runner.go @@ -6,7 +6,6 @@ import ( "kcl-lang.io/kcl-go/pkg/kcl" "kcl-lang.io/kcl-go/scripts" "kcl-lang.io/kpm/pkg/opt" - "kcl-lang.io/kpm/pkg/utils" ) // The pattern of the external package argument. @@ -44,10 +43,6 @@ func (compiler *Compiler) AddKclOption(opt kcl.Option) *Compiler { // AddDep will add a file path to the dependency list. func (compiler *Compiler) AddDepPath(depName string, depPath string) *Compiler { - pkg := compiler.opts.Option.Package - if pkg != "" { - depPath, _ = utils.FindFolder(depPath, pkg) - } compiler.opts.Merge(kcl.WithExternalPkgs(fmt.Sprintf(EXTERNAL_PKGS_ARG_PATTERN, depName, depPath))) return compiler } From aaa4610ede307761b86e05bb1881c8de877a55c1 Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Fri, 16 Aug 2024 13:30:23 +0530 Subject: [PATCH 3/4] search for package name inside kcl.mod file Signed-off-by: Asish Kumar --- pkg/client/client.go | 6 +++--- pkg/utils/utils.go | 42 ++++++++++++++++++++++++++++-------------- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/pkg/client/client.go b/pkg/client/client.go index 07b3b3f2..d980aac3 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -503,7 +503,7 @@ func (c *KpmClient) Compile(kclPkg *pkg.KclPkg, kclvmCompiler *runner.Compiler) dPath = filepath.Join(c.homePath, dPath) } if c.subPackage != "" { - dPath, _ = utils.FindFolder(dPath, c.subPackage) + dPath, _ = utils.FindPackage(dPath, c.subPackage) } kclvmCompiler.AddDepPath(dName, dPath) } @@ -1882,11 +1882,11 @@ func (c *KpmClient) DownloadDeps(deps *pkg.Dependencies, lockDeps *pkg.Dependenc return &newDeps, nil } -func (c *KpmClient) SetSubPackage (pkgName string) { +func (c *KpmClient) SetSubPackage(pkgName string) { c.subPackage = pkgName } -func (c *KpmClient) GetSubPackage () string { +func (c *KpmClient) GetSubPackage() string { return c.subPackage } diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 37592739..6f82589e 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -17,6 +17,7 @@ import ( "runtime" "strings" + "github.com/BurntSushi/toml" "github.com/distribution/reference" "github.com/moby/term" "github.com/otiai10/copy" @@ -600,34 +601,47 @@ func AbsTarPath(tarPath string) (string, error) { return absTarPath, nil } -// FindFolder will find the folder 'targetFolder' under the 'root' directory recursively. -func FindFolder(root, targetFolder string) (string, error) { +func FindPackage(root, targetPackage string) (string, error) { var result string - err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error { if err != nil { return err } - - if info.IsDir() && strings.EqualFold(info.Name(), targetFolder) { - absPath, err := filepath.Abs(path) - if err != nil { - return err + if info.IsDir() { + kclModPath := filepath.Join(path, "kcl.mod") + if _, err := os.Stat(kclModPath); err == nil { + if matchesPackageName(kclModPath, targetPackage) { + result = path + return filepath.SkipAll + } } - result = absPath - return filepath.SkipAll } - return nil }) if err != nil { return "", err } - if result == "" { - return "", fmt.Errorf("folder '%s' not found", targetFolder) + return "", fmt.Errorf("kcl.mod with package '%s' not found", targetPackage) } - return result, nil } + +func matchesPackageName(kclModPath, targetPackage string) bool { + type Package struct { + Name string `toml:"name"` + } + type ModFile struct { + Package Package `toml:"package"` + } + + var modFile ModFile + _, err := toml.DecodeFile(kclModPath, &modFile) + if err != nil { + fmt.Printf("Error parsing kcl.mod file: %v\n", err) + return false + } + + return modFile.Package.Name == targetPackage +} From f4bb91a24aa75b5c2cca086a161fd300a6200f04 Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Sat, 17 Aug 2024 21:15:56 +0530 Subject: [PATCH 4/4] add comments on functions and use constant variables Signed-off-by: Asish Kumar --- pkg/utils/utils.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 6f82589e..6c7cdff2 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -601,6 +601,7 @@ func AbsTarPath(tarPath string) (string, error) { return absTarPath, nil } +// FindPackage finds the package with the package name 'targetPackage' under the 'root' directory kcl.mod file. func FindPackage(root, targetPackage string) (string, error) { var result string err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error { @@ -608,7 +609,7 @@ func FindPackage(root, targetPackage string) (string, error) { return err } if info.IsDir() { - kclModPath := filepath.Join(path, "kcl.mod") + kclModPath := filepath.Join(path, constants.KCL_MOD) if _, err := os.Stat(kclModPath); err == nil { if matchesPackageName(kclModPath, targetPackage) { result = path @@ -628,6 +629,7 @@ func FindPackage(root, targetPackage string) (string, error) { return result, nil } +// MatchesPackageName checks whether the package name in the kcl.mod file under 'kclModPath' is equal to 'targetPackage'. func matchesPackageName(kclModPath, targetPackage string) bool { type Package struct { Name string `toml:"name"`