Skip to content

Commit

Permalink
feat: allow the custom artifacthub-pkg.yml
Browse files Browse the repository at this point in the history
Signed-off-by: zongz <[email protected]>
  • Loading branch information
zong-zhe committed Nov 6, 2023
1 parent 5a82772 commit ba2ebb1
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 64 deletions.
25 changes: 25 additions & 0 deletions helloworld/artifacthub-pkg.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: 0.1.1
name: helloworld
displayName: helloworld
createdAt: "2023-10-27T03:24:50Z"
description: This is a KCL package For Testing
links:
- name: KCL homepage
url: https://kcl-lang.io/
- name: KCL repo
url: https://github.com/kcl-lang/kcl
install: |
#### Add `helloworld` with tag `0.1.1` as dependency
```
kpm add helloworld:0.1.1
```
#### Pull `helloworld` with tag `0.1.1` to local
```
kpm pull helloworld:0.1.1
```
maintainers:
- name: kcl-lang.io
email: [email protected]
provider:
name: kcl-lang.io
2 changes: 1 addition & 1 deletion helloworld/kcl.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "helloworld"
edition = "*"
version = "0.1.0"
version = "0.1.1"

132 changes: 73 additions & 59 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type Person struct {
Email string `yaml:"email"`
}

func UpdateReadmeAndMetadata(pkgPath string) error {
func UpdateReadmeAndMetadata(pkgPath string, allowUserMetadataOverride bool) error {
fileName := filepath.Base(pkgPath)
if fileName != "kcl.mod" {
return nil
Expand All @@ -79,70 +79,90 @@ func UpdateReadmeAndMetadata(pkgPath string) error {

pkgName := kclPkg.GetPkgName()
pkgTag := kclPkg.GetPkgVersion()
ahDir := filepath.Join(pkgPath, pkgTag)

manifest := ocispec.Manifest{}
jsonDesc, err := kpmClient.FetchOciManifestIntoJsonStr(opt.OciFetchOptions{
FetchBytesOptions: oras.DefaultFetchBytesOptions,
OciOptions: opt.OciOptions{
Reg: kpmClient.GetSettings().DefaultOciRegistry(),
Repo: fmt.Sprintf("%s/%s", kpmClient.GetSettings().DefaultOciRepo(), pkgName),
Tag: pkgTag,
},
})
err = os.MkdirAll(ahDir, 0755)
if err != nil {
return err
}

err = json.Unmarshal([]byte(jsonDesc), &manifest)
if err != nil {
return err
}
// Check if artifacthub-pkg.yaml already exists
ahConfPath := filepath.Join(pkgPath, AHConfFile)
if _, err := os.Stat(ahConfPath); err == nil && allowUserMetadataOverride {
// artifacthub-pkg.yaml exists, copy it to the new directory
err = copy.Copy(ahConfPath, filepath.Join(ahDir, AHConfFile))
if err != nil {
return err
}
// artifacthub-pkg.yaml does not exist, generate a new one
} else if os.IsNotExist(err) || (!os.IsNotExist(err) && !allowUserMetadataOverride) {
manifest := ocispec.Manifest{}
jsonDesc, err := kpmClient.FetchOciManifestIntoJsonStr(opt.OciFetchOptions{
FetchBytesOptions: oras.DefaultFetchBytesOptions,
OciOptions: opt.OciOptions{
Reg: kpmClient.GetSettings().DefaultOciRegistry(),
Repo: fmt.Sprintf("%s/%s", kpmClient.GetSettings().DefaultOciRepo(), pkgName),
Tag: pkgTag,
},
})
if err != nil {
return err
}

name := manifest.Annotations[constants.DEFAULT_KCL_OCI_MANIFEST_NAME]
tag := manifest.Annotations[constants.DEFAULT_KCL_OCI_MANIFEST_VERSION]
createTime := manifest.Annotations[constants.DEFAULT_CREATE_OCI_MANIFEST_TIME]
desc := manifest.Annotations[constants.DEFAULT_KCL_OCI_MANIFEST_DESCRIPTION]
if len(desc) == 0 {
desc = DefaultPkgDesc
}
err = json.Unmarshal([]byte(jsonDesc), &manifest)
if err != nil {
return err
}

// 2. generate the install command from the markdown template
installationTemplate, err := os.ReadFile("./templates/install.md")
if err != nil {
return err
}
installDoc := strings.Replace(string(installationTemplate), MdFlagPackageName, pkgName, -1)
installDoc = strings.Replace(string(installDoc), MdFlagPackageTag, pkgTag, -1)
name := manifest.Annotations[constants.DEFAULT_KCL_OCI_MANIFEST_NAME]
tag := manifest.Annotations[constants.DEFAULT_KCL_OCI_MANIFEST_VERSION]
createTime := manifest.Annotations[constants.DEFAULT_CREATE_OCI_MANIFEST_TIME]
desc := manifest.Annotations[constants.DEFAULT_KCL_OCI_MANIFEST_DESCRIPTION]
if len(desc) == 0 {
desc = DefaultPkgDesc
}

// 3. load the artifacthub-pkg.yaml template
data, err := os.ReadFile("./templates/ah.yaml")
if err != nil {
return err
}
// 2. generate the install command from the markdown template
installationTemplate, err := os.ReadFile("./templates/install.md")
if err != nil {
return err
}
installDoc := strings.Replace(string(installationTemplate), MdFlagPackageName, pkgName, -1)
installDoc = strings.Replace(string(installDoc), MdFlagPackageTag, pkgTag, -1)

var metadata Metadata
err = yaml.Unmarshal(data, &metadata)
if err != nil {
return err
}
// 3. load the artifacthub-pkg.yaml template
data, err := os.ReadFile("./templates/ah.yaml")
if err != nil {
return err
}

metadata.Name = name
metadata.DisplayName = name
metadata.Version = tag
metadata.CreatedAt = createTime
metadata.Description = desc
metadata.Install = installDoc
var metadata Metadata
err = yaml.Unmarshal(data, &metadata)
if err != nil {
return err
}

// 4. generate new artifacthub-pkg.yaml
data, err = yaml.Marshal(&metadata)
if err != nil {
return err
}
metadata.Name = name
metadata.DisplayName = name
metadata.Version = tag
metadata.CreatedAt = createTime
metadata.Description = desc
metadata.Install = installDoc

ahDir := filepath.Join(pkgPath, pkgTag)
// 4. generate new artifacthub-pkg.yaml
data, err = yaml.Marshal(&metadata)
if err != nil {
return err
}

err = os.MkdirAll(ahDir, 0755)
if err != nil {
err = os.WriteFile(filepath.Join(ahDir, AHConfFile), data, 0644)
if err != nil {
return err
}

fmt.Printf("generate artifacthub-pkg.yaml for %s succeed\n", pkgName)
} else {
// Some other error occurred
return err
}

Expand All @@ -153,12 +173,6 @@ func UpdateReadmeAndMetadata(pkgPath string) error {
}
}

err = os.WriteFile(filepath.Join(ahDir, AHConfFile), data, 0644)
if err != nil {
return err
}

fmt.Printf("generate artifacthub-pkg.yaml for %s succeed\n", pkgName)
return nil
}

Expand All @@ -169,7 +183,7 @@ func main() {
}

pkgModPath := os.Args[1]
err := UpdateReadmeAndMetadata(pkgModPath)
err := UpdateReadmeAndMetadata(pkgModPath, true)
if err != nil {
log.Fatalf("error: %v", err)
}
Expand Down
35 changes: 31 additions & 4 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ func TestUpdateReadmeAndMetadata(t *testing.T) {
assert.Equal(t, nil, err)
testDir := filepath.Join(pwd, "helloworld")
modPath := filepath.Join(testDir, "kcl.mod")
ahPath := filepath.Join(testDir, "0.1.0", "artifacthub-pkg.yaml")
ahPath := filepath.Join(testDir, "0.1.1", "artifacthub-pkg.yaml")

if utils.DirExists(ahPath) {
err = os.Remove(ahPath)
assert.Equal(t, nil, err)
}
assert.Equal(t, false, utils.DirExists(ahPath))
err = UpdateReadmeAndMetadata(modPath)
err = UpdateReadmeAndMetadata(modPath, false)
assert.Equal(t, nil, err)
assert.Equal(t, true, utils.DirExists(ahPath))

Expand All @@ -36,7 +36,7 @@ func TestUpdateReadmeAndMetadata(t *testing.T) {

assert.Equal(t, "helloworld", metadata.Name)
assert.Equal(t, "helloworld", metadata.DisplayName)
assert.Equal(t, "0.1.0", metadata.Version)
assert.Equal(t, "0.1.1", metadata.Version)
assert.Equal(t, "This is a KCL package", metadata.Description)
assert.Equal(t, len(metadata.Links), 2)
assert.Equal(t, metadata.Links[0].Name, "KCL homepage")
Expand All @@ -51,6 +51,33 @@ func TestUpdateReadmeAndMetadata(t *testing.T) {
installationTemplate, err := os.ReadFile("./templates/install.md")
assert.Equal(t, nil, err)
installDoc := strings.Replace(string(installationTemplate), MdFlagPackageName, "helloworld", -1)
installDoc = strings.Replace(string(installDoc), MdFlagPackageTag, "0.1.0", -1)
installDoc = strings.Replace(string(installDoc), MdFlagPackageTag, "0.1.1", -1)
assert.Equal(t, installDoc, metadata.Install)
}

func TestCustomAhYml(t *testing.T) {
pwd, err := os.Getwd()
assert.Equal(t, nil, err)
testDir := filepath.Join(pwd, "helloworld")
modPath := filepath.Join(testDir, "kcl.mod")
ahPath := filepath.Join(testDir, "0.1.1", "artifacthub-pkg.yaml")

if utils.DirExists(ahPath) {
err = os.Remove(ahPath)
assert.Equal(t, nil, err)
}
assert.Equal(t, false, utils.DirExists(ahPath))
err = UpdateReadmeAndMetadata(modPath, true)
assert.Equal(t, nil, err)
assert.Equal(t, true, utils.DirExists(ahPath))

buf, err := os.ReadFile(ahPath)
assert.Equal(t, nil, err)

var metadata Metadata
err = yaml.Unmarshal(buf, &metadata)
assert.Equal(t, nil, err)

assert.Equal(t, "0.1.1", metadata.Version)
assert.Equal(t, "This is a KCL package For Testing", metadata.Description)
}

0 comments on commit ba2ebb1

Please sign in to comment.