2014-09-05 12:47:20 -04:00
|
|
|
package googlecompute
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2015-10-08 00:36:31 -04:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2014-09-05 12:47:20 -04:00
|
|
|
"os"
|
2015-10-08 00:36:31 -04:00
|
|
|
"strings"
|
2014-09-05 12:47:20 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// accountFile represents the structure of the account file JSON file.
|
2016-07-07 17:50:46 -04:00
|
|
|
type AccountFile struct {
|
2014-09-05 12:47:20 -04:00
|
|
|
PrivateKeyId string `json:"private_key_id"`
|
|
|
|
PrivateKey string `json:"private_key"`
|
|
|
|
ClientEmail string `json:"client_email"`
|
|
|
|
ClientId string `json:"client_id"`
|
|
|
|
}
|
|
|
|
|
2015-10-08 00:36:31 -04:00
|
|
|
func parseJSON(result interface{}, text string) error {
|
|
|
|
r := strings.NewReader(text)
|
|
|
|
dec := json.NewDecoder(r)
|
|
|
|
return dec.Decode(result)
|
|
|
|
}
|
|
|
|
|
2016-07-07 17:50:46 -04:00
|
|
|
func ProcessAccountFile(account_file *AccountFile, text string) error {
|
2015-10-08 00:36:31 -04:00
|
|
|
// Assume text is a JSON string
|
|
|
|
if err := parseJSON(account_file, text); err != nil {
|
|
|
|
// If text was not JSON, assume it is a file path instead
|
|
|
|
if _, err := os.Stat(text); os.IsNotExist(err) {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"account_file path does not exist: %s",
|
|
|
|
text)
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := ioutil.ReadFile(text)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error reading account_file from path '%s': %s",
|
|
|
|
text, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
contents := string(b)
|
|
|
|
|
|
|
|
if err := parseJSON(account_file, contents); err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error parsing account file '%s': %s",
|
|
|
|
contents, err)
|
|
|
|
}
|
2014-09-05 12:47:20 -04:00
|
|
|
}
|
|
|
|
|
2015-10-08 00:36:31 -04:00
|
|
|
return nil
|
2014-09-05 12:47:20 -04:00
|
|
|
}
|