forked from azer/go-flickr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
42 lines (37 loc) · 912 Bytes
/
utils.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
package flickr
import (
"errors"
"fmt"
"log"
"os"
"github.com/joho/godotenv"
)
var ErrKeyNotInEnv = errors.New("no API Key in Environment")
func getApiKey(key, envFileName string) (string, error) {
if key != "" {
return key, nil
}
/* Check specified env file */
if envFileName != "" {
if _, envVarExists := os.LookupEnv(ApiKeyEnvVar); envVarExists {
log.Printf("Value for env var %s already exists, but asking to read file %s. File will not override", ApiKeyEnvVar, envFileName)
}
err := godotenv.Load(envFileName)
fmt.Printf("Attempting to load env file %s\n", envFileName)
if err != nil {
return "", err
}
} else {
/* Check for ./.env file */
err := godotenv.Load()
_, ok := err.(*os.PathError)
if err != nil && !ok {
return "", err
}
}
if key, ok := os.LookupEnv(ApiKeyEnvVar); ok {
return key, nil
}
fmt.Println("No key!!")
return "", ErrKeyNotInEnv
}