Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions config/types/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package types

import (
"encoding/base64"
"errors"
"flag"
"fmt"
Expand Down Expand Up @@ -129,6 +130,19 @@ func (fc FileContents) Validate() report.Report {
return report.Report{}
}

func makeDataUrl(contents []byte) string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest putting private function below the init() function, which tends to be at the top of the file.

opaque := "," + dataurl.Escape(contents)
b64 := ";base64," + base64.StdEncoding.EncodeToString(contents)
if len(b64) < len(opaque) {
opaque = b64
}
uri := (&url.URL{
Scheme: "data",
Opaque: opaque,
}).String()
return uri
}
Comment on lines +133 to +144
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think formatting can be more compact:

Suggested change
func makeDataUrl(contents []byte) string {
opaque := "," + dataurl.Escape(contents)
b64 := ";base64," + base64.StdEncoding.EncodeToString(contents)
if len(b64) < len(opaque) {
opaque = b64
}
uri := (&url.URL{
Scheme: "data",
Opaque: opaque,
}).String()
return uri
}
func makeDataUrl(contents []byte) string {
opaque := "," + dataurl.Escape(contents)
if b64 := ";base64," + base64.StdEncoding.EncodeToString(contents); len(b64) < len(opaque) {
opaque = b64
}
return (&url.URL{
Scheme: "data",
Opaque: opaque,
}).String()
}

b64 declaration can be moved outside of if block though, no preferences here.


func init() {
register(func(in Config, ast astnode.AstNode, out ignTypes.Config, platform string) (ignTypes.Config, report.Report, astnode.AstNode) {
r := report.Report{}
Expand Down Expand Up @@ -167,10 +181,7 @@ func init() {

if file.Contents.Inline != "" {
newFile.Contents = ignTypes.FileContents{
Source: (&url.URL{
Scheme: "data",
Opaque: "," + dataurl.EscapeString(file.Contents.Inline),
}).String(),
Source: makeDataUrl([]byte(file.Contents.Inline)),
}
}

Expand Down Expand Up @@ -203,10 +214,7 @@ func init() {

// Include the contents of the local file as if it were provided inline.
newFile.Contents = ignTypes.FileContents{
Source: (&url.URL{
Scheme: "data",
Opaque: "," + dataurl.Escape(contents),
}).String(),
Source: makeDataUrl(contents),
}
}

Expand Down