Address code review feedback

Signed-off-by: Alexander Bezzubov <bzz@apache.org>
This commit is contained in:
Alexander Bezzubov 2019-04-08 16:07:10 +02:00
parent 416afb45fc
commit bdb5603f28
No known key found for this signature in database
GPG Key ID: 8039F5787EFCD05D
3 changed files with 14 additions and 18 deletions

View File

@ -29,7 +29,7 @@ func main() {
breakdownFlag := flag.Bool("breakdown", false, "") breakdownFlag := flag.Bool("breakdown", false, "")
jsonFlag := flag.Bool("json", false, "") jsonFlag := flag.Bool("json", false, "")
showVersion := flag.Bool("version", false, "Show the enry version information") showVersion := flag.Bool("version", false, "Show the enry version information")
allLangs := flag.Bool("all", false, "Show not only the files with programming languages (default) but all languages instead") allLangs := flag.Bool("all", false, "Show all files, including those identifed as non-programming languages")
countMode := flag.String("mode", "byte", "the method used to count file size. Available options are: file, line and byte") countMode := flag.String("mode", "byte", "the method used to count file size. Available options are: file, line and byte")
limitKB := flag.Int64("limit", 16*1024, "Analyse first N KB of the file (-1 means no limit)") limitKB := flag.Int64("limit", 16*1024, "Analyse first N KB of the file (-1 means no limit)")
flag.Parse() flag.Parse()
@ -98,15 +98,15 @@ func main() {
} }
// TODO(bzz): provide API that mimics lingust CLI output for // TODO(bzz): provide API that mimics lingust CLI output for
// running ByExtension & ByFilename // - running ByExtension & ByFilename
// reading the file, if that did not work // - reading the file, if that did not work
// GetLanguage([]Strategy) // - GetLanguage([]Strategy)
content, err := readFile(path, limit) content, err := readFile(path, limit)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return nil return nil
} }
//TODO(bzz): skip enry.IsGeneratedContent() after https://github.com/src-d/enry/issues/213 // TODO(bzz): skip enry.IsGeneratedContent() as well, after https://github.com/src-d/enry/issues/213
language := enry.GetLanguage(filepath.Base(path), content) language := enry.GetLanguage(filepath.Base(path), content)
if language == enry.OtherLanguage { if language == enry.OtherLanguage {
@ -184,7 +184,7 @@ func (e filelistError) Error() string {
func printPercents(root string, fSummary map[string][]string, buff *bytes.Buffer, mode string) { func printPercents(root string, fSummary map[string][]string, buff *bytes.Buffer, mode string) {
// Select the way we quantify 'amount' of code. // Select the way we quantify 'amount' of code.
var reducer func(string, []string) (float64, filelistError) reducer := fileCountValues
switch mode { switch mode {
case "file": case "file":
reducer = fileCountValues reducer = fileCountValues
@ -192,8 +192,6 @@ func printPercents(root string, fSummary map[string][]string, buff *bytes.Buffer
reducer = lineCountValues reducer = lineCountValues
case "byte": case "byte":
reducer = byteCountValues reducer = byteCountValues
default:
reducer = fileCountValues
} }
// Reduce the list of files to a quantity of file type. // Reduce the list of files to a quantity of file type.
@ -204,7 +202,6 @@ func printPercents(root string, fSummary map[string][]string, buff *bytes.Buffer
fileValues = make(map[string]float64) fileValues = make(map[string]float64)
) )
for fType, files := range fSummary { for fType, files := range fSummary {
//FIXME(bzz): all files here have relative paths
val, err := reducer(root, files) val, err := reducer(root, files)
if err != nil { if err != nil {
unreadableFiles = append(unreadableFiles, err...) unreadableFiles = append(unreadableFiles, err...)

View File

@ -391,7 +391,7 @@ func getDotIndexes(filename string) []int {
} }
// GetLanguagesByContent returns a slice of languages for the given content. // GetLanguagesByContent returns a slice of languages for the given content.
// It is a Strategy that uses a content-based regexp heuristics and a filename extension. // It is a Strategy that uses content-based regexp heuristics and a filename extension.
func GetLanguagesByContent(filename string, content []byte, _ []string) []string { func GetLanguagesByContent(filename string, content []byte, _ []string) []string {
if filename == "" { if filename == "" {
return nil return nil

View File

@ -53,12 +53,11 @@ func getFrequencies(samplesDir string) (*samplesFrequencies, error) {
for _, langDir := range langDirs { for _, langDir := range langDirs {
if !langDir.IsDir() { if !langDir.IsDir() {
log.Println(err)
continue continue
} }
lang := langDir.Name() lang := langDir.Name()
samples, err := getSamplesFrom(filepath.Join(samplesDir, lang)) samples, err := readSamples(filepath.Join(samplesDir, lang))
if err != nil { if err != nil {
log.Println(err) log.Println(err)
} }
@ -92,14 +91,14 @@ func getFrequencies(samplesDir string) (*samplesFrequencies, error) {
}, nil }, nil
} }
func getSamplesFrom(samplesLangDir string) ([]string, error) { func readSamples(samplesLangDir string) ([]string, error) {
const samplesLangFilesDir = "filenames" const samplesLangFilesDir = "filenames"
var samples []string
sampleFiles, err := ioutil.ReadDir(samplesLangDir) sampleFiles, err := ioutil.ReadDir(samplesLangDir)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var samples []string
for _, sampleFile := range sampleFiles { for _, sampleFile := range sampleFiles {
filename := filepath.Join(samplesLangDir, sampleFile.Name()) filename := filepath.Join(samplesLangDir, sampleFile.Name())
if sampleFile.Mode().IsRegular() { if sampleFile.Mode().IsRegular() {
@ -108,7 +107,7 @@ func getSamplesFrom(samplesLangDir string) ([]string, error) {
} }
if sampleFile.IsDir() && sampleFile.Name() == samplesLangFilesDir { if sampleFile.IsDir() && sampleFile.Name() == samplesLangFilesDir {
subSamples, err := getSubSamplesFrom(filename) subSamples, err := readSubSamples(filename)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -121,7 +120,7 @@ func getSamplesFrom(samplesLangDir string) ([]string, error) {
return samples, nil return samples, nil
} }
func getSubSamplesFrom(path string) ([]string, error) { func readSubSamples(path string) ([]string, error) {
subSamples := []string{} subSamples := []string{}
entries, err := ioutil.ReadDir(path) entries, err := ioutil.ReadDir(path)
if err != nil { if err != nil {