github.com/opentofu/opentofu@v1.7.1/internal/command/flag_kv.go (about)

     1  // Copyright (c) The OpenTofu Authors
     2  // SPDX-License-Identifier: MPL-2.0
     3  // Copyright (c) 2023 HashiCorp, Inc.
     4  // SPDX-License-Identifier: MPL-2.0
     5  
     6  package command
     7  
     8  import (
     9  	"fmt"
    10  	"strings"
    11  )
    12  
    13  // FlagStringKV is a flag.Value implementation for parsing user variables
    14  // from the command-line in the format of '-var key=value', where value is
    15  // only ever a primitive.
    16  type FlagStringKV map[string]string
    17  
    18  func (v *FlagStringKV) String() string {
    19  	return ""
    20  }
    21  
    22  func (v *FlagStringKV) Set(raw string) error {
    23  	idx := strings.Index(raw, "=")
    24  	if idx == -1 {
    25  		return fmt.Errorf("No '=' value in arg: %s", raw)
    26  	}
    27  
    28  	if *v == nil {
    29  		*v = make(map[string]string)
    30  	}
    31  
    32  	key, value := raw[0:idx], raw[idx+1:]
    33  	(*v)[key] = value
    34  	return nil
    35  }
    36  
    37  // FlagStringSlice is a flag.Value implementation for parsing targets from the
    38  // command line, e.g. -target=aws_instance.foo -target=aws_vpc.bar
    39  type FlagStringSlice []string
    40  
    41  func (v *FlagStringSlice) String() string {
    42  	return ""
    43  }
    44  func (v *FlagStringSlice) Set(raw string) error {
    45  	*v = append(*v, raw)
    46  
    47  	return nil
    48  }