@@ -60,7 +60,7 @@ type Person struct {
60
60
Email string `yaml:"email"`
61
61
}
62
62
63
- func UpdateReadmeAndMetadata (pkgPath string ) error {
63
+ func UpdateReadmeAndMetadata (pkgPath string , allowUserMetadataOverride bool ) error {
64
64
fileName := filepath .Base (pkgPath )
65
65
if fileName != "kcl.mod" {
66
66
return nil
@@ -79,70 +79,90 @@ func UpdateReadmeAndMetadata(pkgPath string) error {
79
79
80
80
pkgName := kclPkg .GetPkgName ()
81
81
pkgTag := kclPkg .GetPkgVersion ()
82
+ ahDir := filepath .Join (pkgPath , pkgTag )
82
83
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 )
92
85
if err != nil {
93
86
return err
94
87
}
95
88
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
+ }
100
111
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
+ }
108
116
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
+ }
116
124
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 )
122
132
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
+ }
128
138
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
+ }
135
144
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
141
151
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
+ }
143
157
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
146
166
return err
147
167
}
148
168
@@ -153,12 +173,6 @@ func UpdateReadmeAndMetadata(pkgPath string) error {
153
173
}
154
174
}
155
175
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 )
162
176
return nil
163
177
}
164
178
@@ -169,7 +183,7 @@ func main() {
169
183
}
170
184
171
185
pkgModPath := os .Args [1 ]
172
- err := UpdateReadmeAndMetadata (pkgModPath )
186
+ err := UpdateReadmeAndMetadata (pkgModPath , true )
173
187
if err != nil {
174
188
log .Fatalf ("error: %v" , err )
175
189
}
0 commit comments