From 35c006bdd9066fc8595d32d2d654a0ba28963488 Mon Sep 17 00:00:00 2001 From: Sven Sauleau Date: Fri, 8 May 2020 21:18:58 +0100 Subject: [PATCH] escape --- cmd/checker/main.go | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/cmd/checker/main.go b/cmd/checker/main.go index 1b1d4d7e..566a1a30 100644 --- a/cmd/checker/main.go +++ b/cmd/checker/main.go @@ -8,6 +8,7 @@ import ( "os" "path/filepath" "sort" + "strings" "github.com/cdnjs/tools/npm" "github.com/cdnjs/tools/packages" @@ -59,10 +60,7 @@ func showFiles(path string) { return } - fmt.Printf("Preview for `%s`\n", path) - if pckg.Autoupdate == nil || pckg.Autoupdate.Source != "npm" { - fmt.Println(pckg.Autoupdate) err(ctx, "unsupported autoupdate") return } @@ -81,19 +79,20 @@ func showFiles(path string) { // print info for the first version firstNpmVersion := npmVersions[0] - fmt.Printf("Last version (%s):\n", firstNpmVersion.Version) { tarballDir := npm.DownloadTar(ctx, firstNpmVersion.Tarball) filesToCopy := pckg.NpmFilesFrom(tarballDir) if len(filesToCopy) == 0 { - err(ctx, "No files will be published for this version; you can debug using") + errormsg := "" + errormsg += fmt.Sprintf("No files will be published for version %s.\n", firstNpmVersion.Version) for _, filemap := range pckg.NpmFileMap { for _, pattern := range filemap.Files { - fmt.Printf("[Click here to debug your glob pattern `%s`](%s).\n", pattern, makeGlobDebugLink(pattern, tarballDir)) + errormsg += fmt.Sprintf("[Click here to debug your glob pattern `%s`](%s).\n", pattern, makeGlobDebugLink(pattern, tarballDir)) } } + err(ctx, errormsg) goto moreversions } @@ -190,18 +189,18 @@ func lintPackage(path string) { func err(ctx context.Context, s string) { if prefix, ok := ctx.Value("loggerPrefix").(string); ok { - fmt.Printf("::error file=%s,line=1,col=1::%s\n", prefix, s) + fmt.Printf("::error file=%s,line=1,col=1::%s\n", prefix, escapeGitHub(s)) } else { - fmt.Printf("error: %s\n", s) + panic("unreachable") } errCount += 1 } func warn(ctx context.Context, s string) { if prefix, ok := ctx.Value("loggerPrefix").(string); ok { - fmt.Printf("::warning file=%s,line=1,col=1::%s\n", prefix, s) + fmt.Printf("::warning file=%s,line=1,col=1::%s\n", prefix, escapeGitHub(s)) } else { - fmt.Printf("warning: %s\n", s) + panic("unreachable") } } @@ -212,3 +211,10 @@ func shouldBeEmpty(name string) string { func shouldNotBeEmpty(name string) string { return fmt.Sprintf("%s should be specified", name) } + +func escapeGitHub(s string) string { + s = strings.ReplaceAll(s, "%", "%25") + s = strings.ReplaceAll(s, "\n", "%0A") + s = strings.ReplaceAll(s, "\r", "%0D") + return s +}