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

sparse checkout feature - Load the package after downloading the entire repository #452

Closed
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
13 changes: 13 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.FindPackage(dPath, c.subPackage)
}
kclvmCompiler.AddDepPath(dName, dPath)
}

Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions pkg/cmd/cmd_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
8 changes: 8 additions & 0 deletions pkg/cmd/cmd_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -103,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())
Expand Down
48 changes: 48 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"runtime"
"strings"

"github.com/BurntSushi/toml"
"github.com/distribution/reference"
"github.com/moby/term"
"github.com/otiai10/copy"
Expand Down Expand Up @@ -599,3 +600,50 @@ 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add more documents and unit tests?

var result string
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
kclModPath := filepath.Join(path, constants.KCL_MOD)
if _, err := os.Stat(kclModPath); err == nil {
if matchesPackageName(kclModPath, targetPackage) {
result = path
return filepath.SkipAll
}
}
}
return nil
})

if err != nil {
return "", err
}
if result == "" {
return "", fmt.Errorf("kcl.mod with package '%s' not found", targetPackage)
}
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"`
}
type ModFile struct {
Package Package `toml:"package"`
}

var modFile ModFile
_, err := toml.DecodeFile(kclModPath, &modFile)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use the loadpackage function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is it? Can you share the link of the code or the docs?

if err != nil {
fmt.Printf("Error parsing kcl.mod file: %v\n", err)
return false
}

return modFile.Package.Name == targetPackage
}
Loading