-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
96 lines (75 loc) · 2.03 KB
/
list.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
package actions
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
myAws "github.com/halprin/cloud-backup-go/aws"
"github.com/halprin/cloud-backup-go/config"
"log"
"path"
)
func List(configFilePath string, timestamp string) error {
log.Println("Listing backups")
overallConfig, err := config.New(configFilePath)
if err != nil {
return err
}
if timestamp == "" {
err = listTimestamps(overallConfig)
} else {
err = listBackups(overallConfig, timestamp)
}
log.Println("Done listing backups")
return err
}
func listTimestamps(overallConfig config.BackupConfiguration) error {
log.Println("Listing timestamps")
awsSession, err := myAws.GetSession(overallConfig.AwsCredentialConfigPath, overallConfig.AwsProfile)
if err != nil {
return err
}
s3Client := s3.New(awsSession)
listObjectsInput := &s3.ListObjectsV2Input{
Bucket: &overallConfig.S3Bucket,
Delimiter: aws.String("/"),
}
listObjectsOutput, err := s3Client.ListObjectsV2(listObjectsInput)
if err != nil {
return err
}
prefixes := listObjectsOutput.CommonPrefixes
if len(prefixes) == 0 {
log.Println("No backup timestamps")
return nil
}
for _, prefix := range prefixes {
prefixSansSlash := (*prefix.Prefix)[0:len(*prefix.Prefix) - 1]
log.Printf("- %s", prefixSansSlash)
}
return nil
}
func listBackups(overallConfig config.BackupConfiguration, timestamp string) error {
log.Printf("Listing backups in %s", timestamp)
awsSession, err := myAws.GetSession(overallConfig.AwsCredentialConfigPath, overallConfig.AwsProfile)
if err != nil {
return err
}
s3Client := s3.New(awsSession)
listObjectsInput := &s3.ListObjectsV2Input{
Bucket: &overallConfig.S3Bucket,
Prefix: ×tamp,
}
listObjectsOutput, err := s3Client.ListObjectsV2(listObjectsInput)
if err != nil {
return err
}
objects := listObjectsOutput.Contents
if len(objects) == 0 {
log.Println("No backups")
return nil
}
for _, object := range objects {
baseFilename := path.Base(*object.Key)
log.Printf("- %s", baseFilename)
}
return nil
}