Conditions | 18 |
Total Lines | 110 |
Code Lines | 64 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like cmd.clean often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /* |
||
70 | func clean(cmd *cobra.Command) { |
||
71 | dryRun, _ := cmd.Flags().GetBool("dryrun") |
||
72 | verbose, _ := cmd.Flags().GetBool("verbose") |
||
73 | |||
74 | client := wasabi.Client() |
||
75 | |||
76 | report := reporting.Report{DryRun: dryRun} |
||
77 | |||
78 | buckets, err := client.ListBuckets(context.TODO(), &s3.ListBucketsInput{}) |
||
79 | if err != nil { |
||
80 | log.Fatal(err) |
||
81 | } |
||
82 | |||
83 | log.Println("Working...") |
||
84 | for _, object := range buckets.Buckets { |
||
85 | if verbose { |
||
86 | fmt.Printf("Checking Bucket %s\n", *object.Name) |
||
87 | } |
||
88 | |||
89 | if config.AppConfig().Buckets[*object.Name] == 0 { |
||
90 | if viper.GetBool("verbose") { |
||
91 | fmt.Printf("\t- Bucket not in config, skipping\n") |
||
92 | } |
||
93 | continue |
||
94 | } |
||
95 | |||
96 | // Return files that need deleting from this bucket based on the Retention Policy |
||
97 | objectList := S3Objects{} |
||
98 | safeList := S3Objects{} |
||
99 | maxKeys := 0 |
||
100 | |||
101 | params := &s3.ListObjectsV2Input{Bucket: object.Name} |
||
102 | |||
103 | // Create the Paginator for the ListObjectsV2 operation. |
||
104 | p := s3.NewListObjectsV2Paginator(client, params, func(o *s3.ListObjectsV2PaginatorOptions) { |
||
105 | if v := int32(maxKeys); v != 0 { |
||
106 | o.Limit = v |
||
107 | } |
||
108 | }) |
||
109 | |||
110 | // The date we need to delete items prior to |
||
111 | comparisonDate := time.Now().AddDate(0, 0, -config.AppConfig().Buckets[*object.Name]-1) |
||
112 | if verbose { |
||
113 | fmt.Printf("\t- Checking files date is before %s\n", comparisonDate) |
||
114 | } |
||
115 | |||
116 | // Iterate through the S3 object pages, printing each object returned. |
||
117 | var i int |
||
118 | for p.HasMorePages() { |
||
119 | i++ |
||
120 | |||
121 | // Next Page takes a new context for each page retrieval. This is where |
||
122 | // you could add timeouts or deadlines. |
||
123 | page, err := p.NextPage(context.TODO()) |
||
124 | if err != nil { |
||
125 | log.Fatalf("\t\tfailed to get page %v, %v", i, err) |
||
126 | } |
||
127 | |||
128 | if verbose { |
||
129 | fmt.Printf("\t\t- Next page (%d)\n", i) |
||
130 | } |
||
131 | |||
132 | // Log the objects found |
||
133 | for _, obj := range page.Contents { |
||
134 | if obj.LastModified.Before(comparisonDate) { |
||
135 | objectList.Items = append(objectList.Items, types.ObjectIdentifier{ |
||
136 | Key: obj.Key, |
||
137 | }) |
||
138 | objectList.Size += obj.Size |
||
139 | |||
140 | if dryRun { |
||
141 | if verbose { |
||
142 | fmt.Printf("\t\t\t- Deleting object %s\n", *obj.Key) |
||
143 | } else { |
||
144 | fmt.Printf("\t- Deleting object %s\n", *obj.Key) |
||
145 | } |
||
146 | } else { |
||
147 | if verbose { |
||
148 | fmt.Printf("\t\t\t- Deleting object %s\n", *obj.Key) |
||
149 | } |
||
150 | _, err = client.DeleteObject(context.Background(), &s3.DeleteObjectInput{ |
||
151 | Bucket: object.Name, |
||
152 | Key: obj.Key, |
||
153 | }) |
||
154 | |||
155 | if err != nil { |
||
156 | panic("Couldn't delete items") |
||
157 | } |
||
158 | } |
||
159 | } else { |
||
160 | safeList.Items = append(safeList.Items, types.ObjectIdentifier{ |
||
161 | Key: obj.Key, |
||
162 | }) |
||
163 | safeList.Size += obj.Size |
||
164 | } |
||
165 | } |
||
166 | } |
||
167 | |||
168 | result := reporting.Result{ |
||
169 | Name: *object.Name, |
||
170 | Kept: len(safeList.Items), |
||
171 | KeptSize: utils.ByteCountSI(safeList.Size), |
||
172 | Deleted: len(objectList.Items), |
||
173 | DeletedSize: utils.ByteCountSI(objectList.Size), |
||
174 | } |
||
175 | |||
176 | report.Result = append(report.Result, result) |
||
177 | } |
||
178 | |||
179 | reporting.Output(report) |
||
180 | } |
||
181 |