From f681d0e5bfb67e37c1d20dea5d89134225e316ba Mon Sep 17 00:00:00 2001 From: zongz Date: Thu, 27 Jul 2023 14:39:20 +0800 Subject: [PATCH] feat: replace the default registry from 'ghcr.io/kusionstack' to 'ghcr.io/kcl-lang'. --- README-zh.md | 6 ++-- README.md | 6 ++-- docs/command-reference-zh/2.add.md | 4 +-- docs/command-reference/2.add.md | 4 +-- docs/kpm_oci-zh.md | 2 +- docs/kpm_oci.md | 2 +- makefile | 2 +- pkg/cmd/cmd_push.go | 2 +- pkg/cmd/cmd_push_test.go | 2 +- pkg/mod/modfile_test.go | 30 +++++++++---------- .../test_data/expected/kcl.mod.reverse.lock | 16 ++++++++++ .../test_data/expected/kcl.reverse.mod | 8 +++++ pkg/settings/settings.go | 25 +++++++++++++--- pkg/settings/settings_test.go | 19 ++++++++++-- pkg/settings/test_data/expected.json | 2 +- .../kpm_pull_with_pkg_name/test_suite.stdout | 6 ++-- .../test_suite.input | 2 +- .../test_suite.stdout | 6 ++-- 18 files changed, 100 insertions(+), 44 deletions(-) create mode 100644 pkg/package/test_data/expected/kcl.mod.reverse.lock create mode 100644 pkg/package/test_data/expected/kcl.reverse.mod diff --git a/README-zh.md b/README-zh.md index bbe260be..f90bdf98 100644 --- a/README-zh.md +++ b/README-zh.md @@ -100,10 +100,10 @@ version = "0.0.1" 然后,您可以通过 `kpm add` 命令来为您当前的库添加一个外部依赖。 -如下面的命令所示,为当前包添加一个版本号为 `1.27.2` 并且名为 `k8s` 的依赖包。 +如下面的命令所示,为当前包添加一个版本号为 `1.27` 并且名为 `k8s` 的依赖包。 ```shell -kpm add k8s:1.27.2 +kpm add k8s:1.27 ``` @@ -117,7 +117,7 @@ edition = "0.0.1" version = "0.0.1" [dependencies] -k8s = "1.27.2" # The dependency 'k8s' with version '1.27.2' +k8s = "1.27" # The dependency 'k8s' with version '1.27' ``` ### 编写一个程序使用包 `konfig` 中的内容 diff --git a/README.md b/README.md index 7cbc5905..0fd0c604 100644 --- a/README.md +++ b/README.md @@ -100,10 +100,10 @@ version = "0.0.1" You can then add a dependency to the current kcl package using the `kpm add` command -As shown below, taking the example of adding a package dependency named `k8s`, the version of the package is `1.27.2`. +As shown below, taking the example of adding a package dependency named `k8s`, the version of the package is `1.27`. ```shell -kpm add k8s:1.27.2 +kpm add k8s:1.27 ``` @@ -117,7 +117,7 @@ edition = "0.0.1" version = "0.0.1" [dependencies] -k8s = "1.27.2" # The dependency 'k8s' with version '1.27.2' +k8s = "1.27" # The dependency 'k8s' with version '1.27' ``` ### Write a kcl program that uses the content in `k8s` diff --git a/docs/command-reference-zh/2.add.md b/docs/command-reference-zh/2.add.md index 85edc93a..5f323d0f 100644 --- a/docs/command-reference-zh/2.add.md +++ b/docs/command-reference-zh/2.add.md @@ -38,10 +38,10 @@ kpm add [options][package_reference] kpm add k8s ``` -添加一个名为 `k8s` 的依赖,版本为 `1.27.2`。 +添加一个名为 `k8s` 的依赖,版本为 `1.27`。 ```shell -kpm add k8s:1.27.2 +kpm add k8s:1.27 ``` ### 添加一个来自 git 仓库的依赖 diff --git a/docs/command-reference/2.add.md b/docs/command-reference/2.add.md index 820f9709..24b7e16b 100644 --- a/docs/command-reference/2.add.md +++ b/docs/command-reference/2.add.md @@ -38,10 +38,10 @@ Add kcl dependency named `k8s` to the current package.The version of the depende kpm add k8s ``` -Add kcl dependency named 'k8s' with version '1.27.2'. +Add kcl dependency named 'k8s' with version '1.27'. ```shell -kpm add k8s:1.27.2 +kpm add k8s:1.27 ``` ### Add a dependency from git repository diff --git a/docs/kpm_oci-zh.md b/docs/kpm_oci-zh.md index 31f39c51..a59de9a3 100644 --- a/docs/kpm_oci-zh.md +++ b/docs/kpm_oci-zh.md @@ -15,7 +15,7 @@ kpm 默认 registry - [https://github.com/orgs/KusionStack/packages](https://git ```json { "DefaultOciRegistry":"ghcr.io", - "DefaultOciRepo":"kusionstack" + "DefaultOciRepo":"kcl-lang" } ``` diff --git a/docs/kpm_oci.md b/docs/kpm_oci.md index 6d649c33..828e8f87 100644 --- a/docs/kpm_oci.md +++ b/docs/kpm_oci.md @@ -15,7 +15,7 @@ The default content of the configuration file is as follows: ```json { "DefaultOciRegistry":"ghcr.io", - "DefaultOciRepo":"kusionstack" + "DefaultOciRepo":"kcl-lang" } ``` diff --git a/makefile b/makefile index 7004287a..6fba3083 100644 --- a/makefile +++ b/makefile @@ -7,7 +7,7 @@ build: COVER_FILE ?= coverage.out SOURCE_PATHS ?= ./pkg/... -test: ## Run the tests +unit_test: ## Run unit tests go test -gcflags=all=-l -timeout=20m `go list $(SOURCE_PATHS)` ${TEST_FLAGS} -v cover: ## Generates coverage report diff --git a/pkg/cmd/cmd_push.go b/pkg/cmd/cmd_push.go index e94b12e6..904598c7 100644 --- a/pkg/cmd/cmd_push.go +++ b/pkg/cmd/cmd_push.go @@ -150,7 +150,7 @@ func pushPackage(ociUrl string, kclPkg *pkg.KclPkg, settings *settings.Settings) // 3. Generate the OCI options from oci url and the version of current kcl package. ociOpts, err := opt.ParseOciOptionFromOciUrl(ociUrl, kclPkg.GetPkgTag()) - if err != nil { + if err != (*reporter.KpmEvent)(nil) { return reporter.NewErrorEvent( reporter.UnsupportOciUrlScheme, errors.InvalidOciUrl, diff --git a/pkg/cmd/cmd_push_test.go b/pkg/cmd/cmd_push_test.go index 66f7b964..25676e2e 100644 --- a/pkg/cmd/cmd_push_test.go +++ b/pkg/cmd/cmd_push_test.go @@ -15,5 +15,5 @@ func TestGenDefaultOciUrlForKclPkg(t *testing.T) { assert.Equal(t, err, nil) url, err := genDefaultOciUrlForKclPkg(kclPkg) assert.Equal(t, err, nil) - assert.Equal(t, url, "oci://ghcr.io/kusionstack/test_gen_oci_url") + assert.Equal(t, url, "oci://ghcr.io/kcl-lang/test_gen_oci_url") } diff --git a/pkg/mod/modfile_test.go b/pkg/mod/modfile_test.go index c89760e1..4619c112 100644 --- a/pkg/mod/modfile_test.go +++ b/pkg/mod/modfile_test.go @@ -176,7 +176,7 @@ func TestGetFilePath(t *testing.T) { // TestDownloadGit test download from oci registry. func TestDownloadOci(t *testing.T) { - testPath := filepath.Join(getTestDir("download"), "k8s_1.27.2") + testPath := filepath.Join(getTestDir("download"), "k8s_1.27") err := os.MkdirAll(testPath, 0755) assert.Equal(t, err, nil) @@ -184,30 +184,30 @@ func TestDownloadOci(t *testing.T) { depFromOci := Dependency{ Name: "k8s", - Version: "1.27.2", + Version: "1.27", Source: Source{ Oci: &Oci{ Reg: settings.DEFAULT_REGISTRY, Repo: urlpath, - Tag: "1.27.2", + Tag: "1.27", }, }, } dep, err := depFromOci.Download(testPath) + assert.Equal(t, err, nil) assert.Equal(t, dep.Name, "k8s") - assert.Equal(t, dep.FullName, "k8s_1.27.2") - assert.Equal(t, dep.Version, "1.27.2") - assert.Equal(t, dep.Sum, "ZI7L/uz53aDOIgVgxBbEPG7wGCWR+Gd3hhgYYRLoIY4=") + assert.Equal(t, dep.FullName, "k8s_1.27") + assert.Equal(t, dep.Version, "1.27") + assert.Equal(t, dep.Sum, "xnYM1FWHAy3m+KcQMQb2rjZouTxumqYt6FGZpu2T4yM=") assert.NotEqual(t, dep.Source.Oci, nil) assert.Equal(t, dep.Source.Oci.Reg, settings.DEFAULT_REGISTRY) assert.Equal(t, dep.Source.Oci.Repo, urlpath) - assert.Equal(t, dep.Source.Oci.Tag, "1.27.2") + assert.Equal(t, dep.Source.Oci.Tag, "1.27") assert.Equal(t, dep.LocalFullPath, testPath) - assert.Equal(t, err, nil) // Check whether the tar downloaded by `kpm add` has been deleted. - assert.Equal(t, utils.DirExists(filepath.Join(testPath, "k8s_1.27.2.tar")), false) + assert.Equal(t, utils.DirExists(filepath.Join(testPath, "k8s_1.27.tar")), false) err = os.RemoveAll(getTestDir("download")) assert.Equal(t, err, nil) @@ -235,18 +235,18 @@ func TestDownloadLatestOci(t *testing.T) { dep, err := depFromOci.Download(testPath) assert.Equal(t, dep.Name, "k8s") - assert.Equal(t, dep.FullName, "k8s_1.27.2") - assert.Equal(t, dep.Version, "1.27.2") - assert.Equal(t, dep.Sum, "ZI7L/uz53aDOIgVgxBbEPG7wGCWR+Gd3hhgYYRLoIY4=") + assert.Equal(t, dep.FullName, "k8s_1.27") + assert.Equal(t, dep.Version, "1.27") + assert.Equal(t, dep.Sum, "xnYM1FWHAy3m+KcQMQb2rjZouTxumqYt6FGZpu2T4yM=") assert.NotEqual(t, dep.Source.Oci, nil) assert.Equal(t, dep.Source.Oci.Reg, settings.DEFAULT_REGISTRY) assert.Equal(t, dep.Source.Oci.Repo, urlpath) - assert.Equal(t, dep.Source.Oci.Tag, "1.27.2") - assert.Equal(t, dep.LocalFullPath, testPath+"1.27.2") + assert.Equal(t, dep.Source.Oci.Tag, "1.27") + assert.Equal(t, dep.LocalFullPath, testPath+"1.27") assert.Equal(t, err, nil) // Check whether the tar downloaded by `kpm add` has been deleted. - assert.Equal(t, utils.DirExists(filepath.Join(testPath, "k8s_1.27.2.tar")), false) + assert.Equal(t, utils.DirExists(filepath.Join(testPath, "k8s_1.27.tar")), false) err = os.RemoveAll(getTestDir("download")) assert.Equal(t, err, nil) diff --git a/pkg/package/test_data/expected/kcl.mod.reverse.lock b/pkg/package/test_data/expected/kcl.mod.reverse.lock new file mode 100644 index 00000000..cebf047c --- /dev/null +++ b/pkg/package/test_data/expected/kcl.mod.reverse.lock @@ -0,0 +1,16 @@ +[dependencies] + [dependencies.test] + name = "name" + full_name = "test_version" + version = "test_version" + sum = "test_sum" + url = "test_url" + git_tag = "test_tag" + [dependencies.oci_test] + name = "oci_name" + full_name = "test_version" + version = "test_version" + sum = "test_sum" + reg = "test_reg" + repo = "test_repo" + oci_tag = "test_tag" diff --git a/pkg/package/test_data/expected/kcl.reverse.mod b/pkg/package/test_data/expected/kcl.reverse.mod new file mode 100644 index 00000000..23d46102 --- /dev/null +++ b/pkg/package/test_data/expected/kcl.reverse.mod @@ -0,0 +1,8 @@ +[package] +name = "test_add_deps" +edition = "0.0.1" +version = "0.0.1" + +[dependencies] +name = { git = "test_url", tag = "test_tag" } +oci_name = "test_tag" diff --git a/pkg/settings/settings.go b/pkg/settings/settings.go index d25e2fca..9657eeb6 100644 --- a/pkg/settings/settings.go +++ b/pkg/settings/settings.go @@ -31,7 +31,9 @@ type KpmConf struct { } const DEFAULT_REGISTRY = "ghcr.io" -const DEFAULT_REPO = "kusionstack" +const DEFAULT_REPO = "kcl-lang" +const DEFAULT_REGISTRY_ENV = "KPM_REG" +const DEFAULT_REPO_ENV = "KPM_REPO" // This is a singleton that loads kpm settings from 'kpm.json' // and is only initialized on the first call by 'Init()' or 'GetSettings()' @@ -116,16 +118,31 @@ func (settings *Settings) DefaultOciRegistry() string { return settings.Conf.DefaultOciRegistry } -// DefaultOciRepo return the default OCI repo 'kusionstack'. +// DefaultOciRepo return the default OCI repo 'kcl-lang'. func (settings *Settings) DefaultOciRepo() string { return settings.Conf.DefaultOciRepo } -// DefaultOciRef return the default OCI ref 'ghcr.io/kusionstack'. +// DefaultOciRef return the default OCI ref 'ghcr.io/kcl-lang'. func (settings *Settings) DefaultOciRef() string { return utils.JoinPath(settings.Conf.DefaultOciRegistry, settings.Conf.DefaultOciRepo) } +// LoadSettingsFromEnv will load the kpm settings from environment variables. +func (settings *Settings) LoadSettingsFromEnv() *Settings { + // Load the env KPM_REG + reg := os.Getenv(DEFAULT_REGISTRY_ENV) + if len(reg) > 0 { + settings.Conf.DefaultOciRegistry = reg + } + // Load the env KPM_REPO + repo := os.Getenv(DEFAULT_REPO_ENV) + if len(repo) > 0 { + settings.Conf.DefaultOciRepo = repo + } + return settings +} + // GetFullPath returns the full path file path under '$HOME/.kpm/config/' func GetFullPath(jsonFileName string) (string, error) { home, err := env.GetAbsPkgPath() @@ -211,7 +228,7 @@ func GetSettings() *Settings { kpm_settings.PackageCacheLock = flock.New(lockPath) }) - return kpm_settings + return kpm_settings.LoadSettingsFromEnv() } // loadOrCreateDefaultKpmJson will load the 'kpm.json' file from '$KCL_PKG_PATH/.kpm/config', diff --git a/pkg/settings/settings_test.go b/pkg/settings/settings_test.go index 4e325e83..b57c0872 100644 --- a/pkg/settings/settings_test.go +++ b/pkg/settings/settings_test.go @@ -47,7 +47,7 @@ func TestDefaultKpmConf(t *testing.T) { Conf: DefaultKpmConf(), } assert.Equal(t, settings.DefaultOciRegistry(), "ghcr.io") - assert.Equal(t, settings.DefaultOciRepo(), "kusionstack") + assert.Equal(t, settings.DefaultOciRepo(), "kcl-lang") } func TestLoadOrCreateDefaultKpmJson(t *testing.T) { @@ -60,7 +60,7 @@ func TestLoadOrCreateDefaultKpmJson(t *testing.T) { kpmConf, err := loadOrCreateDefaultKpmJson() assert.Equal(t, kpmConf.DefaultOciRegistry, "ghcr.io") - assert.Equal(t, kpmConf.DefaultOciRepo, "kusionstack") + assert.Equal(t, kpmConf.DefaultOciRepo, "kcl-lang") assert.Equal(t, err, nil) assert.Equal(t, utils.DirExists(kpmPath), true) @@ -155,3 +155,18 @@ func TestPackageCacheLock(t *testing.T) { (reflect.DeepEqual(gotlist, goroutine_1_first_list) || reflect.DeepEqual(gotlist, goroutine_2_first_list)), true) } + +func TestSettingEnv(t *testing.T) { + settings := GetSettings() + assert.Equal(t, settings.DefaultOciRegistry(), "ghcr.io") + assert.Equal(t, settings.DefaultOciRepo(), "kcl-lang") + + err := os.Setenv("KPM_REG", "test_reg") + assert.Equal(t, err, nil) + err = os.Setenv("KPM_REPO", "test_repo") + assert.Equal(t, err, nil) + + settings = GetSettings() + assert.Equal(t, settings.DefaultOciRegistry(), "test_reg") + assert.Equal(t, settings.DefaultOciRepo(), "test_repo") +} diff --git a/pkg/settings/test_data/expected.json b/pkg/settings/test_data/expected.json index bf1cfba1..c2aa9981 100644 --- a/pkg/settings/test_data/expected.json +++ b/pkg/settings/test_data/expected.json @@ -1 +1 @@ -{"DefaultOciRegistry":"ghcr.io","DefaultOciRepo":"kusionstack"} \ No newline at end of file +{"DefaultOciRegistry":"ghcr.io","DefaultOciRepo":"kcl-lang"} \ No newline at end of file diff --git a/test/e2e/test_suites/kpm/no_args/kpm_pull_with_pkg_name/test_suite.stdout b/test/e2e/test_suites/kpm/no_args/kpm_pull_with_pkg_name/test_suite.stdout index 13a373d8..a745a53e 100644 --- a/test/e2e/test_suites/kpm/no_args/kpm_pull_with_pkg_name/test_suite.stdout +++ b/test/e2e/test_suites/kpm/no_args/kpm_pull_with_pkg_name/test_suite.stdout @@ -1,4 +1,4 @@ kpm: start to pull 'k8s'. -kpm: the lastest version '1.27.2' will be pulled. -kpm: pulling 'kusionstack/k8s:1.27.2' from 'ghcr.io/kusionstack/k8s'. -kpm: pulled 'k8s' in '/ghcr.io/kusionstack/k8s' successfully. +kpm: the lastest version '1.27' will be pulled. +kpm: pulling 'kcl-lang/k8s:1.27' from 'ghcr.io/kcl-lang/k8s'. +kpm: pulled 'k8s' in '/ghcr.io/kcl-lang/k8s' successfully. diff --git a/test/e2e/test_suites/kpm/no_args/kpm_pull_with_pkg_name_tag/test_suite.input b/test/e2e/test_suites/kpm/no_args/kpm_pull_with_pkg_name_tag/test_suite.input index 36162d09..8f4edd70 100644 --- a/test/e2e/test_suites/kpm/no_args/kpm_pull_with_pkg_name_tag/test_suite.input +++ b/test/e2e/test_suites/kpm/no_args/kpm_pull_with_pkg_name_tag/test_suite.input @@ -1 +1 @@ -kpm pull k8s:1.27.2 \ No newline at end of file +kpm pull k8s:1.27 \ No newline at end of file diff --git a/test/e2e/test_suites/kpm/no_args/kpm_pull_with_pkg_name_tag/test_suite.stdout b/test/e2e/test_suites/kpm/no_args/kpm_pull_with_pkg_name_tag/test_suite.stdout index 9624b813..60b1447f 100644 --- a/test/e2e/test_suites/kpm/no_args/kpm_pull_with_pkg_name_tag/test_suite.stdout +++ b/test/e2e/test_suites/kpm/no_args/kpm_pull_with_pkg_name_tag/test_suite.stdout @@ -1,3 +1,3 @@ -kpm: start to pull 'k8s:1.27.2'. -kpm: pulling 'kusionstack/k8s:1.27.2' from 'ghcr.io/kusionstack/k8s'. -kpm: pulled 'k8s:1.27.2' in '/ghcr.io/kusionstack/k8s/1.27.2' successfully. +kpm: start to pull 'k8s:1.27'. +kpm: pulling 'kcl-lang/k8s:1.27' from 'ghcr.io/kcl-lang/k8s'. +kpm: pulled 'k8s:1.27' in '/ghcr.io/kcl-lang/k8s/1.27' successfully.