func runRmtlvsCmd()

in cli/image_cmds.go [214:266]


func runRmtlvsCmd(cmd *cobra.Command, args []string) {
	if len(args) < 2 {
		ImgmodUsage(cmd, nil)
	}

	inFilename := args[0]
	outFilename, err := CalcOutFilename(inFilename)
	if err != nil {
		ImgmodUsage(cmd, err)
	}

	img, err := readImage(inFilename)
	if err != nil {
		ImgmodUsage(cmd, err)
	}

	tlvIndices := []int{}
	idxMap := map[int]struct{}{}
	for _, arg := range args[1:] {
		idx, err := strconv.Atoi(arg)
		if err != nil {
			ImgmodUsage(cmd, errors.Errorf("invalid TLV index: %s", arg))
		}

		if idx < 0 || idx >= len(img.Tlvs) {
			ImgmodUsage(nil, errors.Errorf(
				"TLV index %s out of range; "+
					"must be in range [0, %d] for this image",
				arg, len(img.Tlvs)-1))
		}

		if _, ok := idxMap[idx]; ok {
			ImgmodUsage(nil, errors.Errorf(
				"TLV index %d specified more than once", idx))
		}
		idxMap[idx] = struct{}{}

		tlvIndices = append(tlvIndices, idx)
	}

	// Remove TLVs in reverse order to preserve index mapping.
	sort.Sort(sort.Reverse(sort.IntSlice(tlvIndices)))
	for _, idx := range tlvIndices {
		tlv := img.Tlvs[idx]
		iutil.Printf("Removing TLV%d: %s\n", idx, tlvStr(tlv))

		img.Tlvs = append(img.Tlvs[0:idx], img.Tlvs[idx+1:]...)
	}

	if err := writeImage(img, outFilename); err != nil {
		ImgmodUsage(nil, err)
	}
}