util: make ExecComand return stdout and stderr as string

Most consumers of util.ExecCommand() need to convert the returned []byte
format of stdout and/or stderr to string. By having util.ExecCommand()
return strings instead, the code gets a little simpler.

A few commands return JSON that needs to be parsed. These commands will
be replaced by go-ceph implementations later on. For now, convert the
strings back to []byte when needed.

Signed-off-by: Niels de Vos <ndevos@redhat.com>
This commit is contained in:
Niels de Vos
2020-07-22 14:53:22 +02:00
committed by mergify[bot]
parent ddac66d76b
commit 36469b87e2
6 changed files with 31 additions and 28 deletions

View File

@ -41,7 +41,7 @@ func execCommandJSON(ctx context.Context, v interface{}, program string, args ..
return err
}
if err = json.Unmarshal(stdout, v); err != nil {
if err = json.Unmarshal([]byte(stdout), v); err != nil {
return fmt.Errorf("failed to unmarshal JSON for %s %v: %s: %w", program, util.StripSecretInArgs(args), stdout, err)
}

View File

@ -67,16 +67,15 @@ func getVolumeRootPathCeph(ctx context.Context, volOptions *volumeOptions, cr *u
"--keyfile="+cr.KeyFile)
if err != nil {
stdErrString := string(stderr)
klog.Errorf(util.Log(ctx, "failed to get the rootpath for the vol %s(%s) stdError %s"), string(volID), err, stdErrString)
klog.Errorf(util.Log(ctx, "failed to get the rootpath for the vol %s(%s) stdError %s"), string(volID), err, stderr)
if strings.HasPrefix(stdErrString, errNotFoundString) {
if strings.HasPrefix(stderr, errNotFoundString) {
return "", util.JoinErrors(ErrVolumeNotFound, err)
}
return "", err
}
return strings.TrimSuffix(string(stdout), "\n"), nil
return strings.TrimSuffix(stdout, "\n"), nil
}
type localClusterState struct {

View File

@ -176,7 +176,7 @@ func mountFuse(ctx context.Context, mountPoint string, cr *util.Credentials, vol
// We need "starting fuse" meaning the mount is ok
// and PID of the ceph-fuse daemon for unmount
match := fusePidRx.FindSubmatch(stderr)
match := fusePidRx.FindSubmatch([]byte(stderr))
// validMatchLength is set to 2 as match is expected
// to have 2 items, starting fuse and PID of the fuse daemon
const validMatchLength = 2