mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 06:23:17 -10:00
internal/shell, a stdlib leaf: Quote, QuoteJoin and EnvName. They
replace the 4 shell quoters (lima's shq and shqJoin, docker's shq, vm's
and devcontainer's shellQuote) and the 4 variable-name regexes (docker's
envKey, vmssh's envName, vm's envVarName, devcontainer's envName).
lima's quoting tests move there as TestQuote and FuzzQuote.
internal/vmname, a stdlib leaf below project: Prefix, HashLen, IsVM and
SlugOf. project's vmName regex, IsVM and Prefix go (callers use vmname),
lima's projectVM, docker's hostname parsing and cli's export-name regex
use it. TestImageKey (lima) pinned imageKey before the change;
TestParseExportName covers the export names.
Value patterns exported once: from.IsDigest (lima's idPattern),
image.IsKey (build's keyLike and the keyPattern copies in lima, docker
and smolvm), image.ToolVersion (tools' versionLike) and image.ToolName,
with TestIsDigest and TestValuePatterns. state's featureName stays:
internal/state may not import internal/image.
netpolicy.PolicyFiles names a user layer's policy file only for a part
image.LayerName takes, so an image-name part such as ../x never names a
file outside the user's layers; TestPolicyFiles's ../x cases failed
without the check.
The structure test's owners check gets literals: a non-test string
literal holding '\'' outside internal/shell, or [0-9a-f]{14} outside
internal/vmname, fails, naming the owner. TestRulesFail has cases that
break and keep both. shell and vmname join the leaves' layer, and
devcontainer may import shell. Coverage baselines: shell and vmname
added at 100.0.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
41 lines
1.2 KiB
Go
41 lines
1.2 KiB
Go
// Package shell is how devvm writes for a POSIX shell: a word quoted, a
|
|
// command line of quoted words, and the names a shell's variables can
|
|
// have. Standard library only, so every package can use it.
|
|
package shell
|
|
|
|
import "strings"
|
|
|
|
// Quote quotes s as one word for a POSIX shell: in single quotes, each
|
|
// single quote in s ending the quotes, then a backslash-escaped quote,
|
|
// then opening them again. Nothing inside single quotes is special, so
|
|
// any bytes come through as they are.
|
|
func Quote(s string) string {
|
|
return "'" + strings.ReplaceAll(s, "'", `'\''`) + "'"
|
|
}
|
|
|
|
// QuoteJoin quotes each word and joins them with spaces: a command line.
|
|
func QuoteJoin(words []string) string {
|
|
q := make([]string, len(words))
|
|
for i, w := range words {
|
|
q[i] = Quote(w)
|
|
}
|
|
return strings.Join(q, " ")
|
|
}
|
|
|
|
// EnvName reports whether s can name a shell's variable: a letter or an
|
|
// underscore, then letters, digits and underscores.
|
|
func EnvName(s string) bool {
|
|
if s == "" {
|
|
return false
|
|
}
|
|
for i, r := range s {
|
|
switch {
|
|
case r == '_', r >= 'A' && r <= 'Z', r >= 'a' && r <= 'z':
|
|
case i > 0 && r >= '0' && r <= '9':
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|