Skip to content

Commit

Permalink
Merge pull request #137 from zong-zhe/update-default-reg
Browse files Browse the repository at this point in the history
feat: replace the default registry from 'ghcr.io/kusionstack' to 'ghcr.io/kcl-lang'.
  • Loading branch information
Peefy authored Jul 27, 2023
2 parents 9a4340c + f681d0e commit ba52f1b
Show file tree
Hide file tree
Showing 18 changed files with 100 additions and 44 deletions.
6 changes: 3 additions & 3 deletions README-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

<img src="./docs/gifs/kpm_add_k8s.gif" width="600" align="center" />
Expand All @@ -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` 中的内容
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

<img src="./docs/gifs/kpm_add_k8s.gif" width="600" align="center" />
Expand All @@ -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`
Expand Down
4 changes: 2 additions & 2 deletions docs/command-reference-zh/2.add.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 仓库的依赖
Expand Down
4 changes: 2 additions & 2 deletions docs/command-reference/2.add.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/kpm_oci-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ kpm 默认 registry - [https://github.com/orgs/KusionStack/packages](https://git
```json
{
"DefaultOciRegistry":"ghcr.io",
"DefaultOciRepo":"kusionstack"
"DefaultOciRepo":"kcl-lang"
}
```

Expand Down
2 changes: 1 addition & 1 deletion docs/kpm_oci.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ The default content of the configuration file is as follows:
```json
{
"DefaultOciRegistry":"ghcr.io",
"DefaultOciRepo":"kusionstack"
"DefaultOciRepo":"kcl-lang"
}
```

Expand Down
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/cmd_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/cmd_push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
30 changes: 15 additions & 15 deletions pkg/mod/modfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,38 +176,38 @@ 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)

urlpath := utils.JoinPath(settings.DEFAULT_REPO, "k8s")

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)
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions pkg/package/test_data/expected/kcl.mod.reverse.lock
Original file line number Diff line number Diff line change
@@ -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"
8 changes: 8 additions & 0 deletions pkg/package/test_data/expected/kcl.reverse.mod
Original file line number Diff line number Diff line change
@@ -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"
25 changes: 21 additions & 4 deletions pkg/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()'
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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',
Expand Down
19 changes: 17 additions & 2 deletions pkg/settings/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)

Expand Down Expand Up @@ -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")
}
2 changes: 1 addition & 1 deletion pkg/settings/test_data/expected.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"DefaultOciRegistry":"ghcr.io","DefaultOciRepo":"kusionstack"}
{"DefaultOciRegistry":"ghcr.io","DefaultOciRepo":"kcl-lang"}
Original file line number Diff line number Diff line change
@@ -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 '<workspace>/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 '<workspace>/ghcr.io/kcl-lang/k8s' successfully.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
kpm pull k8s:1.27.2
kpm pull k8s:1.27
Original file line number Diff line number Diff line change
@@ -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 '<workspace>/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 '<workspace>/ghcr.io/kcl-lang/k8s/1.27' successfully.

0 comments on commit ba52f1b

Please sign in to comment.