github.com/marksheahan/packer@v0.10.2-0.20160613200515-1acb2d6645a0/helper/flag-kv/flag_json.go (about)

     1  package kvflag
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"os"
     7  )
     8  
     9  // FlagJSON is a flag.Value implementation for parsing user variables
    10  // from the command-line using JSON files.
    11  type FlagJSON map[string]string
    12  
    13  func (v *FlagJSON) String() string {
    14  	return ""
    15  }
    16  
    17  func (v *FlagJSON) Set(raw string) error {
    18  	f, err := os.Open(raw)
    19  	if err != nil {
    20  		return err
    21  	}
    22  	defer f.Close()
    23  
    24  	if *v == nil {
    25  		*v = make(map[string]string)
    26  	}
    27  
    28  	if err := json.NewDecoder(f).Decode(v); err != nil {
    29  		return fmt.Errorf(
    30  			"Error reading variables in '%s': %s", raw, err)
    31  	}
    32  
    33  	return nil
    34  }