internal/helper: relocate from helper
All checks were successful
Test / Create distribution (push) Successful in 33s
Test / Sandbox (push) Successful in 2m26s
Test / Hakurei (push) Successful in 3m15s
Test / Hpkg (push) Successful in 4m8s
Test / Sandbox (race detector) (push) Successful in 4m16s
Test / Hakurei (race detector) (push) Successful in 5m5s
Test / Flake checks (push) Successful in 1m23s
All checks were successful
Test / Create distribution (push) Successful in 33s
Test / Sandbox (push) Successful in 2m26s
Test / Hakurei (push) Successful in 3m15s
Test / Hpkg (push) Successful in 4m8s
Test / Sandbox (race detector) (push) Successful in 4m16s
Test / Hakurei (race detector) (push) Successful in 5m5s
Test / Flake checks (push) Successful in 1m23s
This package is ugly and is pending removal only kept alive by xdg-dbus-proxy. Its exported symbols are made available until v0.4.0 where it will be removed for #24. Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
59
internal/helper/args.go
Normal file
59
internal/helper/args.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package helper
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
type argsWt [][]byte
|
||||
|
||||
func (a argsWt) WriteTo(w io.Writer) (int64, error) {
|
||||
nt := 0
|
||||
for _, arg := range a {
|
||||
n, err := w.Write(arg)
|
||||
nt += n
|
||||
|
||||
if err != nil {
|
||||
return int64(nt), err
|
||||
}
|
||||
}
|
||||
|
||||
return int64(nt), nil
|
||||
}
|
||||
|
||||
func (a argsWt) String() string {
|
||||
return string(
|
||||
bytes.TrimSuffix(
|
||||
bytes.ReplaceAll(
|
||||
bytes.Join(a, nil),
|
||||
[]byte{0}, []byte{' '},
|
||||
),
|
||||
[]byte{' '},
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
// NewCheckedArgs returns a checked null-terminated argument writer for a copy of args.
|
||||
func NewCheckedArgs(args ...string) (wt io.WriterTo, err error) {
|
||||
a := make(argsWt, len(args))
|
||||
for i, arg := range args {
|
||||
a[i], err = syscall.ByteSliceFromString(arg)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
wt = a
|
||||
return
|
||||
}
|
||||
|
||||
// MustNewCheckedArgs returns a checked null-terminated argument writer for a copy of args.
|
||||
// If s contains a NUL byte this function panics instead of returning an error.
|
||||
func MustNewCheckedArgs(args ...string) io.WriterTo {
|
||||
a, err := NewCheckedArgs(args...)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
|
||||
return a
|
||||
}
|
||||
Reference in New Issue
Block a user