-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
129 lines (116 loc) · 3.38 KB
/
main.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"context"
"fmt"
"log/slog"
"os"
"os/signal"
"syscall"
"time"
_ "github.com/joho/godotenv/autoload"
"github.com/urfave/cli/v2"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/e-flux-platform/mongo-collection-archiver/internal/archive"
"github.com/e-flux-platform/mongo-collection-archiver/internal/source"
"github.com/e-flux-platform/mongo-collection-archiver/internal/storage"
)
type config struct {
storageURL string
mongoURL string
mongoDatabase string
mongoCollection string
delete bool
ignoreFileExistsError bool
retention time.Duration
delay time.Duration
}
func main() {
var cfg config
app := &cli.App{
Flags: []cli.Flag{
&cli.StringFlag{
Name: "storage-url",
EnvVars: []string{"STORAGE_URL"},
Required: true,
Destination: &cfg.storageURL,
},
&cli.StringFlag{
Name: "mongo-url",
EnvVars: []string{"MONGO_URL"},
Required: true,
Destination: &cfg.mongoURL,
},
&cli.StringFlag{
Name: "mongo-database",
EnvVars: []string{"MONGO_DATABASE"},
Required: true,
Destination: &cfg.mongoDatabase,
},
&cli.StringFlag{
Name: "mongo-collection",
EnvVars: []string{"MONGO_COLLECTION"},
Required: true,
Destination: &cfg.mongoCollection,
},
&cli.BoolFlag{
Name: "delete",
EnvVars: []string{"DELETE"},
Destination: &cfg.delete,
},
&cli.BoolFlag{
Name: "ignore-file-exists-error",
EnvVars: []string{"IGNORE_FILE_EXISTS_ERROR"},
Destination: &cfg.ignoreFileExistsError,
},
&cli.DurationFlag{
Name: "retention",
EnvVars: []string{"RETENTION"},
Required: true,
Destination: &cfg.retention,
},
&cli.DurationFlag{
Name: "delay",
EnvVars: []string{"DELAY"},
Destination: &cfg.delay,
Value: time.Second * 30,
},
},
Action: func(cCtx *cli.Context) error {
ctx, cancel := signal.NotifyContext(cCtx.Context, syscall.SIGTERM, syscall.SIGINT)
defer cancel()
return run(ctx, cfg)
},
}
if err := app.RunContext(context.Background(), os.Args); err != nil {
slog.Error("exiting", slog.Any("error", err))
os.Exit(1)
}
}
func run(ctx context.Context, cfg config) error {
slog.Info(
"received configuration",
slog.String("mongoURL", cfg.mongoURL),
slog.String("database", cfg.mongoDatabase),
slog.String("collection", cfg.mongoCollection),
slog.String("storageURL", cfg.storageURL),
slog.Bool("delete", cfg.delete),
slog.Bool("ignoreFileExistsError", cfg.ignoreFileExistsError),
slog.Duration("retention", cfg.retention),
slog.Duration("delay", cfg.delay),
)
client, err := mongo.Connect(ctx, options.Client().ApplyURI(cfg.mongoURL))
if err != nil {
return fmt.Errorf("unable to connect to mongo: %w", err)
}
collection := client.Database(cfg.mongoDatabase).Collection(cfg.mongoCollection)
docSource := source.NewMongoDB(collection)
store, err := storage.FromURL(ctx, cfg.storageURL)
if err != nil {
return fmt.Errorf("unable to connect to storage: %w", err)
}
defer store.Close()
targetDate := time.Now().UTC().Add(cfg.retention * -1)
archiver := archive.NewArchiver(docSource, store, !cfg.delete, cfg.ignoreFileExistsError, cfg.delay)
return archiver.Run(ctx, targetDate)
}