1
0
mirror of https://github.com/gluster/glusterd2.git synced 2026-02-05 12:45:38 +01:00
Files
glusterd2/glustercli/cmd/volume-set.go
Prashanth Pai edda2c5063 Refactor cluster options package
* Merge 'glusterd2/cluster' package into `glusterd2/options` package.
* Rename 'glusterd2/commands/global' to 'glusterd2/commands/options'.

Signed-off-by: Prashanth Pai <ppai@redhat.com>
2018-10-11 16:26:30 +05:30

91 lines
2.2 KiB
Go

package cmd
import (
"errors"
"fmt"
"github.com/gluster/glusterd2/pkg/api"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
const (
volumeSetCmdHelpShort = "Set volume options"
volumeSetCmdHelpLong = "Set options on a specified gluster volume. Needs a volume name and at least one option-value pair."
)
var (
flagSetAdv, flagSetExp, flagSetDep bool
volumeSetCmd = &cobra.Command{
Use: "set <volname> <option> <value> [<option> <value>]...",
Short: volumeSetCmdHelpShort,
Long: volumeSetCmdHelpLong,
Args: volumeSetCmdArgs,
Run: volumeSetCmdRun,
}
)
func init() {
volumeSetCmd.Flags().BoolVar(&flagSetAdv, "advanced", false, "Allow setting advanced options")
volumeSetCmd.Flags().BoolVar(&flagSetExp, "experimental", false, "Allow setting experimental options")
volumeSetCmd.Flags().BoolVar(&flagSetDep, "deprecated", false, "Allow setting deprecated options")
volumeCmd.AddCommand(volumeSetCmd)
}
func volumeSetCmdArgs(cmd *cobra.Command, args []string) error {
// Ensure we have enough arguments for the command
if len(args) < 3 {
return errors.New("need at least 3 arguments")
}
// Ensure we have a proper option-value pairs
if (len(args)-1)%2 != 0 {
return errors.New("needs '<option> <value>' to be in pairs")
}
return nil
}
func volumeSetCmdRun(cmd *cobra.Command, args []string) {
volname := args[0]
options := args[1:]
if err := volumeOptionJSONHandler(cmd, volname, options); err != nil {
if GlobalFlag.Verbose {
log.WithError(err).WithField(
"volume", volname).Error("volume option set failed")
}
failure("Volume option set failed", err, 1)
} else {
fmt.Printf("Options set successfully for %s volume\n", volname)
}
}
func volumeOptionJSONHandler(cmd *cobra.Command, volname string, options []string) error {
vopt := make(map[string]string)
for op, val := range options {
if op%2 == 0 {
vopt[val] = options[op+1]
}
}
if volname == "all" {
err := client.ClusterOptionSet(api.ClusterOptionReq{
Options: vopt,
})
return err
}
err := client.VolumeSet(volname, api.VolOptionReq{
Options: vopt,
VolOptionFlags: api.VolOptionFlags{
AllowAdvanced: flagSetAdv,
AllowExperimental: flagSetExp,
AllowDeprecated: flagSetDep,
},
})
return err
}