-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #376 from zong-zhe/unify-pull-api
feat: add unified api for CLI pull command
- Loading branch information
Showing
20 changed files
with
1,129 additions
and
406 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package client | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"path/filepath" | ||
|
||
"kcl-lang.io/kpm/pkg/downloader" | ||
pkg "kcl-lang.io/kpm/pkg/package" | ||
"kcl-lang.io/kpm/pkg/reporter" | ||
) | ||
|
||
// The PullOptions struct contains the options for pulling a package from the registry. | ||
type PullOptions struct { | ||
// Source is the source of the package to be pulled. | ||
// Including git, oci, local. | ||
Source *downloader.Source | ||
// LocalPath is the local path to download the package. | ||
LocalPath string | ||
} | ||
|
||
type PullOption func(*PullOptions) error | ||
|
||
func WithPullSourceUrl(sourceUrl string) PullOption { | ||
return func(opts *PullOptions) error { | ||
source, err := downloader.NewSourceFromStr(sourceUrl) | ||
if err != nil { | ||
return err | ||
} | ||
opts.Source = source | ||
return nil | ||
} | ||
} | ||
|
||
func WithPullSource(source *downloader.Source) PullOption { | ||
return func(opts *PullOptions) error { | ||
if source == nil { | ||
return errors.New("source cannot be nil") | ||
} | ||
opts.Source = source | ||
return nil | ||
} | ||
} | ||
|
||
func WithLocalPath(path string) PullOption { | ||
return func(opts *PullOptions) error { | ||
opts.LocalPath = path | ||
return nil | ||
} | ||
} | ||
|
||
func NewPullOptions(opts ...PullOption) *PullOptions { | ||
do := &PullOptions{} | ||
for _, opt := range opts { | ||
opt(do) | ||
} | ||
return do | ||
} | ||
|
||
func (c *KpmClient) Pull(options ...PullOption) (*pkg.KclPkg, error) { | ||
opts := &PullOptions{} | ||
for _, option := range options { | ||
if err := option(opts); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
sourceFilePath, err := opts.Source.ToFilePath() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
sourceStr, err := opts.Source.ToString() | ||
if err != nil { | ||
return nil, err | ||
} | ||
reporter.ReportMsgTo( | ||
fmt.Sprintf("start to pull %s", sourceStr), | ||
c.GetLogWriter(), | ||
) | ||
|
||
kPkg, err := c.downloadPkg( | ||
// The package pulled will be stored in the 'opts.LocalPath/sourceFilePath' | ||
// 'opts.LocalPath' is the local path input by the user. | ||
// 'sourceFilePath' is generated by the source. | ||
// For example, | ||
// kcl package from 'https://github.com/kcl-lang/kcl' will be stored in '$LOCAL_PATH/git/github.com/kcl-lang/kcl' | ||
// kcl package from 'oci://ghcr.io/kcl-lang/kcl' will be stored in '$LOCAL_PATH/oci/ghcr.io/kcl-lang/kcl' | ||
downloader.WithLocalPath(filepath.Join(opts.LocalPath, sourceFilePath)), | ||
downloader.WithSource(*opts.Source), | ||
) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
reporter.ReportMsgTo( | ||
fmt.Sprintf("pulled %s %s successfully", kPkg.GetPkgName(), kPkg.GetPkgVersion()), | ||
c.GetLogWriter(), | ||
) | ||
return kPkg, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package client | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"gotest.tools/v3/assert" | ||
"kcl-lang.io/kpm/pkg/downloader" | ||
) | ||
|
||
func TestPull(t *testing.T) { | ||
pulledPath := getTestDir("test_pull") | ||
|
||
kpmcli, err := NewKpmClient() | ||
assert.NilError(t, err) | ||
|
||
var buf bytes.Buffer | ||
kpmcli.SetLogWriter(&buf) | ||
|
||
kPkg, err := kpmcli.Pull( | ||
WithLocalPath(pulledPath), | ||
WithPullSource(&downloader.Source{ | ||
Oci: &downloader.Oci{ | ||
Reg: "ghcr.io", | ||
Repo: "kcl-lang/helloworld", | ||
Tag: "0.0.1", | ||
}, | ||
}), | ||
) | ||
|
||
pkgPath := filepath.Join(pulledPath, "oci", "ghcr.io", "kcl-lang", "helloworld", "0.0.1") | ||
assert.NilError(t, err) | ||
assert.Equal(t, kPkg.GetPkgName(), "helloworld") | ||
assert.Equal(t, kPkg.GetPkgVersion(), "0.0.1") | ||
assert.Equal(t, kPkg.HomePath, pkgPath) | ||
|
||
kPkg, err = kpmcli.Pull( | ||
WithLocalPath(pulledPath), | ||
WithPullSourceUrl("oci://ghcr.io/kcl-lang/helloworld?tag=0.1.0"), | ||
) | ||
pkgPath = filepath.Join(pulledPath, "oci", "ghcr.io", "kcl-lang", "helloworld", "0.1.0") | ||
assert.NilError(t, err) | ||
assert.Equal(t, kPkg.GetPkgName(), "helloworld") | ||
assert.Equal(t, kPkg.GetPkgVersion(), "0.1.0") | ||
assert.Equal(t, kPkg.HomePath, pkgPath) | ||
|
||
defer func() { | ||
err = os.RemoveAll(filepath.Join(pulledPath, "oci")) | ||
assert.NilError(t, err) | ||
}() | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.