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 (
|
2020-02-07 20:37:42 +05:00
|
|
|
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 {
|
2020-02-07 20:37:42 +05:00
|
|
|
_ = 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()
|
|
|
|
}
|