Skip to content

Commit d4a179a

Browse files
committed
Clean up CLI
1 parent 8e70da9 commit d4a179a

File tree

2 files changed

+43
-37
lines changed

2 files changed

+43
-37
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ This key is intentionally absent from pg2s3's configuration in order to require
5151

5252
## Usage
5353
The pg2s3 command-line tool offers three mutually-exclusive actions:
54-
* `pg2s3 -backup` - Create a new backup and upload to S3
55-
* `pg2s3 -restore` - Download the latest backup from S3 and restore
56-
* `pg2s3 -prune` - Prune old backups from S3
54+
* `pg2s3 backup` - Create a new backup and upload to S3
55+
* `pg2s3 restore` - Download the latest backup from S3 and restore
56+
* `pg2s3 prune` - Prune old backups from S3
5757

5858
If none of these are provided, pg2s3 will attempt to run in scheduled mode: sleeping until `backup.schedule` arrives and then performing a backup + prune.
5959

main.go

+40-34
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ func main() {
2323

2424
func run() int {
2525
conf := flag.String("conf", "pg2s3.conf", "pg2s3 config file")
26-
actionBackup := flag.Bool("backup", false, "pg2s3 action: backup")
27-
actionRestore := flag.Bool("restore", false, "pg2s3 action: restore")
28-
actionPrune := flag.Bool("prune", false, "pg2s3 action: prune")
2926
flag.Parse()
3027

3128
cfg, err := config.ReadFile(*conf)
@@ -34,70 +31,79 @@ func run() int {
3431
return 1
3532
}
3633

37-
fmt.Printf("%+v\n", cfg)
38-
3934
client, err := pg2s3.NewClient(cfg)
4035
if err != nil {
4136
fmt.Println(err)
4237
return 1
4338
}
4439

45-
// check how many actions were specified
46-
count := 0
47-
actions := []bool{*actionBackup, *actionRestore, *actionPrune}
48-
for _, action := range actions {
49-
if action {
50-
count++
51-
}
40+
// check for action (default run)
41+
args := flag.Args()
42+
var action string
43+
if len(args) == 0 {
44+
action = "run"
45+
} else {
46+
action = args[0]
5247
}
5348

54-
if *actionBackup {
49+
// backup: create a new backup
50+
if action == "backup" {
5551
err = backup(client, cfg)
5652
if err != nil {
5753
fmt.Println(err)
5854
return 1
5955
}
56+
57+
return 0
6058
}
6159

62-
if *actionRestore {
60+
// restore: restore the most recent backup
61+
if action == "restore" {
6362
err = restore(client, cfg)
6463
if err != nil {
6564
fmt.Println(err)
6665
return 1
6766
}
67+
68+
return 0
6869
}
6970

70-
if *actionPrune {
71+
// prune: delete the oldest backups above the retention count
72+
if action == "prune" {
7173
err = prune(client, cfg)
7274
if err != nil {
7375
fmt.Println(err)
7476
return 1
7577
}
78+
79+
return 0
7680
}
7781

78-
if count == 0 && cfg.Backup.Schedule != "" {
79-
s := gocron.NewScheduler(time.UTC)
80-
s.Cron(cfg.Backup.Schedule).Do(func() {
81-
err := backup(client, cfg)
82-
if err != nil {
83-
fmt.Println(err)
84-
return
85-
}
82+
if cfg.Backup.Schedule == "" {
83+
fmt.Println("no backup schedule specified, exiting")
84+
return 1
85+
}
8686

87-
err = prune(client, cfg)
88-
if err != nil {
89-
fmt.Println(err)
90-
return
91-
}
92-
})
87+
s := gocron.NewScheduler(time.UTC)
88+
s.Cron(cfg.Backup.Schedule).Do(func() {
89+
err := backup(client, cfg)
90+
if err != nil {
91+
fmt.Println(err)
92+
return
93+
}
9394

94-
// let systemd know that we are good to go (no-op if not using systemd)
95-
daemon.SdNotify(false, daemon.SdNotifyReady)
96-
fmt.Println("running scheduler")
95+
err = prune(client, cfg)
96+
if err != nil {
97+
fmt.Println(err)
98+
return
99+
}
100+
})
97101

98-
s.StartBlocking()
99-
}
102+
// let systemd know that we are good to go (no-op if not using systemd)
103+
daemon.SdNotify(false, daemon.SdNotifyReady)
104+
fmt.Printf("running on schedule: %s\n", cfg.Backup.Schedule)
100105

106+
s.StartBlocking()
101107
return 0
102108
}
103109

0 commit comments

Comments
 (0)