|
|
|
|
@ -2,7 +2,10 @@ package googlecompute
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"os"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// accountFile represents the structure of the account file JSON file.
|
|
|
|
|
@ -13,13 +16,37 @@ type accountFile struct {
|
|
|
|
|
ClientId string `json:"client_id"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func loadJSON(result interface{}, path string) error {
|
|
|
|
|
f, err := os.Open(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
func parseJSON(result interface{}, text string) error {
|
|
|
|
|
r := strings.NewReader(text)
|
|
|
|
|
dec := json.NewDecoder(r)
|
|
|
|
|
return dec.Decode(result)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func processAccountFile(account_file *accountFile, text string) error {
|
|
|
|
|
// 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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
|
|
dec := json.NewDecoder(f)
|
|
|
|
|
return dec.Decode(result)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|