File: powershell.go

package info (click to toggle)
golang-github-apparentlymart-go-shquot 0.0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 128 kB
  • sloc: makefile: 2
file content (59 lines) | stat: -rw-r--r-- 1,592 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package shquot

import (
	"strings"
)

var powerShellReplace = strings.NewReplacer(
	"`", "``",
	`"`, "`\"",
	"$", "`$",
	"\x00", "`0",
	"\x07", "`a",
	"\x08", "`b",
	"\x1f", "`e",
	"\x0c", "`f",
	"\n", "`n",
	"\r", "`r",
	"\t", "`t",
	"\v", "`v",
)

// ViaPowerShell wraps a "Split" quoting function to create a normal quoting
// function that produces a command string that can be passed to PowerShell to
// run the given command line as an external process.
//
// PowerShell uses a single string for all of the arguments, which must itself
// be quoted in a manner suitable for the program being run. The wrapped
// split-quoting function provides the quoting for the arguments.
//
// For example, if using PowerShell to run a program that uses the MSVC runtime's
// argument parsing functionality on Windows, use ViaPowerShell(WindowsArgvSplit)
// to get a suitably-quoted command line.
//
// This function uses the PowerShell Start-Process cmdlet to force the
// command to be interpreted as an external program rather than as a cmdlet
// or other PowerShell command type.
func ViaPowerShell(wrapped QS) Q {
	return func(cmdline []string) string {
		if len(cmdline) == 0 {
			return ""
		}
		cmd, args := wrapped(cmdline)

		var buf strings.Builder
		buf.WriteString("& Start-Process -FilePath ")
		powerShellQuoted(cmd, &buf)
		if len(args) > 0 {
			buf.WriteString(" -ArgumentList ")
			powerShellQuoted(args, &buf)
		}
		return buf.String()
	}
}

func powerShellQuoted(a string, buf *strings.Builder) {
	buf.WriteByte('"')
	powerShellReplace.WriteString(buf, a)
	buf.WriteByte('"')
}