Provide shell and escape options when formatting matching files

This commit is contained in:
Michal Dorner 2020-12-13 21:07:47 +01:00
parent ada1eee648
commit e4d886f503
No known key found for this signature in database
GPG key ID: 9EEE04B48DA36786
7 changed files with 124 additions and 47 deletions

View file

@ -6,9 +6,9 @@ import {Webhooks} from '@octokit/webhooks'
import {Filter, FilterResults} from './filter'
import {File, ChangeStatus} from './file'
import * as git from './git'
import shellEscape from './shell-escape'
import {escape, shellEscape} from './shell-escape'
type ExportFormat = 'none' | 'json' | 'shell'
type ExportFormat = 'none' | 'json' | 'shell' | 'escape'
async function run(): Promise<void> {
try {
@ -201,6 +201,8 @@ function serializeExport(files: File[], format: ExportFormat): string {
switch (format) {
case 'json':
return JSON.stringify(fileNames)
case 'escape':
return fileNames.map(escape).join(' ')
case 'shell':
return fileNames.map(shellEscape).join(' ')
default:
@ -209,7 +211,7 @@ function serializeExport(files: File[], format: ExportFormat): string {
}
function isExportFormat(value: string): value is ExportFormat {
return value === 'none' || value === 'shell' || value === 'json'
return value === 'none' || value === 'shell' || value === 'json' || value === 'escape'
}
run()

View file

@ -1,5 +1,28 @@
// Uses easy safe set of characters which can be left unescaped to keep it readable.
// Every other character will be backslash-escaped
export default function shellEscape(value: string): string {
// Backslash escape every character except small subset of definitely safe characters
export function escape(value: string): string {
return value.replace(/([^a-zA-Z0-9,._+:@%/-])/gm, '\\$1')
}
// Returns filename escaped for usage as shell argument.
// Applies "human readable" approach with as few escaping applied as possible
export function shellEscape(value: string): string {
if (value === '') return value
// Only safe characters
if (/^[a-zA-Z0-9,._+:@%/-]+$/m.test(value)) {
return value
}
if (value.includes("'")) {
// Only safe characters, single quotes and white-spaces
if (/^[a-zA-Z0-9,._+:@%/'\s-]+$/m.test(value)) {
return `"${value}"`
}
// Split by single quote and apply escaping recursively
return value.split("'").map(shellEscape).join("\\'")
}
// Contains some unsafe characters but no single quote
return `'${value}'`
}