mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 06:23:17 -10:00
Every script a build runs with bash, devvm's own and the user's layers, now gets the prelude as one line, eval $'…', in place of its #! line, so bash's errors and $LINENO give the script's own line numbers. A script without a #! line is one line further down. - shell.QuoteLine: ANSI-C quoting for bash, on one line of printable ASCII; every other byte as \xHH, so a multibyte locale's lead byte (Shift-JIS, GBK, GB18030, Big5) can't swallow the backslash escaping a quote, and a NUL panics as a caller's bug. image's layer rule allows internal/shell, a leaf. - image.WithPrelude, the one place the prelude and a script are joined (OwnScript calls it), used by base.sh, apt.sh, the built-in layers and features, the user's layers, shared-tools/build.sh and oci/build.sh. Every devvm script has a #! line. A leading UTF-8 BOM goes before the #! check. - A build's exports for a company proxy (lima's proxyExports) and the user's CAs (trust's exports) end in "; ", not a newline, so they share the first line and move no line either; backend.Trust.Env says so. - The stub logs a bash -s script's second line, which is the script's own second line with or without the prelude. - Keys: every build script's bytes change, so base, every image and every shared tool image are built again once; the CHANGELOG's After you upgrade says so with git-lfs, and what VMs say until devvm update, and Breaking has its own heading. - Tests: TestQuoteLine (every byte but NUL, a break-out word, in /bin/bash 3.2 and PATH's bash, in C and each of ja_JP.SJIS, zh_CN.GBK, zh_CN.GB18030 and zh_TW.Big5 that locale -a has), TestQuoteLineNUL, TestWithPrelude's shebang and BOM cases, TestWithPreludeRuns (the real prelude's line gives the prelude back; a layer under set -euo pipefail gets the names, keeps its function and its line numbers, with and without exports before it). - e2e nix: the layer copy no longer adds the prelude itself. - Docs: images.md's prelude section, Customizing's layer bullet, structure.md. Co-Authored-By: Claude Opus 5.5 <[email protected]>
75 lines
2.2 KiB
Go
75 lines
2.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; and for bash, a word quoted on one line. 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, "'", `'\''`) + "'"
|
|
}
|
|
|
|
// QuoteLine quotes s as one word on one line for bash, not a POSIX sh:
|
|
// ANSI-C quoting, $'…', which bash 3.2 reads back as s. A backslash, a
|
|
// single quote and a newline are escaped, and every other byte that isn't
|
|
// printable ASCII is written as \xHH, so the word is printable ASCII
|
|
// alone: in a locale such as Shift-JIS or GBK, a raw byte from 0x80 up
|
|
// could start a character that swallows the backslash escaping a quote.
|
|
// A bash string can't hold a NUL, so a NUL in s is a caller's bug, and
|
|
// QuoteLine panics rather than drop it.
|
|
func QuoteLine(s string) string {
|
|
var b strings.Builder
|
|
b.WriteString("$'")
|
|
for i := range len(s) {
|
|
switch c := s[i]; {
|
|
case c == 0:
|
|
panic("shell.QuoteLine: a NUL, which a bash string can't hold")
|
|
case c == '\\' || c == '\'':
|
|
b.WriteByte('\\')
|
|
b.WriteByte(c)
|
|
case c == '\n':
|
|
b.WriteString(`\n`)
|
|
case c < 0x20 || c >= 0x7f:
|
|
b.WriteString(`\x`)
|
|
b.WriteByte(hex[c>>4])
|
|
b.WriteByte(hex[c&0xf])
|
|
default:
|
|
b.WriteByte(c)
|
|
}
|
|
}
|
|
b.WriteString("'")
|
|
return b.String()
|
|
}
|
|
|
|
const hex = "0123456789abcdef"
|
|
|
|
// 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
|
|
}
|