Skip to content

Commit d9bda9c

Browse files
committed
Chore(refactor): wrap app in urfave/cli
1 parent dee11fe commit d9bda9c

File tree

4 files changed

+118
-66
lines changed

4 files changed

+118
-66
lines changed

cmd/bot/main.go

Lines changed: 104 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"context"
55
"errors"
6+
"fmt"
67
"os"
78
"time"
89

@@ -13,6 +14,7 @@ import (
1314
bk "github.com/tailscale/go-bluesky"
1415
"github.com/till/golangoss-bluesky/internal/bluesky"
1516
"github.com/till/golangoss-bluesky/internal/content"
17+
"github.com/urfave/cli/v2"
1618
)
1719

1820
var (
@@ -22,6 +24,14 @@ var (
2224
cacheBucket string = "golangoss-cache-bucket"
2325

2426
ctx context.Context
27+
28+
// for cache
29+
awsEndpoint string = ""
30+
awsAccessKeyId string = ""
31+
awsSecretKey string = ""
32+
33+
// for github crawling
34+
githubToken string = ""
2535
)
2636

2737
func init() {
@@ -30,71 +40,106 @@ func init() {
3040
})))
3141

3242
ctx = context.Background()
33-
34-
if _, status := os.LookupEnv("BLUESKY_APP_KEY"); !status {
35-
slog.ErrorContext(ctx, "no app key")
36-
os.Exit(1)
37-
}
38-
39-
blueskyAppKey = os.Getenv("BLUESKY_APP_KEY")
4043
}
4144

4245
func main() {
43-
client, err := bk.Dial(ctx, bk.ServerBskySocial)
44-
if err != nil {
45-
panic(err)
46-
}
47-
defer client.Close()
48-
49-
if err := client.Login(ctx, blueskyHandle, blueskyAppKey); err != nil {
50-
switch {
51-
case errors.Is(err, bk.ErrMasterCredentials):
52-
panic("You're not allowed to use your full-access credentials, please create an appkey")
53-
case errors.Is(err, bk.ErrLoginUnauthorized):
54-
panic("Username of application password seems incorrect, please double check")
55-
case err != nil:
56-
panic("Something else went wrong, please look at the returned error")
57-
}
58-
}
59-
60-
// init s3 client
61-
mc, err := minio.New(os.Getenv("AWS_ENDPOINT"), &minio.Options{
62-
Creds: credentials.NewStaticV4(os.Getenv("AWS_ACCESS_KEY_ID"), os.Getenv("AWS_SECRET_KEY"), ""),
63-
Secure: true,
64-
})
65-
if err != nil {
66-
slog.ErrorContext(ctx, "Failed to initialize MinIO client", slog.Any("err", err))
67-
os.Exit(1)
46+
bot := cli.App{
47+
Name: "golangoss-bluesky",
48+
Flags: []cli.Flag{
49+
&cli.StringFlag{
50+
Name: "bluesky-app-key",
51+
EnvVars: []string{"BLUESKY_APP_KEY"},
52+
Required: true,
53+
Destination: &blueskyAppKey,
54+
},
55+
&cli.StringFlag{
56+
Name: "aws-endpoint",
57+
EnvVars: []string{"AWS_ENDPOINT"},
58+
Required: true,
59+
Destination: &awsEndpoint,
60+
},
61+
&cli.StringFlag{
62+
Name: "aws-access-key-id",
63+
EnvVars: []string{"AWS_ACCESS_KEY_ID"},
64+
Required: true,
65+
Destination: &awsAccessKeyId,
66+
},
67+
&cli.StringFlag{
68+
Name: "aws-secret-key",
69+
EnvVars: []string{"AWS_SECRET_KEY"},
70+
Required: true,
71+
Destination: &awsSecretKey,
72+
},
73+
&cli.StringFlag{
74+
Name: "github-token",
75+
EnvVars: []string{"GITHUB_TOKEN"},
76+
Required: true,
77+
Destination: &githubToken,
78+
},
79+
},
80+
81+
Action: func(cCtx *cli.Context) error {
82+
client, err := bk.Dial(ctx, bk.ServerBskySocial)
83+
if err != nil {
84+
return fmt.Errorf("failed to open connection: %v", err)
85+
}
86+
defer client.Close()
87+
88+
if err := client.Login(ctx, blueskyHandle, blueskyAppKey); err != nil {
89+
switch {
90+
case errors.Is(err, bk.ErrMasterCredentials):
91+
return fmt.Errorf("you're not allowed to use your full-access credentials, please create an appkey")
92+
case errors.Is(err, bk.ErrLoginUnauthorized):
93+
return fmt.Errorf("username of application password seems incorrect, please double check")
94+
case err != nil:
95+
return fmt.Errorf("something else went wrong, please look at the returned error")
96+
}
97+
}
98+
99+
// init s3 client
100+
mc, err := minio.New(awsEndpoint, &minio.Options{
101+
Creds: credentials.NewStaticV4(awsAccessKeyId, awsSecretKey, ""),
102+
Secure: true,
103+
})
104+
if err != nil {
105+
return fmt.Errorf("failed to initialize minio client: %v", err)
106+
}
107+
108+
// ensure the bucket exists
109+
if err := mc.MakeBucket(ctx, cacheBucket, minio.MakeBucketOptions{}); err != nil {
110+
return fmt.Errorf("failed to create bucket: %v", err)
111+
}
112+
113+
c := bluesky.Client{
114+
Client: client,
115+
}
116+
117+
cacheClient := &content.CacheClientS3{
118+
MC: mc,
119+
Bucket: cacheBucket,
120+
CTX: ctx,
121+
}
122+
123+
if err := content.Start(githubToken, cacheClient); err != nil {
124+
return fmt.Errorf("failed to start service: %v", err)
125+
}
126+
127+
for {
128+
slog.DebugContext(ctx, "checking...")
129+
if err := content.Do(ctx, c); err != nil {
130+
slog.ErrorContext(ctx, err.Error())
131+
os.Exit(1)
132+
}
133+
134+
time.Sleep(5 * time.Minute)
135+
}
136+
return nil
137+
},
68138
}
69139

70-
// ensure the bucket exists
71-
if err := mc.MakeBucket(ctx, cacheBucket, minio.MakeBucketOptions{}); err != nil {
72-
slog.ErrorContext(ctx, "failed to create bucket", slog.Any("err", err))
140+
if err := bot.Run(os.Args); err != nil {
141+
slog.ErrorContext(ctx, err.Error())
73142
os.Exit(1)
74143
}
75144

76-
c := bluesky.Client{
77-
Client: client,
78-
}
79-
80-
cacheClient := &content.CacheClientS3{
81-
MC: mc,
82-
Bucket: cacheBucket,
83-
CTX: ctx,
84-
}
85-
86-
if err := content.Start(cacheClient); err != nil {
87-
slog.ErrorContext(ctx, "failed to start service", slog.Any("err", err))
88-
os.Exit(1)
89-
}
90-
91-
for {
92-
slog.DebugContext(ctx, "checking...")
93-
if err := content.Do(ctx, c); err != nil {
94-
slog.ErrorContext(ctx, err.Error())
95-
os.Exit(1)
96-
}
97-
98-
time.Sleep(5 * time.Minute)
99-
}
100145
}

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
require (
1111
github.com/carlmjohnson/versioninfo v0.22.5 // indirect
1212
github.com/cespare/xxhash/v2 v2.2.0 // indirect
13+
github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect
1314
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
1415
github.com/dustin/go-humanize v1.0.1 // indirect
1516
github.com/felixge/httpsnoop v1.0.4 // indirect
@@ -23,6 +24,8 @@ require (
2324
github.com/klauspost/compress v1.17.11 // indirect
2425
github.com/minio/md5-simd v1.1.2 // indirect
2526
github.com/rs/xid v1.6.0 // indirect
27+
github.com/russross/blackfriday/v2 v2.1.0 // indirect
28+
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
2629
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1 // indirect
2730
go.opentelemetry.io/otel v1.21.0 // indirect
2831
go.opentelemetry.io/otel/metric v1.21.0 // indirect
@@ -73,6 +76,7 @@ require (
7376
github.com/polydawn/refmt v0.89.1-0.20221221234430-40501e09de1f // indirect
7477
github.com/spaolacci/murmur3 v1.1.0 // indirect
7578
github.com/ureeves/jwt-go-secp256k1 v0.2.0 // indirect
79+
github.com/urfave/cli/v2 v2.27.5
7680
github.com/whyrusleeping/cbor-gen v0.2.1-0.20241030202151-b7a6831be65e // indirect
7781
go.uber.org/atomic v1.11.0 // indirect
7882
go.uber.org/multierr v1.11.0 // indirect

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ github.com/carlmjohnson/versioninfo v0.22.5/go.mod h1:QT9mph3wcVfISUKd0i9sZfVrPv
2121
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
2222
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
2323
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
24+
github.com/cpuguy83/go-md2man/v2 v2.0.5 h1:ZtcqGrnekaHpVLArFSe4HK5DoKx1T0rq2DwVB0alcyc=
25+
github.com/cpuguy83/go-md2man/v2 v2.0.5/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
2426
github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2527
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2628
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
@@ -181,6 +183,8 @@ github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncj
181183
github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU=
182184
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
183185
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
186+
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
187+
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
184188
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
185189
github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N3yZFZkDFs=
186190
github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo=
@@ -200,10 +204,14 @@ github.com/tailscale/go-bluesky v0.0.0-20241115170709-693553a07285/go.mod h1:kk7
200204
github.com/ureeves/jwt-go-secp256k1 v0.2.0 h1:A2D2F5E8a+WxZdkO9YviVxA9aUo3IqSvA/4zQztOZ5A=
201205
github.com/ureeves/jwt-go-secp256k1 v0.2.0/go.mod h1:7WMTEkrUxSM5PEesinVfdsiq5vu7kUJvLZUXBmL1svM=
202206
github.com/urfave/cli v1.22.10/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
207+
github.com/urfave/cli/v2 v2.27.5 h1:WoHEJLdsXr6dDWoJgMq/CboDmyY/8HMMH1fTECbih+w=
208+
github.com/urfave/cli/v2 v2.27.5/go.mod h1:3Sevf16NykTbInEnD0yKkjDAeZDS0A6bzhBH5hrMvTQ=
203209
github.com/warpfork/go-wish v0.0.0-20220906213052-39a1cc7a02d0 h1:GDDkbFiaK8jsSDJfjId/PEGEShv6ugrt4kYsC5UIDaQ=
204210
github.com/warpfork/go-wish v0.0.0-20220906213052-39a1cc7a02d0/go.mod h1:x6AKhvSSexNrVSrViXSHUEbICjmGXhtgABaHIySUSGw=
205211
github.com/whyrusleeping/cbor-gen v0.2.1-0.20241030202151-b7a6831be65e h1:28X54ciEwwUxyHn9yrZfl5ojgF4CBNLWX7LR0rvBkf4=
206212
github.com/whyrusleeping/cbor-gen v0.2.1-0.20241030202151-b7a6831be65e/go.mod h1:pM99HXyEbSQHcosHc0iW7YFmwnscr+t9Te4ibko05so=
213+
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
214+
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=
207215
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
208216
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
209217
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=

internal/content/content.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66
"log/slog"
7-
"os"
87
"strings"
98

109
"github.com/ezeoleaf/larry/cache"
@@ -17,16 +16,12 @@ var (
1716
provider github.Provider
1817
)
1918

20-
func Start(cacheClient cache.Client) error {
21-
if _, status := os.LookupEnv("GITHUB_TOKEN"); !status {
22-
panic("No github token")
23-
}
24-
19+
func Start(token string, cacheClient cache.Client) error {
2520
cfg := config.Config{
2621
Language: "go",
2722
}
2823

29-
provider = github.NewProvider(os.Getenv("GITHUB_TOKEN"), cfg, cacheClient)
24+
provider = github.NewProvider(token, cfg, cacheClient)
3025
return nil
3126
}
3227

0 commit comments

Comments
 (0)