glp/outputters/csv/csv.go

50 lines
1.1 KiB
Go
Raw Normal View History

2020-02-06 15:09:25 +05:00
package csv
import (
// stdlib
c "encoding/csv"
"log"
"os"
"strconv"
// local
"go.dev.pztrn.name/glp/structs"
)
var (
headers = []string{"Module", "Version", "License", "Repository URL", "License URL", "Project", "Copyrights"}
2020-02-06 15:09:25 +05:00
)
// Responsible for pushing passed data into CSV file.
type outputter struct{}
func (o *outputter) Write(deps []*structs.Dependency, outFile string) {
log.Println("Got", strconv.Itoa(len(deps)), "dependencies to write")
// Check if file exists and remove it if so.
if _, err := os.Stat(outFile); !os.IsNotExist(err) || err == nil {
os.Remove(outFile)
}
// Open file and create writer.
f, err := os.Create(outFile)
if err != nil {
log.Fatalln("Failed to open '"+outFile+"' for writing:", err.Error())
}
writer := c.NewWriter(f)
writer.Comma = ';'
// Write header first.
_ = writer.Write(headers)
// Write dependencies information.
for _, dep := range deps {
_ = writer.Write([]string{dep.Name, dep.Version, dep.License.Name, dep.VCS.VCSPath, dep.License.URL, dep.Parent, dep.License.Copyrights})
2020-02-06 15:09:25 +05:00
}
writer.Flush()
f.Close()
}