func AskImage()

in pkg/question/question.go [426:508]


func AskImage(h *ec2helper.EC2Helper, instanceType string) (*ec2.Image, error) {
	// get info about the instance type
	instanceTypeInfo, err := h.GetInstanceType(instanceType)
	if err != nil {
		return nil, err
	}

	// Use instance-store if supported
	rootDeviceType := "ebs"
	if *instanceTypeInfo.InstanceStorageSupported {
		rootDeviceType = "instance-store"
	}

	s := spinner.New(spinner.CharSets[11], 100*time.Millisecond)
	s.Suffix = " fetching images"
	s.Color("blue", "bold")
	s.Start()
	defaultImages, err := h.GetLatestImages(&rootDeviceType)
	if err != nil {
		return nil, err
	}
	s.Stop()

	data := [][]string{}
	indexedOptions := []string{}

	var defaultImageRepr, defaultImageId, optionsText string
	if defaultImages != nil && len(*defaultImages) > 0 {
		// Pick the available image with the highest priority as the default choice
		priority := ec2helper.GetImagePriority()
		for _, osName := range priority {
			image, found := (*defaultImages)[osName]
			if found {
				defaultImageRepr = fmt.Sprintf("Latest %s image", osName)
				defaultImageId = *image.ImageId
				break
			}
		}

		// Add all default images to indexed options, with priority
		counter := 0
		for _, osName := range priority {
			image, found := (*defaultImages)[osName]
			if found {
				indexedOptions = append(indexedOptions, *image.ImageId)
				data = append(data, []string{fmt.Sprintf("%d.", counter+1), osName, *image.ImageId,
					*image.CreationDate})
				counter++
			}
		}

		optionsText = table.BuildTable(data, []string{"Option", "Operating System", "Image ID",
			"Creation Date"})
	} else {
		optionsText = "No default images available\n"
	}

	// Add the option to enter an image id
	optionsText += "[ any image id ]: Select the image id\n"

	question := "AMI"

	answer := AskQuestion(&AskQuestionInput{
		QuestionString:    question,
		DefaultOptionRepr: &defaultImageRepr,
		DefaultOption:     &defaultImageId,
		OptionsString:     &optionsText,
		IndexedOptions:    indexedOptions,
		EC2Helper:         h,
		Fns:               []CheckInput{ec2helper.ValidateImageId},
	})

	// Find the image information
	if defaultImages != nil {
		for _, image := range *defaultImages {
			if *image.ImageId == answer {
				return image, nil
			}
		}
	}

	return nil, errors.New(fmt.Sprintf("No image information for %s found", answer))
}