-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
52 lines (43 loc) · 1.21 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package bigquery
import (
"encoding/base64"
"errors"
"fmt"
"os"
"golang.org/x/oauth2/google"
)
type Config struct {
ProjectID string `envconfig:"BIGQUERY_PROJECT"`
CredentialsFilePath string `envconfig:"BIGQUERY_CREDENTIALS_FILE"`
CredentialsJSON string
Credentials *google.Credentials
Location string `envconfig:"BIGQUERY_LOCATION"`
}
func (c Config) IsValid() bool {
return c.ProjectID != "" && c.CredentialsFilePath != ""
}
func (c Config) GetConnectionURI() (string, error) {
var creds []byte
switch {
case c.CredentialsJSON != "":
creds = []byte(c.CredentialsJSON)
case c.CredentialsFilePath != "":
var err error
creds, err = os.ReadFile(c.CredentialsFilePath)
if err != nil {
return "", err
}
case c.Credentials != nil:
return "", errors.New("only `service_account_json` or `service_account_file` supported")
default:
return "", errors.New("could not find google credentials")
}
URI := fmt.Sprintf("bigquery://%s?credentials_base64=%s", c.ProjectID, base64.StdEncoding.EncodeToString(creds))
if c.Location != "" {
URI += "&location=" + c.Location
}
return URI, nil
}
func (c Config) GetIngestrURI() (string, error) {
return c.GetConnectionURI()
}