| 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 | /* |
||
| 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 |