aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--bommom.go7
-rw-r--r--core.go4
-rw-r--r--formats.go67
3 files changed, 39 insertions, 39 deletions
diff --git a/bommom.go b/bommom.go
index 6b62bfa..623e477 100644
--- a/bommom.go
+++ b/bommom.go
@@ -258,9 +258,14 @@ func convertCmd() {
}
if inFormat == "csv" && bs == nil {
// TODO: from inname? if ShortName?
- bs = &BomMeta{Name: "untitled", Owner: anonUser.name}
+ bs = &BomMeta{Name: "untitled",
+ Owner: anonUser.name}
+ b.Version = "unversioned"
}
+ b.Created = time.Now()
+ b.Progeny = "File import from " + inFname + " (" + inFormat + ")"
+
if err := bs.Validate(); err != nil {
log.Fatal("loaded bommeta not valid: " + err.Error())
}
diff --git a/core.go b/core.go
index 74a72b9..f7f0555 100644
--- a/core.go
+++ b/core.go
@@ -41,7 +41,7 @@ type BomMeta struct {
Owner string `json:"owner_name"`
Description string `json:"description"`
HeadVersion string `json:"head_version"`
- Homepage *Url `json:"homepage_url"`
+ Homepage Url `json:"homepage_url"`
IsPublicView bool `json:"is_publicview",omitempty`
IsPublicEdit bool `json:"is_publicedit",omitempty`
}
@@ -81,7 +81,7 @@ func (b *Bom) Validate() error {
if !isShortName(b.Version) {
return Error("version not a ShortName: \"" + b.Version + "\"")
}
- if &b.Created == nil {
+ if b.Created.IsZero() {
return Error("created timestamp not defined")
}
return nil
diff --git a/formats.go b/formats.go
index e24c0ae..da22bd0 100644
--- a/formats.go
+++ b/formats.go
@@ -12,14 +12,28 @@ import (
"strings"
)
+// This compound container struct is useful for serializing to XML and JSON
+type BomContainer struct {
+ BomMetadata *BomMeta `json:"metadata"`
+ Bom *Bom `json:"bom"`
+}
+
// --------------------- text (CLI only ) -----------------------
func DumpBomAsText(bs *BomMeta, b *Bom, out io.Writer) {
fmt.Fprintln(out)
- fmt.Fprintf(out, "%s (version %s, created %s)\n", bs.Name, b.Version, b.Created)
- fmt.Fprintf(out, "Creator: %s\n", bs.Owner)
+ fmt.Fprintf(out, "Name:\t\t%s\n", bs.Name)
+ fmt.Fprintf(out, "Version:\t%s\n", b.Version)
+ fmt.Fprintf(out, "Creator:\t%s\n", bs.Owner)
+ fmt.Fprintf(out, "Timestamp:\t%s\n", b.Created)
+ if bs.Homepage != "" {
+ fmt.Fprintf(out, "Homepage:\t%s\n", bs.Homepage)
+ }
+ if b.Progeny != "" {
+ fmt.Fprintf(out, "Source:\t\t%s\n", b.Progeny)
+ }
if bs.Description != "" {
- fmt.Fprintf(out, "Description: %s\n", bs.Description)
+ fmt.Fprintf(out, "Description:\t%s\n", bs.Description)
}
fmt.Println()
// "by line item"
@@ -83,65 +97,46 @@ func LoadBomFromCSV(out io.Writer) (*Bom, error) {
func DumpBomAsJSON(bs *BomMeta, b *Bom, out io.Writer) {
- obj := map[string]interface{}{
- "bom_meta": bs,
- "bom": b,
- }
+ container := &BomContainer{BomMetadata: bs, Bom: b}
enc := json.NewEncoder(out)
- if err := enc.Encode(&obj); err != nil {
+ if err := enc.Encode(&container); err != nil {
log.Fatal(err)
}
}
func LoadBomFromJSON(input io.Reader) (*BomMeta, *Bom, error) {
- bs := &BomMeta{}
- b := &Bom{}
-
- obj := map[string]interface{}{
- "bom_meta": &bs,
- "bom": &b,
- }
-
- fmt.Println(obj)
+ container := &BomContainer{}
enc := json.NewDecoder(input)
- if err := enc.Decode(&obj); err != nil {
+ if err := enc.Decode(&container); err != nil {
log.Fatal(err)
}
- if &bs == nil || &b == nil {
- log.Fatal("didn't load successfully")
- }
- fmt.Println(bs)
- fmt.Println(b)
- return bs, b, nil
+ return container.BomMetadata, container.Bom, nil
}
// --------------------- XML -----------------------
func DumpBomAsXML(bs *BomMeta, b *Bom, out io.Writer) {
+ container := &BomContainer{BomMetadata: bs, Bom: b}
enc := xml.NewEncoder(out)
- if err := enc.Encode(bs); err != nil {
- log.Fatal(err)
- }
- if err := enc.Encode(b); err != nil {
+
+ // generic XML header
+ io.WriteString(out, xml.Header)
+
+ if err := enc.Encode(container); err != nil {
log.Fatal(err)
}
}
func LoadBomFromXML(input io.Reader) (*BomMeta, *Bom, error) {
- bs := BomMeta{}
- b := Bom{}
-
+ container := &BomContainer{}
enc := xml.NewDecoder(input)
- if err := enc.Decode(&bs); err != nil {
- log.Fatal(err)
- }
- if err := enc.Decode(&b); err != nil {
+ if err := enc.Decode(&container); err != nil {
log.Fatal(err)
}
- return &bs, &b, nil
+ return container.BomMetadata, container.Bom, nil
}