Skip to content

Commit

Permalink
refactor: add struct 'KpmClient' to manage the settings of kpm
Browse files Browse the repository at this point in the history
  • Loading branch information
zong-zhe committed Sep 25, 2023
1 parent d8ea659 commit e8cb738
Show file tree
Hide file tree
Showing 63 changed files with 2,032 additions and 1,601 deletions.
28 changes: 14 additions & 14 deletions kpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,38 @@ import (
"os"

"github.com/urfave/cli/v2"
"kcl-lang.io/kpm/pkg/client"
"kcl-lang.io/kpm/pkg/cmd"
"kcl-lang.io/kpm/pkg/reporter"
"kcl-lang.io/kpm/pkg/settings"
"kcl-lang.io/kpm/pkg/version"
)

func main() {
reporter.InitReporter()
setting := settings.GetSettings()
if setting.ErrorEvent != nil {
reporter.Fatal(setting.ErrorEvent)
kpmcli, err := client.NewKpmClient()
if err != nil {
reporter.Fatal(err)
}
app := cli.NewApp()
app.Name = "kpm"
app.Usage = "kpm is a kcl package manager"
app.Version = version.GetVersionInStr()
app.UsageText = "kpm <command> [arguments]..."
app.Commands = []*cli.Command{
cmd.NewInitCmd(),
cmd.NewAddCmd(),
cmd.NewPkgCmd(),
cmd.NewMetadataCmd(),
cmd.NewInitCmd(kpmcli),
cmd.NewAddCmd(kpmcli),
cmd.NewPkgCmd(kpmcli),
cmd.NewMetadataCmd(kpmcli),

// todo: The following commands are bound to the oci registry.
// Refactor them to compatible with the other registry.
cmd.NewRunCmd(),
cmd.NewLoginCmd(setting),
cmd.NewLogoutCmd(setting),
cmd.NewPushCmd(setting),
cmd.NewPullCmd(),
cmd.NewRunCmd(kpmcli),
cmd.NewLoginCmd(kpmcli),
cmd.NewLogoutCmd(kpmcli),
cmd.NewPushCmd(kpmcli),
cmd.NewPullCmd(kpmcli),
}
err := app.Run(os.Args)
err = app.Run(os.Args)
if err != nil {
reporter.Fatal(err)
}
Expand Down
File renamed without changes.
7 changes: 6 additions & 1 deletion pkg/api/kpm_pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"kcl-lang.io/kcl-go/pkg/kcl"
"kcl-lang.io/kcl-go/pkg/spec/gpyrpc"
"kcl-lang.io/kpm/pkg/client"
"kcl-lang.io/kpm/pkg/errors"
pkg "kcl-lang.io/kpm/pkg/package"
)
Expand Down Expand Up @@ -57,7 +58,11 @@ func GetKclPackage(pkgPath string) (*KclPackage, error) {
//
// 'pkg_path' is the path of dependencies download by kpm.
func (pkg *KclPackage) UpdateDependencyInPath(pkg_path string) error {
return pkg.pkg.ResolveDepsMetadata(pkg_path, true)
kpmcli, err := client.NewKpmClient()
if err != nil {
return err
}
return kpmcli.ResolvePkgDepsMetadata(pkg.pkg, true)
}

// GetPkgName returns the name of the package.
Expand Down
31 changes: 14 additions & 17 deletions pkg/api/kpm_pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import (
"testing"

"gotest.tools/v3/assert"
"kcl-lang.io/kpm/pkg/client"
)

func TestPackageApi(t *testing.T) {
pkg_path := filepath.Join(getTestDir("test_kpm_package"), "kcl_pkg")
kcl_pkg_path, err := GetKclPkgPath()

assert.Equal(t, err, nil)
kpmcli, err := client.NewKpmClient()
assert.Equal(t, err, nil)
pkg, err := GetKclPackage(pkg_path)
assert.Equal(t, err, nil)
err = pkg.pkg.ResolveDepsMetadata(kcl_pkg_path, true)
assert.Equal(t, err, nil)

err = kpmcli.ResolvePkgDepsMetadata(pkg.pkg, true)
assert.Equal(t, err, nil)
assert.Equal(t, pkg.GetPkgName(), "kcl_pkg")
assert.Equal(t, pkg.GetVersion(), "0.0.1")
Expand Down Expand Up @@ -60,12 +60,12 @@ func TestPackageApi(t *testing.T) {

func TestGetAllSchemaTypesMappingNamed(t *testing.T) {
pkg_path := filepath.Join(getTestDir("test_kpm_package"), "kcl_pkg")
kcl_pkg_path, err := GetKclPkgPath()

assert.Equal(t, err, nil)
pkg, err := GetKclPackage(pkg_path)
assert.Equal(t, err, nil)
err = pkg.pkg.ResolveDepsMetadata(kcl_pkg_path, true)
kpmcli, err := client.NewKpmClient()
assert.Equal(t, err, nil)

err = kpmcli.ResolvePkgDepsMetadata(pkg.pkg, true)
assert.Equal(t, err, nil)

schemas, err := pkg.GetSchemaTypeMappingNamed("SchemaWithSameName")
Expand All @@ -84,14 +84,12 @@ func TestGetAllSchemaTypesMappingNamed(t *testing.T) {
}

func TestGetSchemaTypeMappingWithFilters(t *testing.T) {

pkg_path := filepath.Join(getTestDir("test_kpm_package"), "kcl_pkg")
kcl_pkg_path, err := GetKclPkgPath()

assert.Equal(t, err, nil)
pkg, err := GetKclPackage(pkg_path)
assert.Equal(t, err, nil)
err = pkg.pkg.ResolveDepsMetadata(kcl_pkg_path, true)
kpmcli, err := client.NewKpmClient()
assert.Equal(t, err, nil)
err = kpmcli.ResolvePkgDepsMetadata(pkg.pkg, true)
assert.Equal(t, err, nil)

filterFunc := func(kt *KclType) bool {
Expand Down Expand Up @@ -138,12 +136,11 @@ func TestGetSchemaTypeMappingWithFilters(t *testing.T) {

func TestGetSchemaTypeUnderEmptyDir(t *testing.T) {
pkg_path := filepath.Join(getTestDir("test_kpm_package"), "no_kcl_files")
kcl_pkg_path, err := GetKclPkgPath()

assert.Equal(t, err, nil)
pkg, err := GetKclPackage(pkg_path)
assert.Equal(t, err, nil)
err = pkg.pkg.ResolveDepsMetadata(kcl_pkg_path, true)
kpmcli, err := client.NewKpmClient()
assert.Equal(t, err, nil)
err = kpmcli.ResolvePkgDepsMetadata(pkg.pkg, true)
assert.Equal(t, err, nil)
schemas, err := pkg.GetSchemaTypeMappingNamed("SchemaInMain")
assert.Equal(t, err, nil)
Expand Down
44 changes: 17 additions & 27 deletions pkg/api/kpm_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strings"

"kcl-lang.io/kcl-go/pkg/kcl"
"kcl-lang.io/kpm/pkg/client"
"kcl-lang.io/kpm/pkg/constants"
"kcl-lang.io/kpm/pkg/env"
"kcl-lang.io/kpm/pkg/errors"
"kcl-lang.io/kpm/pkg/oci"
Expand All @@ -32,8 +34,6 @@ func RunTar(tarPath string, opts *opt.CompileOptions) (string, error) {
return compileResult.GetRawYamlResult(), nil
}

const KCL_PKG_TAR = "*.tar"

// RunOci will compile the kcl package from an OCI reference.
func RunOci(ociRef, version string, opts *opt.CompileOptions) (string, error) {
compileResult, compileErr := RunOciPkg(ociRef, version, opts)
Expand Down Expand Up @@ -85,23 +85,6 @@ func RunWithOpt(opts *opt.CompileOptions) (*kcl.KCLResultList, error) {
return kcl.RunWithOpts(*opts.Option)
}

// absTarPath checks whether path 'tarPath' exists and whether path 'tarPath' ends with '.tar'
// And after checking, absTarPath return the abs path for 'tarPath'.
func absTarPath(tarPath string) (string, error) {
absTarPath, err := filepath.Abs(tarPath)
if err != nil {
return "", errors.InternalBug
}

if filepath.Ext(absTarPath) != ".tar" {
return "", errors.InvalidKclPacakgeTar
} else if !utils.DirExists(absTarPath) {
return "", errors.KclPacakgeTarNotFound
}

return absTarPath, nil
}

// getAbsInputPath will return the abs path of the file path described by '--input'.
// If the path exists after 'inputPath' is computed as a full path, it will be returned.
// If not, the kpm checks whether the full path of 'pkgPath/inputPath' exists,
Expand Down Expand Up @@ -165,11 +148,13 @@ func RunPkgWithOpt(opts *opt.CompileOptions) (*kcl.KCLResultList, error) {
// Calculate the absolute path of entry file described by '--input'.
compiler := runner.NewCompilerWithOpts(opts)

kpmcli, err := client.NewKpmClient()
if err != nil {
return nil, err
}

// Call the kcl compiler.
compileResult, err := kclPkg.Compile(
globalPkgPath,
compiler,
)
compileResult, err := kpmcli.Compile(kclPkg, compiler)

if err != nil {
return nil, reporter.NewErrorEvent(reporter.CompileFailed, err, "failed to compile the kcl package")
Expand All @@ -192,7 +177,7 @@ func RunCurrentPkg(opts *opt.CompileOptions) (*kcl.KCLResultList, error) {

// RunTarPkg will compile the kcl package from a kcl package tar.
func RunTarPkg(tarPath string, opts *opt.CompileOptions) (*kcl.KCLResultList, error) {
absTarPath, err := absTarPath(tarPath)
absTarPath, err := utils.AbsTarPath(tarPath)
if err != nil {
return nil, err
}
Expand All @@ -216,7 +201,12 @@ func RunTarPkg(tarPath string, opts *opt.CompileOptions) (*kcl.KCLResultList, er

// RunOciPkg will compile the kcl package from an OCI reference.
func RunOciPkg(ociRef, version string, opts *opt.CompileOptions) (*kcl.KCLResultList, error) {
ociOpts, err := opt.ParseOciOptionFromString(ociRef, version)
kpmcli, err := client.NewKpmClient()
if err != nil {
return nil, err
}

ociOpts, err := kpmcli.ParseOciOptionFromString(ociRef, version)

if err != nil {
return nil, err
Expand All @@ -233,14 +223,14 @@ func RunOciPkg(ociRef, version string, opts *opt.CompileOptions) (*kcl.KCLResult
localPath := ociOpts.AddStoragePathSuffix(tmpDir)

// 2. Pull the tar.
err = oci.Pull(localPath, ociOpts.Reg, ociOpts.Repo, ociOpts.Tag)
err = oci.Pull(localPath, ociOpts.Reg, ociOpts.Repo, ociOpts.Tag, kpmcli.GetSettings())

if err != (*reporter.KpmEvent)(nil) {
return nil, err
}

// 3.Get the (*.tar) file path.
matches, err := filepath.Glob(filepath.Join(localPath, KCL_PKG_TAR))
matches, err := filepath.Glob(filepath.Join(localPath, constants.KCL_PKG_TAR))
if err != nil || len(matches) != 1 {
return nil, errors.FailedPull
}
Expand Down
17 changes: 0 additions & 17 deletions pkg/api/kpm_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,6 @@ func TestGetAbsInputPath(t *testing.T) {
assert.Equal(t, path, "")
}

func TestAbsTarPath(t *testing.T) {
pkgPath := getTestDir("test_check_tar_path")
expectAbsTarPath, _ := filepath.Abs(filepath.Join(pkgPath, "test.tar"))

abs, err := absTarPath(filepath.Join(pkgPath, "test.tar"))
assert.Equal(t, err, nil)
assert.Equal(t, abs, expectAbsTarPath)

abs, err = absTarPath(filepath.Join(pkgPath, "no_exist.tar"))
assert.NotEqual(t, err, nil)
assert.Equal(t, abs, "")

abs, err = absTarPath(filepath.Join(pkgPath, "invalid_tar"))
assert.NotEqual(t, err, nil)
assert.Equal(t, abs, "")
}

func TestRunPkgInPath(t *testing.T) {
pkgPath := getTestDir("test_run_pkg_in_path")
opts := opt.DefaultCompileOptions()
Expand Down
Loading

0 comments on commit e8cb738

Please sign in to comment.