當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Mount.ReadOnly方法代碼示例

本文整理匯總了Golang中github.com/docker/engine-api/types/swarm.Mount.ReadOnly方法的典型用法代碼示例。如果您正苦於以下問題:Golang Mount.ReadOnly方法的具體用法?Golang Mount.ReadOnly怎麽用?Golang Mount.ReadOnly使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/docker/engine-api/types/swarm.Mount的用法示例。


在下文中一共展示了Mount.ReadOnly方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: Set

// Set a new mount value
func (m *MountOpt) Set(value string) error {
	csvReader := csv.NewReader(strings.NewReader(value))
	fields, err := csvReader.Read()
	if err != nil {
		return err
	}

	mount := swarm.Mount{}

	volumeOptions := func() *swarm.VolumeOptions {
		if mount.VolumeOptions == nil {
			mount.VolumeOptions = &swarm.VolumeOptions{
				Labels: make(map[string]string),
			}
		}
		if mount.VolumeOptions.DriverConfig == nil {
			mount.VolumeOptions.DriverConfig = &swarm.Driver{}
		}
		return mount.VolumeOptions
	}

	bindOptions := func() *swarm.BindOptions {
		if mount.BindOptions == nil {
			mount.BindOptions = new(swarm.BindOptions)
		}
		return mount.BindOptions
	}

	setValueOnMap := func(target map[string]string, value string) {
		parts := strings.SplitN(value, "=", 2)
		if len(parts) == 1 {
			target[value] = ""
		} else {
			target[parts[0]] = parts[1]
		}
	}

	// Set writable as the default
	for _, field := range fields {
		parts := strings.SplitN(field, "=", 2)
		if len(parts) == 1 && strings.ToLower(parts[0]) == "readonly" {
			mount.ReadOnly = true
			continue
		}

		if len(parts) == 1 && strings.ToLower(parts[0]) == "volume-nocopy" {
			volumeOptions().NoCopy = true
			continue
		}

		if len(parts) != 2 {
			return fmt.Errorf("invalid field '%s' must be a key=value pair", field)
		}

		key, value := parts[0], parts[1]
		switch strings.ToLower(key) {
		case "type":
			mount.Type = swarm.MountType(strings.ToUpper(value))
		case "source":
			mount.Source = value
		case "target":
			mount.Target = value
		case "readonly":
			ro, err := strconv.ParseBool(value)
			if err != nil {
				return fmt.Errorf("invalid value for readonly: %s", value)
			}
			mount.ReadOnly = ro
		case "bind-propagation":
			bindOptions().Propagation = swarm.MountPropagation(strings.ToUpper(value))
		case "volume-nocopy":
			volumeOptions().NoCopy, err = strconv.ParseBool(value)
			if err != nil {
				return fmt.Errorf("invalid value for populate: %s", value)
			}
		case "volume-label":
			setValueOnMap(volumeOptions().Labels, value)
		case "volume-driver":
			volumeOptions().DriverConfig.Name = value
		case "volume-driver-opt":
			if volumeOptions().DriverConfig.Options == nil {
				volumeOptions().DriverConfig.Options = make(map[string]string)
			}
			setValueOnMap(volumeOptions().DriverConfig.Options, value)
		default:
			return fmt.Errorf("unexpected key '%s' in '%s'", key, field)
		}
	}

	if mount.Type == "" {
		return fmt.Errorf("type is required")
	}

	if mount.Target == "" {
		return fmt.Errorf("target is required")
	}

	if mount.VolumeOptions != nil && mount.Source == "" {
		return fmt.Errorf("source is required when specifying volume-* options")
//.........這裏部分代碼省略.........
開發者ID:CrocdileChan,項目名稱:docker,代碼行數:101,代碼來源:opts.go


注:本文中的github.com/docker/engine-api/types/swarm.Mount.ReadOnly方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。