aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbnewbold <bnewbold@robocracy.org>2012-04-19 01:03:40 -0400
committerbnewbold <bnewbold@robocracy.org>2012-04-19 01:03:40 -0400
commit2d81a2a6ad8aa059c6249a2299a13c6867df0766 (patch)
treeaf7a76f28532b95c38e963fd9d543727be308942
parent90f7cebfb43708835aa26c57206d7c9dc9183a50 (diff)
downloadbommom-2d81a2a6ad8aa059c6249a2299a13c6867df0766.zip
bommom-2d81a2a6ad8aa059c6249a2299a13c6867df0766.tar.gz
more loading
-rw-r--r--bommom.go7
-rw-r--r--formats.go73
-rw-r--r--store.go2
3 files changed, 76 insertions, 6 deletions
diff --git a/bommom.go b/bommom.go
index 4441353..11b47c4 100644
--- a/bommom.go
+++ b/bommom.go
@@ -211,7 +211,7 @@ func dumpCmd() {
func loadCmd() {
if flag.NArg() != 5 {
- log.Fatal("Error: wrong number of arguments (expected input file, username, bomname)")
+ log.Fatal("Error: wrong number of arguments (expected input file, username, bomname, version)")
}
var userName, bomName, version string
@@ -234,10 +234,13 @@ func loadCmd() {
bm.Name = bomName
b.Progeny = "File import from " + inFname + " (" + inFormat + ")"
b.Created = time.Now()
+ b.Version = version
openBomStore()
- bomstore.Persist(bm, b, ShortName(version))
+ if err := bomstore.Persist(bm, b, ShortName(version)); err != nil {
+ log.Fatal(err)
+ }
}
func convertCmd() {
diff --git a/formats.go b/formats.go
index 9309d96..3cc66d1 100644
--- a/formats.go
+++ b/formats.go
@@ -10,6 +10,7 @@ import (
"io"
"log"
"strings"
+ "strconv"
)
// This compound container struct is useful for serializing to XML and JSON
@@ -86,10 +87,74 @@ func DumpBomAsCSV(b *Bom, out io.Writer) {
}
}
-func LoadBomFromCSV(out io.Writer) (*Bom, error) {
-
- b := Bom{}
-
+func LoadBomFromCSV(input io.Reader) (*Bom, error) {
+
+ b := Bom{LineItems: []LineItem{} }
+ reader := csv.NewReader(input)
+ reader.TrailingComma = true
+ reader.TrimLeadingSpace = true
+
+ header, err := reader.Read()
+ if err != nil {
+ log.Fatal(err)
+ }
+ var li *LineItem
+ var el_count int
+ var records []string
+ var qty string
+ for records, err = reader.Read(); err == nil; records, err = reader.Read() {
+ qty = ""
+ li = &LineItem{Elements: []string{}}
+ for i, col := range header {
+ switch strings.ToLower(col) {
+ case "qty", "quantity":
+ qty = strings.TrimSpace(records[i])
+ case "mpn", "manufacturer part number":
+ li.Mpn = strings.TrimSpace(records[i])
+ case "mfg", "manufacturer":
+ li.Manufacturer = strings.TrimSpace(records[i])
+ case "symbol", "symbols":
+ for _, symb := range strings.Split(records[i], ",") {
+ symb = strings.TrimSpace(symb)
+ if !isShortName(symb) {
+ li.Elements = append(li.Elements, symb)
+ } else if *verbose {
+ log.Println("symbol not a ShortName, skipped: " + symb)
+ }
+ }
+ case "description", "function":
+ li.Description = strings.TrimSpace(records[i])
+ case "comment", "comments":
+ li.Comment = strings.TrimSpace(records[i])
+ default:
+ // pass, no assignment
+ }
+ }
+ if qty != "" {
+ if n, err := strconv.Atoi(qty); err == nil && n >= 0 {
+ el_count = len(li.Elements)
+ // XXX: kludge, should handle this better
+ if n > 99999 || el_count > 99999 {
+ log.Fatal("too large a quantity of elements passed, crashing")
+ } else if el_count > n {
+ if *verbose {
+ log.Println("more symbols than qty, taking all symbols")
+ }
+ } else if el_count < n {
+ for j := 0; j < (n - el_count); j++ {
+ li.Elements = append(li.Elements, "")
+ }
+ }
+ }
+ }
+ if len(li.Elements) == 0 {
+ li.Elements = []string{"", }
+ }
+ b.LineItems = append(b.LineItems, *li)
+ }
+ if err.Error() != "EOF" {
+ log.Fatal(err)
+ }
return &b, nil
}
diff --git a/store.go b/store.go
index 54d4bb0..e14fca6 100644
--- a/store.go
+++ b/store.go
@@ -130,6 +130,8 @@ func (jfbs *JSONFileBomStore) listBomsForUser(user ShortName) ([]BomMeta, error)
func (jfbs *JSONFileBomStore) Persist(bm *BomMeta, b *Bom, version ShortName) error {
+ b.Version = string(version)
+ bm.HeadVersion = string(version)
if err := bm.Validate(); err != nil {
return err
}