Files
Packer-Cn/builder/googlecompute/account.go
T

35 lines
821 B
Go
Raw Normal View History

2014-09-05 09:47:20 -07:00
package googlecompute
import (
2015-10-08 00:36:31 -04:00
"fmt"
"io/ioutil"
2014-09-05 09:47:20 -07:00
"os"
"golang.org/x/oauth2/google"
"golang.org/x/oauth2/jwt"
2014-09-05 09:47:20 -07:00
)
func ProcessAccountFile(text string) (*jwt.Config, error) {
2015-10-08 00:36:31 -04:00
// Assume text is a JSON string
conf, err := google.JWTConfigFromJSON([]byte(text), DriverScopes...)
if err != nil {
2015-10-08 00:36:31 -04:00
// If text was not JSON, assume it is a file path instead
if _, err := os.Stat(text); os.IsNotExist(err) {
return nil, fmt.Errorf(
2015-10-08 00:36:31 -04:00
"account_file path does not exist: %s",
text)
}
data, err := ioutil.ReadFile(text)
2015-10-08 00:36:31 -04:00
if err != nil {
return nil, fmt.Errorf(
2015-10-08 00:36:31 -04:00
"Error reading account_file from path '%s': %s",
text, err)
}
conf, err = google.JWTConfigFromJSON(data, DriverScopes...)
if err != nil {
return nil, fmt.Errorf("Error parsing account_file: %s", err)
2015-10-08 00:36:31 -04:00
}
}
return conf, nil
2015-10-08 00:36:31 -04:00
}