This repository has been archived by the owner on Apr 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
99 lines (84 loc) · 2.5 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
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/ChimeraCoder/anaconda"
)
var (
version string
commit string
date string
)
const (
accessTokenName = "access-token"
accessTokenSecretName = "access-token-secret"
consumerKeyName = "consumer-key"
consumerSecretName = "consumer-secret"
tweetAgeName = "tweet-age"
dryRunName = "dry-run"
checkIntervalName = "check-interval"
)
func main() {
if err := run(os.Args, os.Stdout); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
}
func run(args []string, stdout io.Writer) error {
log.SetOutput(stdout)
flags := flag.NewFlagSet(args[0], flag.ExitOnError)
var (
accessToken = flags.String(accessTokenName, "", "Access token")
accessTokenSecret = flags.String(accessTokenSecretName, "", "Access token secret")
consumerKey = flags.String(consumerKeyName, "", "Consumer key")
consumerSecret = flags.String(consumerSecretName, "", "Consumer secret")
tweetAge = flags.Duration(tweetAgeName, time.Hour*4380, "Tweets older than this duration will be deleted")
dryRun = flags.Bool(dryRunName, false, "Check that something can be deleted, no real deletion is made")
checkInterval = flags.Duration(checkIntervalName, time.Hour*24, "Cleanup interval")
showVersion = flags.Bool("v", false, "Show version")
)
if err := flags.Parse(args[1:]); err != nil {
return fmt.Errorf("failed to parse flags: %w", err)
}
if *showVersion {
fmt.Printf("Version: %s\nCommit: %s\nDate: %s\n", version, commit, date)
return nil
}
if *accessToken == "" {
return fmt.Errorf("%s can't be empty", accessTokenName)
}
if *accessTokenSecret == "" {
return fmt.Errorf("%s can't be empty", accessTokenSecretName)
}
if *consumerKey == "" {
return fmt.Errorf("%s can't be empty", consumerKeyName)
}
if *consumerSecret == "" {
return fmt.Errorf("%s can't be empty", consumerSecretName)
}
tw := &Twitter{
api: anaconda.NewTwitterApiWithCredentials(*accessToken, *accessTokenSecret, *consumerKey, *consumerSecret),
}
c := NewCleaner(tw, *tweetAge, *checkInterval, *dryRun)
if err := c.Init(); err != nil {
return fmt.Errorf("failed to start: %w", err)
}
log.Println("successfully started")
if *dryRun {
log.Println("running in \"dry run\" mode")
}
go func() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)
<-sigs
log.Println("shutdown")
c.Stop()
}()
return c.Start()
}