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 2 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.FindFolder(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
32 changes: 32 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Peefy marked this conversation as resolved.
Show resolved Hide resolved
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
}
Loading