(ctx context.Context)
| 135 | } |
| 136 | |
| 137 | func (c *commandBlobShardsModify) run(ctx context.Context) error { |
| 138 | var numMoved, numUnchanged, numRemoved int |
| 139 | |
| 140 | dotShardsFile := filepath.Join(c.rootPath, sharded.ParametersFile) |
| 141 | |
| 142 | log(ctx).Info("Reading .shards file.") |
| 143 | |
| 144 | srcPar, err := c.getParameters(dotShardsFile) |
| 145 | if err != nil { |
| 146 | return err |
| 147 | } |
| 148 | |
| 149 | dstPar := srcPar.Clone() |
| 150 | |
| 151 | if err2 := c.applyParameterChangesFromFlags(dstPar); err2 != nil { |
| 152 | return err2 |
| 153 | } |
| 154 | |
| 155 | log(ctx).Info("Moving files...") |
| 156 | |
| 157 | if err2 := c.renameBlobs(ctx, c.rootPath, "", dstPar, &numMoved, &numUnchanged); err2 != nil { |
| 158 | return errors.Wrap(err2, "error processing directory") |
| 159 | } |
| 160 | |
| 161 | if c.dryRun { |
| 162 | log(ctx).Infof("Would move %v file, %v unchanged.", numMoved, numUnchanged) |
| 163 | |
| 164 | return nil |
| 165 | } |
| 166 | |
| 167 | log(ctx).Infof("Moved %v files, %v unchanged.", numMoved, numUnchanged) |
| 168 | log(ctx).Info("Removing empty directories...") |
| 169 | |
| 170 | if _, err2 := c.removeEmptyDirs(ctx, c.rootPath, &numRemoved); err2 != nil { |
| 171 | return errors.Wrap(err2, "error removing empty directories") |
| 172 | } |
| 173 | |
| 174 | log(ctx).Infof("Removed %v empty directories...", numRemoved) |
| 175 | log(ctx).Info("Writing new .shards file.") |
| 176 | |
| 177 | of, err := os.Create(dotShardsFile) //nolint:gosec |
| 178 | if err != nil { |
| 179 | return errors.Wrap(err, "error creating .shards file") |
| 180 | } |
| 181 | defer of.Close() //nolint:errcheck |
| 182 | |
| 183 | return errors.Wrap(dstPar.Save(of), "error saving .shards file") |
| 184 | } |
| 185 | |
| 186 | func (c *commandBlobShardsModify) removeEmptyDirs(ctx context.Context, dir string, numRemoved *int) (bool, error) { |
| 187 | entries, err := os.ReadDir(dir) |
nothing calls this directly
no test coverage detected