Example #1
0
// queryRegion asks the user to pick a region of the ones passed in. The first
// region in the list will be the default.
func queryRegion(cloud string, regions []jujucloud.Region, scanner *bufio.Scanner, w io.Writer) (string, error) {
	fmt.Fprintf(w, "Regions in %s:\n", cloud)
	names := jujucloud.RegionNames(regions)
	// add an empty string to allow for a default value. Also gives us an extra
	// line return after the list of names.
	names = append(names, "")
	if _, err := fmt.Fprintln(w, strings.Join(names, "\n")); err != nil {
		return "", errors.Trace(err)
	}
	verify := interact.MatchOptions(names, errors.Errorf("Invalid region."))
	defaultRegion := regions[0].Name
	query := fmt.Sprintf("Select a region in %s [%s]: ", cloud, defaultRegion)
	region, err := interact.QueryVerify([]byte(query), scanner, w, verify)
	if err != nil {
		return "", errors.Trace(err)
	}
	if region == "" {
		return defaultRegion, nil
	}
	regionName, ok := interact.FindMatch(region, names)
	if !ok {
		// should be impossible
		return "", errors.Errorf("invalid region name chosen: %s", region)
	}

	return regionName, nil
}
Example #2
0
// queryCloud asks the user to choose a cloud.
func queryCloud(clouds []string, defCloud string, scanner *bufio.Scanner, w io.Writer) (string, error) {
	list := strings.Join(clouds, "\n")
	if _, err := fmt.Fprint(w, "Clouds\n", list, "\n\n"); err != nil {
		return "", errors.Trace(err)
	}

	// add support for a default (empty) selection.
	clouds = append(clouds, "")

	verify := interact.MatchOptions(clouds, errors.Errorf("Invalid cloud."))

	query := fmt.Sprintf("Select a cloud [%s]: ", defCloud)
	cloud, err := interact.QueryVerify([]byte(query), scanner, w, verify)
	if err != nil {
		return "", errors.Trace(err)
	}
	if cloud == "" {
		return defCloud, nil
	}

	cloudName, ok := interact.FindMatch(cloud, clouds)
	if !ok {
		// should be impossible
		return "", errors.Errorf("invalid cloud name chosen: %s", cloud)
	}

	return cloudName, nil
}