Skip to content

Commit 5c0a1c9

Browse files
committed
♻️ Extract fs parameter for test
1 parent 66f7e0c commit 5c0a1c9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+161
-76
lines changed

cmd/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ go_library(
1919
"Arch": "{STABLE_ARCH}",
2020
},
2121
deps = [
22+
"//pkg",
2223
"//pkg/auth",
2324
"@com_github_mark3labs_mcp_go//server",
2425
"@com_github_savioxavier_termlink//:termlink",

cmd/auth.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"github.com/eat-pray-ai/yutu/pkg"
45
"github.com/eat-pray-ai/yutu/pkg/auth"
56
"github.com/spf13/cobra"
67
)
@@ -23,8 +24,8 @@ var authCmd = &cobra.Command{
2324
Long: authLong,
2425
Run: func(cmd *cobra.Command, args []string) {
2526
auth.NewY2BService(
26-
auth.WithCredential(credential),
27-
auth.WithCacheToken(cacheToken),
27+
auth.WithCredential(credential, pkg.Fsys),
28+
auth.WithCacheToken(cacheToken, pkg.Fsys),
2829
)
2930
},
3031
}

pkg/BUILD.bazel

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ load("@rules_go//go:def.bzl", "go_library")
22

33
go_library(
44
name = "pkg",
5-
srcs = ["interface.go"],
5+
srcs = [
6+
"fs.go",
7+
"interface.go",
8+
],
69
importpath = "github.com/eat-pray-ai/yutu/pkg",
710
visibility = ["//visibility:public"],
811
)

pkg/activity/BUILD.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ go_library(
66
importpath = "github.com/eat-pray-ai/yutu/pkg/activity",
77
visibility = ["//visibility:public"],
88
deps = [
9+
"//pkg",
910
"//pkg/auth",
1011
"//pkg/utils",
1112
"@com_github_jedib0t_go_pretty_v6//table",
@@ -17,5 +18,5 @@ go_test(
1718
name = "activity_test",
1819
srcs = ["activity_test.go"],
1920
embed = [":activity"],
20-
deps = ["//pkg/utils"],
21+
deps = ["@org_golang_google_api//youtube/v3:youtube"],
2122
)

pkg/activity/activity.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"io"
66

7+
"github.com/eat-pray-ai/yutu/pkg"
78
"github.com/eat-pray-ai/yutu/pkg/auth"
89
"github.com/eat-pray-ai/yutu/pkg/utils"
910
"github.com/jedib0t/go-pretty/v6/table"
@@ -163,8 +164,8 @@ func WithService(svc *youtube.Service) Option {
163164
return func(_ *activity) {
164165
if svc == nil {
165166
svc = auth.NewY2BService(
166-
auth.WithCredential(""),
167-
auth.WithCacheToken(""),
167+
auth.WithCredential("", pkg.Fsys),
168+
auth.WithCacheToken("", pkg.Fsys),
168169
).GetService()
169170
}
170171
service = svc

pkg/auth/BUILD.bazel

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("@rules_go//go:def.bzl", "go_library")
1+
load("@rules_go//go:def.bzl", "go_library", "go_test")
22

33
go_library(
44
name = "auth",
@@ -16,3 +16,9 @@ go_library(
1616
"@org_golang_x_oauth2//google",
1717
],
1818
)
19+
20+
go_test(
21+
name = "auth_test",
22+
srcs = ["service_test.go"],
23+
embed = [":auth"],
24+
)

pkg/auth/auth.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
const (
2323
cacheTokenFile = "youtube.token.json"
24+
credentialFile = "client_secret.json"
2425
manualInputHint = `
2526
After completing the authorization flow, enter the authorization code on command line.
2627

pkg/auth/service.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import (
55
"encoding/base64"
66
"errors"
77
"fmt"
8+
"io/fs"
89
"log"
910
"net/http"
1011
"os"
12+
"path/filepath"
1113
"strings"
1214

1315
"github.com/eat-pray-ai/yutu/pkg/utils"
@@ -21,15 +23,7 @@ var (
2123
errReadSecret = errors.New("unable to read client secret")
2224
)
2325

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"
3327

3428
type svc struct {
3529
Cacheable bool `yaml:"cacheable" json:"cacheable"`
@@ -62,23 +56,26 @@ func NewY2BService(opts ...Option) Svc {
6256
return s
6357
}
6458

65-
func WithCredential(cred string) Option {
59+
func WithCredential(cred string, fsys fs.FS) Option {
6660
return func(s *svc) {
6761
// cred > YUTU_CREDENTIAL
6862
envCred, ok := os.LookupEnv("YUTU_CREDENTIAL")
6963
if cred == "" && ok {
7064
cred = envCred
7165
} else if cred == "" {
72-
cred = "client_secret.json"
66+
cred = credentialFile
7367
}
74-
7568
// 1. cred is a file path
7669
// 2. cred is a base64 encoded string
7770
// 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)
8077
if err != nil {
81-
fmt.Printf(missingClientSecretsHint, cred)
78+
fmt.Println(missingClientSecretsHint)
8279
log.Fatalln(errors.Join(errReadSecret, err))
8380
}
8481
s.Credential = string(credBytes)
@@ -90,13 +87,13 @@ func WithCredential(cred string) Option {
9087
} else if utils.IsJson(cred) {
9188
s.Credential = cred
9289
} else {
93-
fmt.Printf(missingClientSecretsHint, cred)
90+
fmt.Println(missingClientSecretsHint)
9491
log.Fatalln(errors.Join(errReadSecret, err))
9592
}
9693
}
9794
}
9895

99-
func WithCacheToken(token string) Option {
96+
func WithCacheToken(token string, fsys fs.FS) Option {
10097
return func(s *svc) {
10198
// token > YUTU_CACHE_TOKEN
10299
envToken, ok := os.LookupEnv("YUTU_CACHE_TOKEN")
@@ -109,8 +106,11 @@ func WithCacheToken(token string) Option {
109106
// 1. token is a file path
110107
// 2. token is a base64 encoded string
111108
// 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)
114114
if err != nil {
115115
log.Fatalln(errors.Join(errReadToken, err))
116116
}

pkg/caption/BUILD.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ go_library(
66
importpath = "github.com/eat-pray-ai/yutu/pkg/caption",
77
visibility = ["//visibility:public"],
88
deps = [
9+
"//pkg",
910
"//pkg/auth",
1011
"//pkg/utils",
1112
"@com_github_jedib0t_go_pretty_v6//table",
@@ -17,5 +18,5 @@ go_test(
1718
name = "caption_test",
1819
srcs = ["caption_test.go"],
1920
embed = [":caption"],
20-
deps = ["//pkg/utils"],
21+
deps = ["@org_golang_google_api//youtube/v3:youtube"],
2122
)

pkg/caption/caption.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77
"os"
88

9+
"github.com/eat-pray-ai/yutu/pkg"
910
"github.com/eat-pray-ai/yutu/pkg/auth"
1011
"github.com/eat-pray-ai/yutu/pkg/utils"
1112
"github.com/jedib0t/go-pretty/v6/table"
@@ -403,8 +404,8 @@ func WithService(svc *youtube.Service) Option {
403404
return func(_ *caption) {
404405
if svc == nil {
405406
svc = auth.NewY2BService(
406-
auth.WithCredential(""),
407-
auth.WithCacheToken(""),
407+
auth.WithCredential("", pkg.Fsys),
408+
auth.WithCacheToken("", pkg.Fsys),
408409
).GetService()
409410
}
410411
service = svc

0 commit comments

Comments
 (0)