func AppendInstances()

in pkg/table/table.go [177:228]


func AppendInstances(data [][]string, indexedOptions []string, instances []*ec2.Instance,
	addedInstanceIds []string) ([][]string, []string, int) {
	counter := 0
	for _, instance := range instances {
		if addedInstanceIds != nil {
			// If this instance is already added, just don't display it here
			isFound := false
			for _, addedInstanceId := range addedInstanceIds {
				if addedInstanceId == *instance.InstanceId {
					isFound = true
					break
				}
			}
			if isFound {
				continue
			}
		}

		instanceName := *instance.InstanceId
		instanceTagName := ec2helper.GetTagName(instance.Tags)
		if instanceTagName != nil {
			instanceName = fmt.Sprintf("%s(%s)", *instanceTagName, *instance.InstanceId)
		}
		firstRow := []string{fmt.Sprintf("%d.", counter+1), instanceName, "", ""}
		indexedOptions = append(indexedOptions, *instance.InstanceId)
		counter++

		// Extract all tags that are not Name
		displayTags := []*ec2.Tag{}
		for _, tag := range instance.Tags {
			if *tag.Key != "Name" {
				displayTags = append(displayTags, tag)
			}
		}

		// Append the first tag, if applicable
		if len(displayTags) > 0 {
			firstRow[2] = *displayTags[0].Key
			firstRow[3] = *displayTags[0].Value
		}

		// Append the main row
		data = append(data, firstRow)

		// Append subrows, if applicable
		for i := 1; i < len(displayTags); i++ {
			data = append(data, []string{"", "", *displayTags[i].Key, *displayTags[i].Value})
		}
	}

	return data, indexedOptions, counter
}