Passed
Push — development ( db54e3...2d43b6 )
by Clive
01:27
created

cmd/clean.go   A

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 20
eloc 93
dl 0
loc 156
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
F cmd.clean 0 110 18
1
/*
2
Copyright © 2022 Clive Walkden <[email protected]>
3
4
Permission is hereby granted, free of charge, to any person obtaining a copy
5
of this software and associated documentation files (the "Software"), to deal
6
in the Software without restriction, including without limitation the rights
7
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
copies of the Software, and to permit persons to whom the Software is
9
furnished to do so, subject to the following conditions:
10
11
The above copyright notice and this permission notice shall be included in
12
all copies or substantial portions of the Software.
13
14
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
THE SOFTWARE.
21
*/
22
package cmd
23
24
import (
25
	"context"
26
	"fmt"
27
	"github.com/aws/aws-sdk-go-v2/service/s3"
28
	"github.com/aws/aws-sdk-go-v2/service/s3/types"
29
	"github.com/spf13/viper"
30
	"log"
31
	"time"
32
	"wasabiCleanup/internal/client/wasabi"
33
	"wasabiCleanup/internal/config"
34
	"wasabiCleanup/internal/reporting"
35
	"wasabiCleanup/internal/utils"
36
37
	"github.com/spf13/cobra"
38
)
39
40
var (
41
	dryRun bool
42
43
	// cleanCmd represents the clean command
44
	cleanCmd = &cobra.Command{
45
		Use:   "clean",
46
		Short: "Clean up the outdated files.",
47
		Run: func(cmd *cobra.Command, args []string) {
48
			clean(cmd)
49
		},
50
	}
51
)
52
53
type S3Object struct {
54
	Key          string
55
	LastModified time.Time
56
	Size         int64
57
}
58
59
type S3Objects struct {
60
	Items []types.ObjectIdentifier
61
	Size  int64
62
}
63
64
func init() {
65
	cleanCmd.Flags().BoolVarP(&dryRun, "dryrun", "n", false, "Show what will be deleted but don't delete it")
66
}
67
68
func clean(cmd *cobra.Command) {
69
	dryRun, _ := cmd.Flags().GetBool("dryrun")
70
	verbose, _ := cmd.Flags().GetBool("verbose")
71
72
	client := wasabi.Client()
73
74
	report := reporting.Report{DryRun: dryRun}
75
76
	buckets, err := client.ListBuckets(context.TODO(), &s3.ListBucketsInput{})
77
	if err != nil {
78
		log.Fatal(err)
79
	}
80
81
	log.Println("Working...")
82
	for _, object := range buckets.Buckets {
83
		if verbose {
84
			fmt.Printf("Checking Bucket %s\n", *object.Name)
85
		}
86
87
		if config.AppConfig().Buckets[*object.Name] == 0 {
88
			if viper.GetBool("verbose") {
89
				fmt.Printf("\t- Bucket not in config, skipping\n")
90
			}
91
			continue
92
		}
93
94
		// Return files that need deleting from this bucket based on the Retention Policy
95
		objectList := S3Objects{}
96
		safeList := S3Objects{}
97
		maxKeys := 0
98
99
		params := &s3.ListObjectsV2Input{Bucket: object.Name}
100
101
		// Create the Paginator for the ListObjectsV2 operation.
102
		p := s3.NewListObjectsV2Paginator(client, params, func(o *s3.ListObjectsV2PaginatorOptions) {
103
			if v := int32(maxKeys); v != 0 {
104
				o.Limit = v
105
			}
106
		})
107
108
		// The date we need to delete items prior to
109
		comparisonDate := time.Now().AddDate(0, 0, -config.AppConfig().Buckets[*object.Name]-1)
110
		if verbose {
111
			fmt.Printf("\t- Checking files date is before %s\n", comparisonDate)
112
		}
113
114
		// Iterate through the S3 object pages, printing each object returned.
115
		var i int
116
		for p.HasMorePages() {
117
			i++
118
119
			// Next Page takes a new context for each page retrieval. This is where
120
			// you could add timeouts or deadlines.
121
			page, err := p.NextPage(context.TODO())
122
			if err != nil {
123
				log.Fatalf("\t\tfailed to get page %v, %v", i, err)
124
			}
125
126
			if verbose {
127
				fmt.Printf("\t\t- Next page (%d)\n", i)
128
			}
129
130
			// Log the objects found
131
			for _, obj := range page.Contents {
132
				if obj.LastModified.Before(comparisonDate) {
133
					objectList.Items = append(objectList.Items, types.ObjectIdentifier{
134
						Key: obj.Key,
135
					})
136
					objectList.Size += obj.Size
137
138
					if dryRun {
139
						if verbose {
140
							fmt.Printf("\t\t\t- Deleting object %s\n", *obj.Key)
141
						} else {
142
							fmt.Printf("\t- Deleting object %s\n", *obj.Key)
143
						}
144
					} else {
145
						if verbose {
146
							fmt.Printf("\t\t\t- Deleting object %s\n", *obj.Key)
147
						}
148
						_, err = client.DeleteObject(context.Background(), &s3.DeleteObjectInput{
149
							Bucket: object.Name,
150
							Key:    obj.Key,
151
						})
152
153
						if err != nil {
154
							panic("Couldn't delete items")
155
						}
156
					}
157
				} else {
158
					safeList.Items = append(safeList.Items, types.ObjectIdentifier{
159
						Key: obj.Key,
160
					})
161
					safeList.Size += obj.Size
162
				}
163
			}
164
		}
165
166
		result := reporting.Result{
167
			Name:        *object.Name,
168
			Kept:        len(safeList.Items),
169
			KeptSize:    utils.ByteCountSI(safeList.Size),
170
			Deleted:     len(objectList.Items),
171
			DeletedSize: utils.ByteCountSI(objectList.Size),
172
		}
173
174
		report.Result = append(report.Result, result)
175
	}
176
177
	reporting.Output(report)
178
}
179