| Conditions | 14 |
| Total Lines | 65 |
| Code Lines | 43 |
| 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.scanHandler 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 | package cmd |
||
| 46 | func scanHandler(flags scancmd.Flags, appConfig config.AppConfig, initialPoolSize, minChunkSize int) error { |
||
| 47 | if flags.Verbosity() { |
||
| 48 | log.Visibility.Debug = true |
||
| 49 | log.Visibility.Log = true |
||
| 50 | log.Visibility.Command = true |
||
| 51 | } |
||
| 52 | |||
| 53 | log.Log("Scanning...", fmt.Sprintf("Directory is \"%s\"", util.Abs(flags.Directory()))) |
||
| 54 | |||
| 55 | var files = scan.MustDir(flags.Directory(), appConfig.Extensions()) |
||
| 56 | poolSize, chunkSize := util.AdjustSizes(len(files), initialPoolSize, minChunkSize) |
||
| 57 | |||
| 58 | var chunks = make(chan vars.Chunk) |
||
| 59 | var scannedFiles = make(chan vars.File) |
||
| 60 | var wg sync.WaitGroup |
||
| 61 | scancmd.CreatePool( |
||
| 62 | &wg, |
||
| 63 | poolSize, |
||
| 64 | chunks, |
||
| 65 | func(files vars.Chunk) ([]byte, error) { |
||
| 66 | return etool.Read(files, appConfig.Fields()) |
||
| 67 | }, |
||
| 68 | func(data []byte) { |
||
| 69 | for _, parsed := range etool.Parse(data) { |
||
| 70 | scannedFiles <- parsed |
||
| 71 | } |
||
| 72 | }, |
||
| 73 | ) |
||
| 74 | |||
| 75 | for _, chunk := range files.Split(chunkSize) { |
||
| 76 | wg.Add(1) |
||
| 77 | chunks <- chunk |
||
| 78 | } |
||
| 79 | |||
| 80 | go func() { |
||
| 81 | wg.Wait() |
||
| 82 | close(chunks) |
||
| 83 | close(scannedFiles) |
||
| 84 | }() |
||
| 85 | |||
| 86 | outputFilename := randomizeOutputFilename(flags.Filename()) |
||
| 87 | |||
| 88 | headers := packHeaders(appConfig.Fields()) |
||
| 89 | wr, err := writers.Get(flags.Format()) |
||
| 90 | if err != nil { |
||
| 91 | return err |
||
| 92 | } |
||
| 93 | |||
| 94 | err = wr.Open(outputFilename, headers) |
||
| 95 | if err != nil { |
||
| 96 | return err |
||
| 97 | } |
||
| 98 | defer wr.Close() |
||
| 99 | |||
| 100 | for file := range scannedFiles { |
||
| 101 | file.WithRelPath(flags.Directory()) |
||
| 102 | err := wr.Write(&file) |
||
| 103 | if err != nil { |
||
| 104 | log.Failure("CSV write", fmt.Sprintf("failed writing data for \"%s\" file", file.RelPath())) |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | log.Done("Scanning completed", fmt.Sprintf("Output file is \"%s\" file", outputFilename)) |
||
| 109 | |||
| 110 | return nil |
||
| 111 | } |
||
| 131 |