@@ -3,6 +3,7 @@ package main
3
3
import (
4
4
"context"
5
5
"errors"
6
+ "fmt"
6
7
"os"
7
8
"time"
8
9
@@ -13,6 +14,7 @@ import (
13
14
bk "github.com/tailscale/go-bluesky"
14
15
"github.com/till/golangoss-bluesky/internal/bluesky"
15
16
"github.com/till/golangoss-bluesky/internal/content"
17
+ "github.com/urfave/cli/v2"
16
18
)
17
19
18
20
var (
22
24
cacheBucket string = "golangoss-cache-bucket"
23
25
24
26
ctx context.Context
27
+
28
+ // for cache
29
+ awsEndpoint string = ""
30
+ awsAccessKeyId string = ""
31
+ awsSecretKey string = ""
32
+
33
+ // for github crawling
34
+ githubToken string = ""
25
35
)
26
36
27
37
func init () {
@@ -30,71 +40,106 @@ func init() {
30
40
})))
31
41
32
42
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" )
40
43
}
41
44
42
45
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
+ },
68
138
}
69
139
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 ())
73
142
os .Exit (1 )
74
143
}
75
144
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
- }
100
145
}
0 commit comments