Beispiel #1
0
func verifyAllowedSlice(slice []string) error {
	for i, v := range slice {
		if _, err := strconv.ParseUint(v, 10, 32); err != nil {
			// can fail which means it needs to be a label
			labels.ParseLabel(v)
		} else if i != 0 {
			return fmt.Errorf("value %q: must be only one unsigned "+
				"number or label(s) in format of SOURCE:KEY[=VALUE]", v)
		}
	}
	return nil
}
Beispiel #2
0
func parseAllowedSlice(slice []string) ([]labels.Label, error) {
	inLabels := []labels.Label{}
	id := uint32(0)

	for _, v := range slice {
		if n, err := strconv.ParseUint(v, 10, 32); err != nil {
			// can fail which means it needs to be a label
			lbl := labels.ParseLabel(v)
			inLabels = append(inLabels, *lbl)
		} else {
			if id != 0 {
				return nil, fmt.Errorf("More than one security ID provided")
			}

			id = uint32(n)
		}
	}

	if id != 0 {
		if len(inLabels) > 0 {
			return nil, fmt.Errorf("You can only specify either ID or labels")
		}

		ctx, err := client.GetLabels(id)
		if err != nil {
			return nil, fmt.Errorf("Unable to retrieve labels for ID %d: %s", id, err)
		}
		if ctx == nil {
			return nil, fmt.Errorf("ID %d not found", id)
		}

		return ctx.Labels.ToSlice(), nil
	} else {
		if len(inLabels) == 0 {
			return nil, fmt.Errorf("No label or security ID provided")
		}

		return inLabels, nil
	}
}