Ejemplo n.º 1
0
func before(c *cli.Context) error {
	initLogFormatter()

	// Log level
	logLevelStr := c.String(LogLevelKey)
	if logLevelStr == "" {
		logLevelStr = "info"
	}

	level, err := log.ParseLevel(logLevelStr)
	if err != nil {
		return err
	}
	log.SetLevel(level)

	// Command Host
	commandHostStr := c.String(CommandHostKey)
	if commandHostStr != "" {
		switch commandHostStr {
		case bridge.CommandHostIDCmdBridge, bridge.CommandHostIDNone, bridge.CommandHostIDDocker:
			CommandHostID = commandHostStr
		default:
			log.Fatalf("Invalid / not supported command-host specified: %s", commandHostStr)
		}
	}
	log.Debugf("Command host: %s", CommandHostID)

	// Load config file, if any
	configFilePath := config.DefaultConfigFilePath()
	if exist, err := pathutil.IsPathExists(configFilePath); err != nil {
		log.Fatalf("Failed to check config file: %s", err)
	} else if exist {
		log.Debugf("Using config found at path: %s", configFilePath)
		conf, err := config.LoadConfigFromFile(configFilePath)
		if err != nil {
			log.Fatalf("Failed to read config file: %s", err)
		}
		BridgeConfigs = conf
	}

	// Command Host Args
	if CommandHostID == bridge.CommandHostIDDocker {
		commandHostDockerImage := c.String(DockerImageIDNameKey)
		if commandHostDockerImage != "" {
			BridgeConfigs.Docker.Image = commandHostDockerImage
		}
		commandHostDockerAllowAccessToDockerInContainer := c.String(DockerAllowAccessToDockerInContainer)
		if commandHostDockerAllowAccessToDockerInContainer != "" {
			val, err := parseutil.ParseBool(commandHostDockerAllowAccessToDockerInContainer)
			if err != nil {
				log.Warnf("Invalid parameter 'docker-allow-access-to-docker-in-container': %s", commandHostDockerAllowAccessToDockerInContainer)
				log.Warn("=> Ignoring the parameter")
			} else {
				BridgeConfigs.Docker.IsAllowAccessToDockerInContainer = val
			}
		}
	}
	log.Debugf("Configs: %#v", BridgeConfigs)

	return nil
}
Ejemplo n.º 2
0
// ParseFromInterfaceMap ...
func (envSerModel *EnvironmentItemOptionsModel) ParseFromInterfaceMap(input map[string]interface{}) error {
	for keyStr, value := range input {
		log.Debugf("  ** processing (key:%#v) (value:%#v) (envSerModel:%#v)", keyStr, value, envSerModel)
		switch keyStr {
		case "title":
			castedValue, ok := value.(string)
			if !ok {
				return fmt.Errorf("Invalid value type (key:%s): %#v", keyStr, value)
			}
			envSerModel.Title = pointers.NewStringPtr(castedValue)
		case "description":
			castedValue, ok := value.(string)
			if !ok {
				return fmt.Errorf("Invalid value type (key:%s): %#v", keyStr, value)
			}
			envSerModel.Description = pointers.NewStringPtr(castedValue)
		case "summary":
			castedValue, ok := value.(string)
			if !ok {
				return fmt.Errorf("Invalid value type (key:%s): %#v", keyStr, value)
			}
			envSerModel.Summary = pointers.NewStringPtr(castedValue)
		case "value_options":
			castedValue, ok := value.([]string)
			if !ok {
				// try with []interface{} instead and cast the
				//  items to string
				castedValue = []string{}
				interfArr, ok := value.([]interface{})
				if !ok {
					return fmt.Errorf("Invalid value type (key:%s): %#v", keyStr, value)
				}
				for _, interfItm := range interfArr {
					castedItm, ok := interfItm.(string)
					if !ok {
						castedItm = parseutil.CastToString(interfItm)
						if castedItm == "" {
							return fmt.Errorf("Invalid value in value_options (%#v), not a string: %#v", interfArr, interfItm)
						}
					}
					castedValue = append(castedValue, castedItm)
				}
			}
			envSerModel.ValueOptions = castedValue
		case "is_required":
			castedValue, ok := value.(bool)
			if !ok {
				castedStr := parseutil.CastToString(value)
				if castedStr != "" {
					casted, err := parseutil.ParseBool(castedStr)
					if err != nil {
						return fmt.Errorf("Faild to parse bool (key:%s): %#v, err: %s", keyStr, value, err)
					}
					castedValue = casted
				} else {
					return fmt.Errorf("Invalid value type (key:%s): %#v", keyStr, value)
				}
			}
			envSerModel.IsRequired = pointers.NewBoolPtr(castedValue)
		case "is_expand":
			castedValue, ok := value.(bool)
			if !ok {
				castedStr := parseutil.CastToString(value)
				if castedStr != "" {
					casted, err := parseutil.ParseBool(castedStr)
					if err != nil {
						return fmt.Errorf("Faild to parse bool (key:%s): %#v, err: %s", keyStr, value, err)
					}
					castedValue = casted
				} else {
					return fmt.Errorf("Invalid value type (key:%s): %#v", keyStr, value)
				}
			}
			envSerModel.IsExpand = pointers.NewBoolPtr(castedValue)
		case "is_dont_change_value":
			castedValue, ok := value.(bool)
			if !ok {
				castedStr := parseutil.CastToString(value)
				if castedStr != "" {
					casted, err := parseutil.ParseBool(castedStr)
					if err != nil {
						return fmt.Errorf("Faild to parse bool (key:%s): %#v, err: %s", keyStr, value, err)
					}
					castedValue = casted
				} else {
					return fmt.Errorf("Invalid value type (key:%s): %#v", keyStr, value)
				}
			}
			envSerModel.IsDontChangeValue = pointers.NewBoolPtr(castedValue)
		case "is_template":
			castedValue, ok := value.(bool)
			if !ok {
				castedStr := parseutil.CastToString(value)
				if castedStr != "" {
					casted, err := parseutil.ParseBool(castedStr)
					if err != nil {
						return fmt.Errorf("Faild to parse bool (key:%s): %#v, err: %s", keyStr, value, err)
					}
					castedValue = casted
				} else {
					return fmt.Errorf("Invalid value type (key:%s): %#v", keyStr, value)
				}
			}
			envSerModel.IsTemplate = pointers.NewBoolPtr(castedValue)
		default:
			return fmt.Errorf("Not supported key found in options: %#v", keyStr)
		}
	}
	return nil
}