From ba2ebb181c9cac4f9e6f78ce427a1d396427ccc4 Mon Sep 17 00:00:00 2001 From: zongz Date: Mon, 6 Nov 2023 19:51:29 +0800 Subject: [PATCH] feat: allow the custom artifacthub-pkg.yml Signed-off-by: zongz --- helloworld/artifacthub-pkg.yaml | 25 ++++++ helloworld/kcl.mod | 2 +- main.go | 132 ++++++++++++++++++-------------- main_test.go | 35 ++++++++- 4 files changed, 130 insertions(+), 64 deletions(-) create mode 100644 helloworld/artifacthub-pkg.yaml diff --git a/helloworld/artifacthub-pkg.yaml b/helloworld/artifacthub-pkg.yaml new file mode 100644 index 00000000..72fd7bbb --- /dev/null +++ b/helloworld/artifacthub-pkg.yaml @@ -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: kcl-lang.io@domainsbyproxy.com +provider: + name: kcl-lang.io diff --git a/helloworld/kcl.mod b/helloworld/kcl.mod index d71c4440..0e276852 100644 --- a/helloworld/kcl.mod +++ b/helloworld/kcl.mod @@ -1,5 +1,5 @@ [package] name = "helloworld" edition = "*" -version = "0.1.0" +version = "0.1.1" diff --git a/main.go b/main.go index 1274da5f..9c9b6030 100644 --- a/main.go +++ b/main.go @@ -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 @@ -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 } @@ -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 } @@ -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) } diff --git a/main_test.go b/main_test.go index 00867540..9364babf 100644 --- a/main_test.go +++ b/main_test.go @@ -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)) @@ -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") @@ -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) +}