github.phpd.cn/hashicorp/packer@v1.3.2/builder/googlecompute/account.go (about) 1 package googlecompute 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "io/ioutil" 7 "os" 8 "strings" 9 ) 10 11 // accountFile represents the structure of the account file JSON file. 12 type AccountFile struct { 13 PrivateKeyId string `json:"private_key_id"` 14 PrivateKey string `json:"private_key"` 15 ClientEmail string `json:"client_email"` 16 ClientId string `json:"client_id"` 17 } 18 19 func parseJSON(result interface{}, text string) error { 20 r := strings.NewReader(text) 21 dec := json.NewDecoder(r) 22 return dec.Decode(result) 23 } 24 25 func ProcessAccountFile(account_file *AccountFile, text string) error { 26 // Assume text is a JSON string 27 if err := parseJSON(account_file, text); err != nil { 28 // If text was not JSON, assume it is a file path instead 29 if _, err := os.Stat(text); os.IsNotExist(err) { 30 return fmt.Errorf( 31 "account_file path does not exist: %s", 32 text) 33 } 34 35 b, err := ioutil.ReadFile(text) 36 if err != nil { 37 return fmt.Errorf( 38 "Error reading account_file from path '%s': %s", 39 text, err) 40 } 41 42 contents := string(b) 43 44 if err := parseJSON(account_file, contents); err != nil { 45 return fmt.Errorf( 46 "Error parsing account file '%s': %s", 47 contents, err) 48 } 49 } 50 51 return nil 52 }