Beispiel #1
0
// If the user has specified a driver, they should not see the flags for all
// of the drivers in `docker-machine create`.  This method replaces the 100+
// create flags with only the ones applicable to the driver specified
func trimDriverFlags(driver string, cmds []cli.Command) ([]cli.Command, error) {
	filteredCmds := cmds
	driverFlags, err := drivers.GetCreateFlagsForDriver(driver)
	if err != nil {
		return nil, err
	}

	for i, cmd := range cmds {
		if cmd.HasName("create") {
			filteredCmds[i].Flags = append(driverFlags, sharedCreateFlags...)
		}
	}

	return filteredCmds, nil
}
func fetchData() (*helpers.ResourceData, error) {
	blacklist := loadBlacklist()
	resourceMap := make(map[string]helpers.ResourceFields)
	documentationMap := make(map[string][]helpers.DocumentationFields)
	for _, driver := range drivers.GetDriverNames() {
		resourceFieldStruct := make(helpers.ResourceFields)
		resourceFieldMap := make(helpers.ResourceFieldConfigs)
		resourceFieldStruct["resourceFields"] = resourceFieldMap
		resourceMap[driver] = resourceFieldStruct
		documentationFieldStruct := new(helpers.DocumentationFields)
		documentationFieldMap := make(helpers.DocumentationFieldConfigs)
		documentationFieldStruct.ResourceFields = documentationFieldMap
		documentationFieldStruct.Id = driver + "Config"
		documentationMap[driver] = []helpers.DocumentationFields{*documentationFieldStruct}
		createFlags, err := drivers.GetCreateFlagsForDriver(driver)
		if err != nil {
			return nil, fmt.Errorf("error getting create flags for driver=%s, err=%v", driver, err)
		}
		for _, flagStruct := range createFlags {
			var flagName, flagType, description string
			var err error
			switch flagStruct.(type) {
			case cli.StringFlag:
				flag := flagStruct.(cli.StringFlag)
				flagName, err = getRancherName(flag.Name)
				if err != nil {
					return nil, fmt.Errorf("error getting the rancher name of flagStruct=%v for driver=%s, err=%v", flagStruct, driver, err)
				}
				flagType = "string"
				description = flag.Usage
			case cli.IntFlag:
				flag := flagStruct.(cli.IntFlag)
				flagName, err = getRancherName(flag.Name)
				if err != nil {
					return nil, fmt.Errorf("error getting the rancher name of flagStruct=%v for driver=%s, err=%v", flagStruct, driver, err)
				}
				flagType = "string"
				description = flag.Usage
			case cli.BoolFlag:
				flag := flagStruct.(cli.BoolFlag)
				flagName, err = getRancherName(flag.Name)
				if err != nil {
					return nil, fmt.Errorf("error getting the rancher name of flagStruct=%v for driver=%s, err=%v", flagStruct, driver, err)
				}
				flagType = "boolean"
				description = flag.Usage
			case cli.BoolTFlag:
				flag := flagStruct.(cli.BoolTFlag)
				flagName, err = getRancherName(flag.Name)
				if err != nil {
					return nil, fmt.Errorf("error getting the rancher name of flagStruct=%v for driver=%s, err=%v", flagStruct, driver, err)
				}
				flagType = "boolean"
				description = flag.Usage
			case cli.StringSliceFlag:
				flag := flagStruct.(cli.StringSliceFlag)
				flagName, err = getRancherName(flag.Name)
				if err != nil {
					return nil, fmt.Errorf("error getting the rancher name of flagStruct=%v for driver=%s, err=%v", flagStruct, driver, err)
				}
				flagType = "array[string]"
				description = flag.Usage
			default:
				return nil, fmt.Errorf("unknown type of flag %v, for driver=%s", flagStruct, driver)
			}
			documentationFieldMap[flagName] = helpers.DocumentationFieldConfig{Description: description}
			resourceFieldMap[flagName] = helpers.ResourceFieldConfig{Type: flagType, Nullable: true, Required: false}
		}
	}
	return &helpers.ResourceData{Blacklist: blacklist, Drivers: drivers.GetDriverNames(), ResourceMap: resourceMap, DocumentationMap: documentationMap}, nil
}