cephfs admin: make a common func for unmarshaling json responses

Signed-off-by: John Mulligan <jmulligan@redhat.com>
This commit is contained in:
John Mulligan 2020-08-05 13:23:47 -04:00 committed by John Mulligan
parent 1e4c2d09ae
commit 4e5424dbfb
1 changed files with 11 additions and 7 deletions

View File

@ -79,14 +79,8 @@ type listNamedResult struct {
}
func parseListNames(res []byte, status string, err error) ([]string, error) {
if err != nil {
return nil, err
}
if status != "" {
return nil, fmt.Errorf("error status: %s", status)
}
var r []listNamedResult
if err := json.Unmarshal(res, &r); err != nil {
if err := unmarshalResponseJSON(res, status, err, &r); err != nil {
return nil, err
}
vl := make([]string, len(r))
@ -111,6 +105,16 @@ func checkEmptyResponseExpected(res []byte, status string, err error) error {
return nil
}
func unmarshalResponseJSON(res []byte, status string, err error, v interface{}) error {
if err != nil {
return err
}
if status != "" {
return fmt.Errorf("error status: %s", status)
}
return json.Unmarshal(res, v)
}
// modeString converts a unix-style mode value to a string-ified version in an
// octal representation (e.g. "777", "700", etc). This format is expected by
// some of the ceph JSON command inputs.