pkg/idtools: remove execCmd() utility

The `execCmd()` utility was a basic wrapper around `exec.Command()`. Inlining it
makes the code more transparent.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-10-09 18:59:12 +02:00
parent 6743bf3173
commit 1e88fe578e
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
4 changed files with 10 additions and 12 deletions

View file

@ -167,7 +167,7 @@ func callGetent(database, key string) (io.Reader, error) {
if getentCmd == "" {
return nil, fmt.Errorf("unable to find getent command")
}
out, err := execCmd(getentCmd, database, key)
out, err := exec.Command(getentCmd, database, key).CombinedOutput()
if err != nil {
exitCode, errC := getExitCode(err)
if errC != nil {

View file

@ -328,8 +328,8 @@ func compareTrees(left, right map[string]node) error {
}
func delUser(t *testing.T, name string) {
_, err := execCmd("userdel", name)
assert.Check(t, err)
out, err := exec.Command("userdel", name).CombinedOutput()
assert.Check(t, err, out)
}
func TestParseSubidFileWithNewlinesAndComments(t *testing.T) {

View file

@ -2,6 +2,7 @@ package idtools // import "github.com/docker/docker/pkg/idtools"
import (
"fmt"
"os/exec"
"regexp"
"sort"
"strconv"
@ -36,7 +37,7 @@ func AddNamespaceRangesUser(name string) (int, int, error) {
}
// Query the system for the created uid and gid pair
out, err := execCmd("id", name)
out, err := exec.Command("id", name).CombinedOutput()
if err != nil {
return -1, -1, fmt.Errorf("error trying to find uid/gid for new user %q: %v", name, err)
}
@ -81,7 +82,7 @@ func addUser(name string) error {
return fmt.Errorf("cannot add user; no useradd/adduser binary found")
}
if out, err := execCmd(userCommand, args...); err != nil {
if out, err := exec.Command(userCommand, args...).CombinedOutput(); err != nil {
return fmt.Errorf("failed to add user with error: %v; output: %q", err, string(out))
}
return nil
@ -100,7 +101,8 @@ func createSubordinateRanges(name string) error {
if err != nil {
return fmt.Errorf("can't find available subuid range: %v", err)
}
out, err := execCmd("usermod", "-v", fmt.Sprintf("%d-%d", startID, startID+defaultRangeLen-1), name)
idRange := fmt.Sprintf("%d-%d", startID, startID+defaultRangeLen-1)
out, err := exec.Command("usermod", "-v", idRange, name).CombinedOutput()
if err != nil {
return fmt.Errorf("unable to add subuid range to user: %q; output: %s, err: %v", name, out, err)
}
@ -116,7 +118,8 @@ func createSubordinateRanges(name string) error {
if err != nil {
return fmt.Errorf("can't find available subgid range: %v", err)
}
out, err := execCmd("usermod", "-w", fmt.Sprintf("%d-%d", startID, startID+defaultRangeLen-1), name)
idRange := fmt.Sprintf("%d-%d", startID, startID+defaultRangeLen-1)
out, err := exec.Command("usermod", "-w", idRange, name).CombinedOutput()
if err != nil {
return fmt.Errorf("unable to add subgid range to user: %q; output: %s, err: %v", name, out, err)
}

View file

@ -25,8 +25,3 @@ func resolveBinary(binname string) (string, error) {
}
return "", fmt.Errorf("Binary %q does not resolve to a binary of that name in $PATH (%q)", binname, resolvedPath)
}
func execCmd(cmd string, arg ...string) ([]byte, error) {
execCmd := exec.Command(cmd, arg...)
return execCmd.CombinedOutput()
}