Example #1
0
// Takes positional command arguments and sets options from them (because some may be optional)
func setOptsFromArgs(args []string) {
	// The user may have set a config name in a `.discfg` file, for convenience, to shorten the commands.
	// This will affect the positional arguments. The confusing part will be if a config name has been
	// set and then the user forgets and puts the config name in the positional arguments. To avoid
	// this, a check against the first argument and the config name is made...But that means setting
	// a key name the same as the config name requires 3 positional arguments.
	// I'm beginning to wonder if pulling this out was even worthwhile since some of it also depends
	// on the actual command.
	name := commands.GetDiscfgNameFromFile()
	if name != "" {
		Options.CfgName = name
	}

	switch len(args) {
	case 1:
		if Options.CfgName != "" && args[0] != Options.CfgName {
			Options.Key = args[0]
		} else {
			Options.CfgName = args[0]
		}
		break
	case 2:
		if Options.CfgName != "" && args[0] != Options.CfgName {
			Options.Key = args[0]
			Options.Value = []byte(args[1])
		} else {
			Options.CfgName = args[0]
			Options.Key = args[1]
		}
		break
	case 3:
		// 3 args always means a CfgName was passed. It couldn't mean anything else at this time.
		Options.CfgName = args[0]
		Options.Key = args[1]
		Options.Value = []byte(args[2])
		break
	}

	// A data file will overwrite Options.Value, even if set. Prefer the data file (if it can be read)
	// if both a value command line argument and a file path are specified.
	if dataFile != "" {
		b, err := ioutil.ReadFile(dataFile)
		if err == nil {
			Options.Value = b
		}
	}
}
Example #2
0
			input, _ := inputReader.ReadString('\n')
			if input != "Y\n" {
				DiscfgCmd.Println("Aborted")
				return
			}
		}
		resp := commands.DeleteCfg(Options)
		commands.Out(Options, resp)
	},
}
var updateCfgCmd = &cobra.Command{
	Use:   "update",
	Short: "update config storage settings",
	Long:  `Adjusts options for a config's storage engine`,
	Run: func(cmd *cobra.Command, args []string) {
		name := commands.GetDiscfgNameFromFile()
		Options.CfgName = name
		var settings map[string]interface{}

		switch len(args) {
		case 1:
			if err := json.Unmarshal([]byte(args[0]), &settings); err != nil {
				commands.Out(Options, config.ResponseObject{Action: "update", Error: err.Error()})
			}
			break
		case 2:
			Options.CfgName = args[0]
			if err := json.Unmarshal([]byte(args[0]), &settings); err != nil {
				commands.Out(Options, config.ResponseObject{Action: "update", Error: err.Error()})
			}
		}