Skip to content

Commit 56b588a

Browse files
committed
simplify TranslateResource
1 parent 4021d10 commit 56b588a

File tree

1 file changed

+23
-36
lines changed

1 file changed

+23
-36
lines changed

base/v0_7_exp/translate.go

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -149,23 +149,21 @@ func TranslateResource(from Resource, options common.TranslateOptions) (to types
149149
return
150150
}
151151

152-
handleIgnitionResource(contents, c, &r, &to, &tm, options)
152+
// Validating the contents of the local file from here since there is no way to
153+
// get both the filename and filedirectory in the Validate context
154+
if strings.HasPrefix(c.String(), "$.ignition.config") {
155+
rp, err := ValidateIgnitionConfig(c, contents)
156+
r.Merge(rp)
157+
if err != nil {
158+
return
159+
}
160+
}
161+
contentToURL(contents, c, &r, &to, &tm, options)
153162
}
154163

155164
if from.Inline != nil {
156165
c := path.New("yaml", "inline")
157-
158-
src, compression, err := baseutil.MakeDataURL([]byte(*from.Inline), to.Compression, !options.NoResourceAutoCompression)
159-
if err != nil {
160-
r.AddOnError(c, err)
161-
return
162-
}
163-
to.Source = &src
164-
tm.AddTranslation(c, path.New("json", "source"))
165-
if compression != nil {
166-
to.Compression = compression
167-
tm.AddTranslation(c, path.New("json", "compression"))
168-
}
166+
contentToURL([]byte(*from.Inline), c, &r, &to, &tm, options)
169167
}
170168

171169
if from.LocalButane != nil {
@@ -187,7 +185,16 @@ func TranslateResource(from Resource, options common.TranslateOptions) (to types
187185
return
188186
}
189187

190-
handleIgnitionResource(contents, c, &r, &to, &tm, options)
188+
// Validating the contents of the local file from here since there is no way to
189+
// get both the filename and filedirectory in the Validate context
190+
if strings.HasPrefix(c.String(), "$.ignition.config") {
191+
rp, err := ValidateIgnitionConfig(c, contents)
192+
r.Merge(rp)
193+
if err != nil {
194+
return
195+
}
196+
}
197+
contentToURL(contents, c, &r, &to, &tm, options)
191198
}
192199

193200
if from.InlineButane != nil {
@@ -203,17 +210,7 @@ func TranslateResource(from Resource, options common.TranslateOptions) (to types
203210
return
204211
}
205212

206-
src, compression, err := baseutil.MakeDataURL(contents, to.Compression, !options.NoResourceAutoCompression)
207-
if err != nil {
208-
r.AddOnError(c, err)
209-
return
210-
}
211-
to.Source = &src
212-
tm.AddTranslation(c, path.New("json", "source"))
213-
if compression != nil {
214-
to.Compression = compression
215-
tm.AddTranslation(c, path.New("json", "compression"))
216-
}
213+
contentToURL(contents, c, &r, &to, &tm, options)
217214
}
218215

219216
return
@@ -540,17 +537,7 @@ func mountUnitFromFS(fs Filesystem, remote bool) types.Unit {
540537
}
541538
}
542539

543-
func handleIgnitionResource(contents []byte, c path.ContextPath, r *report.Report, to *types.Resource, tm *translate.TranslationSet, options common.TranslateOptions) {
544-
// Validating the contents of the local file from here since there is no way to
545-
// get both the filename and filedirectory in the Validate context
546-
if strings.HasPrefix(c.String(), "$.ignition.config") {
547-
rp, err := ValidateIgnitionConfig(c, contents)
548-
r.Merge(rp)
549-
if err != nil {
550-
return
551-
}
552-
}
553-
540+
func contentToURL(contents []byte, c path.ContextPath, r *report.Report, to *types.Resource, tm *translate.TranslationSet, options common.TranslateOptions) {
554541
src, compression, err := baseutil.MakeDataURL(contents, to.Compression, !options.NoResourceAutoCompression)
555542
if err != nil {
556543
r.AddOnError(c, err)

0 commit comments

Comments
 (0)