Skip to content

Commit

Permalink
Add support for XLSX output format
Browse files Browse the repository at this point in the history
  • Loading branch information
pmundt committed Feb 26, 2019
1 parent 344f9e8 commit 396f44c
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ license files named appropiately or inline licenses such as the below in source

In a nutshell this project is a reimplementation of http://www.boyter.org/2017/05/identify-software-licenses-python-vector-space-search-ngram-keywords/ using Go while I attempt to nut out the nuances of the language.

It can produce report outputs as valid [SPDX](https://spdx.org/), CSV, JSON and CLI formatted. It has been designed to work inside CI systems that capture either stdout or file artifacts.
It can produce report outputs as valid [SPDX](https://spdx.org/), CSV, XLSX, JSON and CLI formatted. It has been designed to work inside CI systems that capture either stdout or file artifacts.

[![Build Status](https://travis-ci.org/boyter/lc.svg?branch=master)](https://travis-ci.org/boyter/lc)

Expand Down Expand Up @@ -66,7 +66,7 @@ COMMANDS:
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--format csv, -f csv Set output format, supports progress, tabular, json, spdx, summary or csv (default: "tabular")
--format csv, -f csv Set output format, supports progress, tabular, json, spdx, summary, xlsx or csv (default: "tabular")
--output FILE, -o FILE Set output file if not set will print to stdout FILE
--confidence 0.95, -c 0.95 Set required confidence level for licence matching between 0 and 1 E.G. 0.95 (default: "0.85")
--deepguess true, --dg true Should attempt to deep guess the licence false or true true (default: "true")
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func main() {
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "format, f",
Usage: "Set output format, supports progress, tabular, json, spdx or `csv`",
Usage: "Set output format, supports progress, tabular, json, spdx, xlsx or `csv`",
Destination: &parsers.Format,
Value: "tabular",
},
Expand Down
2 changes: 1 addition & 1 deletion parsers/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func processFile(directory string, file string, guessed []LicenseMatch, rootLice
sha1 := ""
sha256 := ""

if strings.ToLower(Format) == "csv" || strings.ToLower(Format) == "json" || strings.ToLower(Format) == "spdx21" || strings.ToLower(Format) == "spdx" {
if strings.ToLower(Format) == "csv" || strings.ToLower(Format) == "xlsx" || strings.ToLower(Format) == "json" || strings.ToLower(Format) == "spdx21" || strings.ToLower(Format) == "spdx" {
md5 = getMd5Hash(content)
sha1 = getSha1Hash(content)
sha256 = getSha256Hash(content)
Expand Down
63 changes: 63 additions & 0 deletions parsers/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/csv"
"encoding/json"
"fmt"
"github.com/tealeg/xlsx"
"io/ioutil"
"log"
"os"
Expand Down Expand Up @@ -69,6 +70,68 @@ func toCSV(fileResults []FileResult) {
}
}

func toXLSX(fileResults []FileResult) {
records := [][]string{{
"filename",
"directory",
"license",
"confidence",
"rootlicenses",
"md5",
"sha1",
"sha256",
"bytes",
"byteshuman"},
}

for _, result := range fileResults {
licenseConcluded, confidence := determineLicense(result)

rootLicenseString := ""
for _, v := range result.LicenseRoots {
rootLicenseString += fmt.Sprintf("%s,", v.LicenseId)
}
rootLicenseString = strings.TrimRight(rootLicenseString, ", ")

records = append(records, []string{
result.Filename,
result.Directory,
licenseConcluded,
confidence,
rootLicenseString,
result.Md5Hash,
result.Sha1Hash,
result.Sha256Hash,
strconv.Itoa(result.Bytes),
result.BytesHuman})
}

file := xlsx.NewFile()
sheet, err := file.AddSheet("Sheet1")
if err != nil {
log.Fatalln("error creating xlsx:", err)
}

for _, record := range records {
row := sheet.AddRow()
written := row.WriteSlice(&record, -1)
if written < 0 {
log.Fatalln("error writing row")
}
}

// As this is a binary format, writing to stdout is primarily for the purpose of enabling redirection
if FileOutput == "" {
err = file.Write(os.Stdout)
if err != nil {
log.Fatalln("error writing xlsx:", err)
}
} else {
file.Save(FileOutput)
fmt.Println("Results written to " + FileOutput)
}
}

func toJSON(fileResults []FileResult) {
t, _ := json.Marshal(fileResults)

Expand Down
2 changes: 2 additions & 0 deletions parsers/guesser.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ func Process() {
switch strings.ToLower(Format) {
case "csv":
toCSV(fileResults)
case "xlsx":
toXLSX(fileResults)
case "json":
toJSON(fileResults)
case "tabular":
Expand Down

0 comments on commit 396f44c

Please sign in to comment.