Skip to content

Commit 3e141d1

Browse files
authored
Merge pull request #51 from zong-zhe/add-default-ah-yml
feat: allow the custom artifacthub-pkg.yml
2 parents 164bee6 + ba2ebb1 commit 3e141d1

File tree

4 files changed

+130
-64
lines changed

4 files changed

+130
-64
lines changed

helloworld/artifacthub-pkg.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
version: 0.1.1
2+
name: helloworld
3+
displayName: helloworld
4+
createdAt: "2023-10-27T03:24:50Z"
5+
description: This is a KCL package For Testing
6+
links:
7+
- name: KCL homepage
8+
url: https://kcl-lang.io/
9+
- name: KCL repo
10+
url: https://github.com/kcl-lang/kcl
11+
install: |
12+
#### Add `helloworld` with tag `0.1.1` as dependency
13+
```
14+
kpm add helloworld:0.1.1
15+
```
16+
17+
#### Pull `helloworld` with tag `0.1.1` to local
18+
```
19+
kpm pull helloworld:0.1.1
20+
```
21+
maintainers:
22+
- name: kcl-lang.io
23+
24+
provider:
25+
name: kcl-lang.io

helloworld/kcl.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
22
name = "helloworld"
33
edition = "*"
4-
version = "0.1.0"
4+
version = "0.1.1"
55

main.go

Lines changed: 73 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ type Person struct {
6060
Email string `yaml:"email"`
6161
}
6262

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

8080
pkgName := kclPkg.GetPkgName()
8181
pkgTag := kclPkg.GetPkgVersion()
82+
ahDir := filepath.Join(pkgPath, pkgTag)
8283

83-
manifest := ocispec.Manifest{}
84-
jsonDesc, err := kpmClient.FetchOciManifestIntoJsonStr(opt.OciFetchOptions{
85-
FetchBytesOptions: oras.DefaultFetchBytesOptions,
86-
OciOptions: opt.OciOptions{
87-
Reg: kpmClient.GetSettings().DefaultOciRegistry(),
88-
Repo: fmt.Sprintf("%s/%s", kpmClient.GetSettings().DefaultOciRepo(), pkgName),
89-
Tag: pkgTag,
90-
},
91-
})
84+
err = os.MkdirAll(ahDir, 0755)
9285
if err != nil {
9386
return err
9487
}
9588

96-
err = json.Unmarshal([]byte(jsonDesc), &manifest)
97-
if err != nil {
98-
return err
99-
}
89+
// Check if artifacthub-pkg.yaml already exists
90+
ahConfPath := filepath.Join(pkgPath, AHConfFile)
91+
if _, err := os.Stat(ahConfPath); err == nil && allowUserMetadataOverride {
92+
// artifacthub-pkg.yaml exists, copy it to the new directory
93+
err = copy.Copy(ahConfPath, filepath.Join(ahDir, AHConfFile))
94+
if err != nil {
95+
return err
96+
}
97+
// artifacthub-pkg.yaml does not exist, generate a new one
98+
} else if os.IsNotExist(err) || (!os.IsNotExist(err) && !allowUserMetadataOverride) {
99+
manifest := ocispec.Manifest{}
100+
jsonDesc, err := kpmClient.FetchOciManifestIntoJsonStr(opt.OciFetchOptions{
101+
FetchBytesOptions: oras.DefaultFetchBytesOptions,
102+
OciOptions: opt.OciOptions{
103+
Reg: kpmClient.GetSettings().DefaultOciRegistry(),
104+
Repo: fmt.Sprintf("%s/%s", kpmClient.GetSettings().DefaultOciRepo(), pkgName),
105+
Tag: pkgTag,
106+
},
107+
})
108+
if err != nil {
109+
return err
110+
}
100111

101-
name := manifest.Annotations[constants.DEFAULT_KCL_OCI_MANIFEST_NAME]
102-
tag := manifest.Annotations[constants.DEFAULT_KCL_OCI_MANIFEST_VERSION]
103-
createTime := manifest.Annotations[constants.DEFAULT_CREATE_OCI_MANIFEST_TIME]
104-
desc := manifest.Annotations[constants.DEFAULT_KCL_OCI_MANIFEST_DESCRIPTION]
105-
if len(desc) == 0 {
106-
desc = DefaultPkgDesc
107-
}
112+
err = json.Unmarshal([]byte(jsonDesc), &manifest)
113+
if err != nil {
114+
return err
115+
}
108116

109-
// 2. generate the install command from the markdown template
110-
installationTemplate, err := os.ReadFile("./templates/install.md")
111-
if err != nil {
112-
return err
113-
}
114-
installDoc := strings.Replace(string(installationTemplate), MdFlagPackageName, pkgName, -1)
115-
installDoc = strings.Replace(string(installDoc), MdFlagPackageTag, pkgTag, -1)
117+
name := manifest.Annotations[constants.DEFAULT_KCL_OCI_MANIFEST_NAME]
118+
tag := manifest.Annotations[constants.DEFAULT_KCL_OCI_MANIFEST_VERSION]
119+
createTime := manifest.Annotations[constants.DEFAULT_CREATE_OCI_MANIFEST_TIME]
120+
desc := manifest.Annotations[constants.DEFAULT_KCL_OCI_MANIFEST_DESCRIPTION]
121+
if len(desc) == 0 {
122+
desc = DefaultPkgDesc
123+
}
116124

117-
// 3. load the artifacthub-pkg.yaml template
118-
data, err := os.ReadFile("./templates/ah.yaml")
119-
if err != nil {
120-
return err
121-
}
125+
// 2. generate the install command from the markdown template
126+
installationTemplate, err := os.ReadFile("./templates/install.md")
127+
if err != nil {
128+
return err
129+
}
130+
installDoc := strings.Replace(string(installationTemplate), MdFlagPackageName, pkgName, -1)
131+
installDoc = strings.Replace(string(installDoc), MdFlagPackageTag, pkgTag, -1)
122132

123-
var metadata Metadata
124-
err = yaml.Unmarshal(data, &metadata)
125-
if err != nil {
126-
return err
127-
}
133+
// 3. load the artifacthub-pkg.yaml template
134+
data, err := os.ReadFile("./templates/ah.yaml")
135+
if err != nil {
136+
return err
137+
}
128138

129-
metadata.Name = name
130-
metadata.DisplayName = name
131-
metadata.Version = tag
132-
metadata.CreatedAt = createTime
133-
metadata.Description = desc
134-
metadata.Install = installDoc
139+
var metadata Metadata
140+
err = yaml.Unmarshal(data, &metadata)
141+
if err != nil {
142+
return err
143+
}
135144

136-
// 4. generate new artifacthub-pkg.yaml
137-
data, err = yaml.Marshal(&metadata)
138-
if err != nil {
139-
return err
140-
}
145+
metadata.Name = name
146+
metadata.DisplayName = name
147+
metadata.Version = tag
148+
metadata.CreatedAt = createTime
149+
metadata.Description = desc
150+
metadata.Install = installDoc
141151

142-
ahDir := filepath.Join(pkgPath, pkgTag)
152+
// 4. generate new artifacthub-pkg.yaml
153+
data, err = yaml.Marshal(&metadata)
154+
if err != nil {
155+
return err
156+
}
143157

144-
err = os.MkdirAll(ahDir, 0755)
145-
if err != nil {
158+
err = os.WriteFile(filepath.Join(ahDir, AHConfFile), data, 0644)
159+
if err != nil {
160+
return err
161+
}
162+
163+
fmt.Printf("generate artifacthub-pkg.yaml for %s succeed\n", pkgName)
164+
} else {
165+
// Some other error occurred
146166
return err
147167
}
148168

@@ -153,12 +173,6 @@ func UpdateReadmeAndMetadata(pkgPath string) error {
153173
}
154174
}
155175

156-
err = os.WriteFile(filepath.Join(ahDir, AHConfFile), data, 0644)
157-
if err != nil {
158-
return err
159-
}
160-
161-
fmt.Printf("generate artifacthub-pkg.yaml for %s succeed\n", pkgName)
162176
return nil
163177
}
164178

@@ -169,7 +183,7 @@ func main() {
169183
}
170184

171185
pkgModPath := os.Args[1]
172-
err := UpdateReadmeAndMetadata(pkgModPath)
186+
err := UpdateReadmeAndMetadata(pkgModPath, true)
173187
if err != nil {
174188
log.Fatalf("error: %v", err)
175189
}

main_test.go

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ func TestUpdateReadmeAndMetadata(t *testing.T) {
1616
assert.Equal(t, nil, err)
1717
testDir := filepath.Join(pwd, "helloworld")
1818
modPath := filepath.Join(testDir, "kcl.mod")
19-
ahPath := filepath.Join(testDir, "0.1.0", "artifacthub-pkg.yaml")
19+
ahPath := filepath.Join(testDir, "0.1.1", "artifacthub-pkg.yaml")
2020

2121
if utils.DirExists(ahPath) {
2222
err = os.Remove(ahPath)
2323
assert.Equal(t, nil, err)
2424
}
2525
assert.Equal(t, false, utils.DirExists(ahPath))
26-
err = UpdateReadmeAndMetadata(modPath)
26+
err = UpdateReadmeAndMetadata(modPath, false)
2727
assert.Equal(t, nil, err)
2828
assert.Equal(t, true, utils.DirExists(ahPath))
2929

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

3737
assert.Equal(t, "helloworld", metadata.Name)
3838
assert.Equal(t, "helloworld", metadata.DisplayName)
39-
assert.Equal(t, "0.1.0", metadata.Version)
39+
assert.Equal(t, "0.1.1", metadata.Version)
4040
assert.Equal(t, "This is a KCL package", metadata.Description)
4141
assert.Equal(t, len(metadata.Links), 2)
4242
assert.Equal(t, metadata.Links[0].Name, "KCL homepage")
@@ -51,6 +51,33 @@ func TestUpdateReadmeAndMetadata(t *testing.T) {
5151
installationTemplate, err := os.ReadFile("./templates/install.md")
5252
assert.Equal(t, nil, err)
5353
installDoc := strings.Replace(string(installationTemplate), MdFlagPackageName, "helloworld", -1)
54-
installDoc = strings.Replace(string(installDoc), MdFlagPackageTag, "0.1.0", -1)
54+
installDoc = strings.Replace(string(installDoc), MdFlagPackageTag, "0.1.1", -1)
5555
assert.Equal(t, installDoc, metadata.Install)
5656
}
57+
58+
func TestCustomAhYml(t *testing.T) {
59+
pwd, err := os.Getwd()
60+
assert.Equal(t, nil, err)
61+
testDir := filepath.Join(pwd, "helloworld")
62+
modPath := filepath.Join(testDir, "kcl.mod")
63+
ahPath := filepath.Join(testDir, "0.1.1", "artifacthub-pkg.yaml")
64+
65+
if utils.DirExists(ahPath) {
66+
err = os.Remove(ahPath)
67+
assert.Equal(t, nil, err)
68+
}
69+
assert.Equal(t, false, utils.DirExists(ahPath))
70+
err = UpdateReadmeAndMetadata(modPath, true)
71+
assert.Equal(t, nil, err)
72+
assert.Equal(t, true, utils.DirExists(ahPath))
73+
74+
buf, err := os.ReadFile(ahPath)
75+
assert.Equal(t, nil, err)
76+
77+
var metadata Metadata
78+
err = yaml.Unmarshal(buf, &metadata)
79+
assert.Equal(t, nil, err)
80+
81+
assert.Equal(t, "0.1.1", metadata.Version)
82+
assert.Equal(t, "This is a KCL package For Testing", metadata.Description)
83+
}

0 commit comments

Comments
 (0)