Skip to content

Commit

Permalink
feat: make output csv
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Sep 16, 2020
1 parent 562fcd8 commit d8cc503
Showing 1 changed file with 37 additions and 13 deletions.
50 changes: 37 additions & 13 deletions cmd/cloc.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"encoding/csv"
"fmt"
"github.com/boyter/scc/processor"
"github.com/phodal/coca/cmd/config"
Expand All @@ -11,6 +12,7 @@ import (
"log"
"os"
"path/filepath"
"strconv"
"strings"
)

Expand Down Expand Up @@ -60,19 +62,7 @@ var clocCmd = &cobra.Command{
processor.Process()
}

var summaryMap = make(map[string]cloc.ClocSummary)
for _, file := range outputFiles {
var summary = cloc.ClocSummary{}
contents, _ := ioutil.ReadFile(file)
baseName := strings.TrimSuffix(filepath.Base(file), filepath.Ext(file))
err := yaml.Unmarshal(contents, &summary)
if err != nil {
panic(err);
}
summaryMap[baseName] = summary
}

fmt.Println(summaryMap)
convertToCsv(outputFiles)

return
} else {
Expand All @@ -88,6 +78,40 @@ var clocCmd = &cobra.Command{
},
}

func convertToCsv(outputFiles []string) {
var data = [][]string{{"module", "summary"}}
var summaryMap = make(map[string]cloc.ClocSummary)
for _, file := range outputFiles {
var summary = cloc.ClocSummary{}
contents, _ := ioutil.ReadFile(file)
baseName := strings.TrimSuffix(filepath.Base(file), filepath.Ext(file))
err := yaml.Unmarshal(contents, &summary)
if err != nil {
panic(err)
}
summaryMap[baseName] = summary
data = append(data, []string{baseName, strconv.Itoa(int(summary.Sum.Code))})
}

file, err := os.Create(filepath.FromSlash(config.CocaConfig.ReporterPath + "/" + "cloc.csv"))
checkError("Cannot create file", err)
defer file.Close()

writer := csv.NewWriter(file)
defer writer.Flush()

for _, value := range data {
err := writer.Write(value)
checkError("Cannot write to file", err)
}
}

func checkError(message string, err error) {
if err != nil {
log.Fatal(message, err)
}
}

func init() {
rootCmd.AddCommand(clocCmd)

Expand Down

0 comments on commit d8cc503

Please sign in to comment.