コード例 #1
0
func (config FlagsConfig) ApplyTo(cmd string, c *cli.Context) error {
	entry := config[""]
	if cmd != "" {
		entry.Apply(config[cmd])
	}
	vars := entry.Vars()
	if cmd == "" {
		vars["local"] = vars["global"]
		vars["global"] = nil
	}
	for key, val := range vars["global"] {
		if !c.GlobalIsSet(key) {
			switch val.(type) {
			case string:
				strVal := val.(string)
				if strVal == "" {
					continue
				}
				c.GlobalSet(key, strVal)
			case []string:
				strSlice := val.([]string)
				if len(strSlice) == 0 {
					continue
				}
				for _, valBit := range strSlice {
					c.GlobalSet(key, valBit)
				}
			default:
				return fmt.Errorf(`unknown value type %T (%s: %+v)`, val, key, val)
			}
		}
	}
	for key, val := range vars["local"] {
		if !c.IsSet(key) {
			switch val.(type) {
			case string:
				strVal := val.(string)
				if strVal == "" {
					continue
				}
				c.Set(key, strVal)
			case []string:
				strSlice := val.([]string)
				if len(strSlice) == 0 {
					continue
				}
				for _, valBit := range strSlice {
					c.Set(key, valBit)
				}
			default:
				return fmt.Errorf(`unknown value type %T (%s: %+v)`, val, key, val)
			}
		}
	}
	return nil
}