helper/bwrap: PositionalArg implement fmt.Stringer
All checks were successful
Test / Create distribution (push) Successful in 49s
Test / Run NixOS test (push) Successful in 3m28s

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
Ophestra 2025-02-14 23:58:52 +09:00
parent ace97952cc
commit be7d944b39
Signed by: cat
SSH Key Fingerprint: SHA256:gQ67O0enBZ7UdZypgtspB2FDM1g3GVw8nX0XSdcFw8Q
4 changed files with 28 additions and 44 deletions

View File

@ -30,7 +30,7 @@ func (c *Config) Args(syncFd *os.File, extraFiles *proc.ExtraFilesPre, files *[]
c.stringArgs(), c.stringArgs(),
c.pairArgs(), c.pairArgs(),
c.seccompArgs(), c.seccompArgs(),
newFile(positionalArgs[SyncFd], syncFd), newFile(SyncFd.String(), syncFd),
} }
builders = slices.Grow(builders, len(c.Filesystem)+1) builders = slices.Grow(builders, len(c.Filesystem)+1)

View File

@ -39,23 +39,23 @@ func (c *Config) Bind(src, dest string, opts ...bool) *Config {
if dev { if dev {
if try { if try {
c.Filesystem = append(c.Filesystem, &pairF{DevBindTry.Unwrap(), src, dest}) c.Filesystem = append(c.Filesystem, &pairF{DevBindTry.String(), src, dest})
} else { } else {
c.Filesystem = append(c.Filesystem, &pairF{DevBind.Unwrap(), src, dest}) c.Filesystem = append(c.Filesystem, &pairF{DevBind.String(), src, dest})
} }
return c return c
} else if write { } else if write {
if try { if try {
c.Filesystem = append(c.Filesystem, &pairF{BindTry.Unwrap(), src, dest}) c.Filesystem = append(c.Filesystem, &pairF{BindTry.String(), src, dest})
} else { } else {
c.Filesystem = append(c.Filesystem, &pairF{Bind.Unwrap(), src, dest}) c.Filesystem = append(c.Filesystem, &pairF{Bind.String(), src, dest})
} }
return c return c
} else { } else {
if try { if try {
c.Filesystem = append(c.Filesystem, &pairF{ROBindTry.Unwrap(), src, dest}) c.Filesystem = append(c.Filesystem, &pairF{ROBindTry.String(), src, dest})
} else { } else {
c.Filesystem = append(c.Filesystem, &pairF{ROBind.Unwrap(), src, dest}) c.Filesystem = append(c.Filesystem, &pairF{ROBind.String(), src, dest})
} }
return c return c
} }
@ -64,35 +64,35 @@ func (c *Config) Bind(src, dest string, opts ...bool) *Config {
// Dir create dir in sandbox // Dir create dir in sandbox
// (--dir DEST) // (--dir DEST)
func (c *Config) Dir(dest string) *Config { func (c *Config) Dir(dest string) *Config {
c.Filesystem = append(c.Filesystem, &stringF{Dir.Unwrap(), dest}) c.Filesystem = append(c.Filesystem, &stringF{Dir.String(), dest})
return c return c
} }
// RemountRO remount path as readonly; does not recursively remount // RemountRO remount path as readonly; does not recursively remount
// (--remount-ro DEST) // (--remount-ro DEST)
func (c *Config) RemountRO(dest string) *Config { func (c *Config) RemountRO(dest string) *Config {
c.Filesystem = append(c.Filesystem, &stringF{RemountRO.Unwrap(), dest}) c.Filesystem = append(c.Filesystem, &stringF{RemountRO.String(), dest})
return c return c
} }
// Procfs mount new procfs in sandbox // Procfs mount new procfs in sandbox
// (--proc DEST) // (--proc DEST)
func (c *Config) Procfs(dest string) *Config { func (c *Config) Procfs(dest string) *Config {
c.Filesystem = append(c.Filesystem, &stringF{Procfs.Unwrap(), dest}) c.Filesystem = append(c.Filesystem, &stringF{Procfs.String(), dest})
return c return c
} }
// DevTmpfs mount new dev in sandbox // DevTmpfs mount new dev in sandbox
// (--dev DEST) // (--dev DEST)
func (c *Config) DevTmpfs(dest string) *Config { func (c *Config) DevTmpfs(dest string) *Config {
c.Filesystem = append(c.Filesystem, &stringF{DevTmpfs.Unwrap(), dest}) c.Filesystem = append(c.Filesystem, &stringF{DevTmpfs.String(), dest})
return c return c
} }
// Mqueue mount new mqueue in sandbox // Mqueue mount new mqueue in sandbox
// (--mqueue DEST) // (--mqueue DEST)
func (c *Config) Mqueue(dest string) *Config { func (c *Config) Mqueue(dest string) *Config {
c.Filesystem = append(c.Filesystem, &stringF{Mqueue.Unwrap(), dest}) c.Filesystem = append(c.Filesystem, &stringF{Mqueue.String(), dest})
return c return c
} }

View File

@ -81,5 +81,5 @@ func (s *seccompBuilder) Append(args *[]string) {
return return
} }
*args = append(*args, positionalArgs[Seccomp], strconv.Itoa(int(s.Fd()))) *args = append(*args, Seccomp.String(), strconv.Itoa(int(s.Fd())))
} }

View File

@ -14,9 +14,7 @@ func init() {
type PositionalArg int type PositionalArg int
func (p PositionalArg) Unwrap() string { func (p PositionalArg) String() string { return positionalArgs[p] }
return positionalArgs[p]
}
const ( const (
Tmpfs PositionalArg = iota Tmpfs PositionalArg = iota
@ -87,9 +85,7 @@ type PermConfig[T FSBuilder] struct {
Inner T `json:"path"` Inner T `json:"path"`
} }
func (p *PermConfig[T]) Path() string { func (p *PermConfig[T]) Path() string { return p.Inner.Path() }
return p.Inner.Path()
}
func (p *PermConfig[T]) Len() int { func (p *PermConfig[T]) Len() int {
if p.Mode != nil { if p.Mode != nil {
@ -101,7 +97,7 @@ func (p *PermConfig[T]) Len() int {
func (p *PermConfig[T]) Append(args *[]string) { func (p *PermConfig[T]) Append(args *[]string) {
if p.Mode != nil { if p.Mode != nil {
*args = append(*args, Perms.Unwrap(), strconv.FormatInt(int64(*p.Mode), 8)) *args = append(*args, Perms.String(), strconv.FormatInt(int64(*p.Mode), 8))
} }
p.Inner.Append(args) p.Inner.Append(args)
} }
@ -115,9 +111,7 @@ type TmpfsConfig struct {
Dir string `json:"dir"` Dir string `json:"dir"`
} }
func (t *TmpfsConfig) Path() string { func (t *TmpfsConfig) Path() string { return t.Dir }
return t.Dir
}
func (t *TmpfsConfig) Len() int { func (t *TmpfsConfig) Len() int {
if t.Size > 0 { if t.Size > 0 {
@ -129,9 +123,9 @@ func (t *TmpfsConfig) Len() int {
func (t *TmpfsConfig) Append(args *[]string) { func (t *TmpfsConfig) Append(args *[]string) {
if t.Size > 0 { if t.Size > 0 {
*args = append(*args, Size.Unwrap(), strconv.Itoa(t.Size)) *args = append(*args, Size.String(), strconv.Itoa(t.Size))
} }
*args = append(*args, Tmpfs.Unwrap(), t.Dir) *args = append(*args, Tmpfs.String(), t.Dir)
} }
type OverlayConfig struct { type OverlayConfig struct {
@ -164,9 +158,7 @@ type OverlayConfig struct {
Dest string `json:"dest"` Dest string `json:"dest"`
} }
func (o *OverlayConfig) Path() string { func (o *OverlayConfig) Path() string { return o.Dest }
return o.Dest
}
func (o *OverlayConfig) Len() int { func (o *OverlayConfig) Len() int {
// (--tmp-overlay DEST) or (--ro-overlay DEST) // (--tmp-overlay DEST) or (--ro-overlay DEST)
@ -182,20 +174,20 @@ func (o *OverlayConfig) Len() int {
func (o *OverlayConfig) Append(args *[]string) { func (o *OverlayConfig) Append(args *[]string) {
// --overlay-src SRC // --overlay-src SRC
for _, src := range o.Src { for _, src := range o.Src {
*args = append(*args, OverlaySrc.Unwrap(), src) *args = append(*args, OverlaySrc.String(), src)
} }
if o.Persist != nil { if o.Persist != nil {
if o.Persist[0] != "" && o.Persist[1] != "" { if o.Persist[0] != "" && o.Persist[1] != "" {
// --overlay RWSRC WORKDIR // --overlay RWSRC WORKDIR
*args = append(*args, Overlay.Unwrap(), o.Persist[0], o.Persist[1]) *args = append(*args, Overlay.String(), o.Persist[0], o.Persist[1])
} else { } else {
// --ro-overlay // --ro-overlay
*args = append(*args, ROOverlay.Unwrap()) *args = append(*args, ROOverlay.String())
} }
} else { } else {
// --tmp-overlay // --tmp-overlay
*args = append(*args, TmpOverlay.Unwrap()) *args = append(*args, TmpOverlay.String())
} }
// DEST // DEST
@ -204,17 +196,9 @@ func (o *OverlayConfig) Append(args *[]string) {
type SymlinkConfig [2]string type SymlinkConfig [2]string
func (s SymlinkConfig) Path() string { func (s SymlinkConfig) Path() string { return s[1] }
return s[1] func (s SymlinkConfig) Len() int { return 3 }
} func (s SymlinkConfig) Append(args *[]string) { *args = append(*args, Symlink.String(), s[0], s[1]) }
func (s SymlinkConfig) Len() int {
return 3
}
func (s SymlinkConfig) Append(args *[]string) {
*args = append(*args, Symlink.Unwrap(), s[0], s[1])
}
type ChmodConfig map[string]os.FileMode type ChmodConfig map[string]os.FileMode
@ -224,6 +208,6 @@ func (c ChmodConfig) Len() int {
func (c ChmodConfig) Append(args *[]string) { func (c ChmodConfig) Append(args *[]string) {
for path, mode := range c { for path, mode := range c {
*args = append(*args, Chmod.Unwrap(), strconv.FormatInt(int64(mode), 8), path) *args = append(*args, Chmod.String(), strconv.FormatInt(int64(mode), 8), path)
} }
} }