@@ -5,9 +5,11 @@ import (
5
5
"encoding/base64"
6
6
"errors"
7
7
"fmt"
8
+ "io/fs"
8
9
"log"
9
10
"net/http"
10
11
"os"
12
+ "path/filepath"
11
13
"strings"
12
14
13
15
"github.com/eat-pray-ai/yutu/pkg/utils"
21
23
errReadSecret = errors .New ("unable to read client secret" )
22
24
)
23
25
24
- const missingClientSecretsHint string = `
25
- Please configure OAuth 2.0
26
- You need to populate the client_secrets.json file
27
- found at: %s
28
- with information from the {{ Google Cloud Console }}
29
- {{ https://cloud.google.com/console }}
30
- For more information about the client_secrets.json file format, please visit:
31
- https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
32
- `
26
+ const missingClientSecretsHint string = "Please configure client secrets as described in https://github.com/eat-pray-ai/yutu#prerequisites"
33
27
34
28
type svc struct {
35
29
Cacheable bool `yaml:"cacheable" json:"cacheable"`
@@ -62,23 +56,26 @@ func NewY2BService(opts ...Option) Svc {
62
56
return s
63
57
}
64
58
65
- func WithCredential (cred string ) Option {
59
+ func WithCredential (cred string , fsys fs. FS ) Option {
66
60
return func (s * svc ) {
67
61
// cred > YUTU_CREDENTIAL
68
62
envCred , ok := os .LookupEnv ("YUTU_CREDENTIAL" )
69
63
if cred == "" && ok {
70
64
cred = envCred
71
65
} else if cred == "" {
72
- cred = "client_secret.json"
66
+ cred = credentialFile
73
67
}
74
-
75
68
// 1. cred is a file path
76
69
// 2. cred is a base64 encoded string
77
70
// 3. cred is a json string
78
- if _ , err := os .Stat (cred ); err == nil {
79
- credBytes , err := os .ReadFile (cred )
71
+ absCred , _ := filepath .Abs (cred )
72
+ relCred , _ := filepath .Rel ("/" , absCred )
73
+
74
+ if _ , err := fs .Stat (fsys , relCred ); err == nil {
75
+ // credBytes, err := yfs.ReadFile(cred)
76
+ credBytes , err := fs .ReadFile (fsys , relCred )
80
77
if err != nil {
81
- fmt .Printf (missingClientSecretsHint , cred )
78
+ fmt .Println (missingClientSecretsHint )
82
79
log .Fatalln (errors .Join (errReadSecret , err ))
83
80
}
84
81
s .Credential = string (credBytes )
@@ -90,13 +87,13 @@ func WithCredential(cred string) Option {
90
87
} else if utils .IsJson (cred ) {
91
88
s .Credential = cred
92
89
} else {
93
- fmt .Printf (missingClientSecretsHint , cred )
90
+ fmt .Println (missingClientSecretsHint )
94
91
log .Fatalln (errors .Join (errReadSecret , err ))
95
92
}
96
93
}
97
94
}
98
95
99
- func WithCacheToken (token string ) Option {
96
+ func WithCacheToken (token string , fsys fs. FS ) Option {
100
97
return func (s * svc ) {
101
98
// token > YUTU_CACHE_TOKEN
102
99
envToken , ok := os .LookupEnv ("YUTU_CACHE_TOKEN" )
@@ -109,8 +106,11 @@ func WithCacheToken(token string) Option {
109
106
// 1. token is a file path
110
107
// 2. token is a base64 encoded string
111
108
// 3. token is a json string
112
- if _ , err := os .Stat (token ); err == nil {
113
- tokenBytes , err := os .ReadFile (token )
109
+ absToken , _ := filepath .Abs (token )
110
+ relToken , _ := filepath .Rel ("/" , absToken )
111
+
112
+ if _ , err := fs .Stat (fsys , relToken ); err == nil {
113
+ tokenBytes , err := fs .ReadFile (fsys , relToken )
114
114
if err != nil {
115
115
log .Fatalln (errors .Join (errReadToken , err ))
116
116
}
0 commit comments