diff options
-rw-r--r-- | bommom.go | 46 | ||||
-rw-r--r-- | core.go | 22 | ||||
-rw-r--r-- | store.go | 13 | ||||
-rw-r--r-- | util.go | 2 | ||||
-rw-r--r-- | util_test.go | 2 |
5 files changed, 81 insertions, 4 deletions
@@ -9,6 +9,7 @@ import ( "log" "os" "path" + "time" ) // Globals @@ -51,13 +52,15 @@ func main() { switch flag.Arg(0) { default: log.Fatal("Error: unknown command: ", flag.Arg(0)) - case "load", "serve": + case "serve": log.Fatal("Error: Unimplemented, sorry") case "init": log.Println("Initializing...") initCmd() case "dump": dumpCmd() + case "load": + loadCmd() case "convert": convertCmd() case "list": @@ -206,6 +209,43 @@ func dumpCmd() { dumpOut(fname, bs, b) } +func loadCmd() { + + var userName, bomName, version string + if flag.NArg() == 4 { + userName = anonUser.name + bomName = flag.Arg(2) + version = flag.Arg(3) + } else if flag.NArg() == 5 { + userName = flag.Arg(2) + bomName = flag.Arg(3) + version = flag.Arg(4) + } else { + log.Fatal("Error: wrong number of arguments (expected input file, optional username, bomname)") + } + inFname := flag.Arg(1) + + if !(isShortName(userName) && isShortName(bomName) && isShortName(version)) { + log.Fatal("user, name, and version must be ShortNames") + } + + + bs, b := loadIn(inFname) + if inFormat == "csv" && bs == nil { + // TODO: from inname? if ShortName? + bs = &BomStub{} + } + + bs.Owner = userName + bs.Name = bomName + b.Progeny = "File import from " + inFname + " (" + inFormat + ")" + b.Created = time.Now() + + openBomStore() + + bomstore.Persist(bs, b, ShortName(version)) +} + func convertCmd() { if flag.NArg() != 3 { log.Fatal("Error: wrong number of arguments (expected input and output files)") @@ -266,9 +306,9 @@ func printUsage() { fmt.Println("") fmt.Println("\tinit \t\t initialize BOM and authentication datastores") fmt.Println("\tlist [user]\t\t list BOMs, optionally filtered by user") - fmt.Println("\tload <file.type> [user] [bom_name]\t import a BOM") + fmt.Println("\tload <file.type> [user] <bom_name> <version>\t import a BOM") fmt.Println("\tdump <user> <name> [file.type]\t dump a BOM to stdout") - fmt.Println("\tconvert <infile.type> [outfile.type]\t convert a BOM file") + fmt.Println("\tconvert <infile.type> <outfile.type>\t convert a BOM file") fmt.Println("\tserve\t\t serve up web interface over HTTP") fmt.Println("") fmt.Println("Extra command line options:") @@ -77,6 +77,28 @@ func (b *Bom) AddLineItem(li *LineItem) error { return nil } +func (b *Bom) Validate() error { + if !isShortName(b.Version) { + return Error("version not a ShortName: \"" + b.Version + "\"") + } + // TODO: nil? what is zero of Time? + nullTime := time.Time{} + if b.Created == nullTime { + return Error("created timestamp not defined") + } + return nil +} + +func (bs *BomStub) Validate() error { + if !isShortName(bs.Name) { + return Error("name not a ShortName: \"" + bs.Name + "\"") + } + if !isShortName(bs.Owner) { + return Error("owner name not a ShortName: \"" + bs.Owner + "\"") + } + return nil +} + // ---------- testing func makeTestBom() *Bom { op1 := OfferPrice{Currency: "usd", Price: 1.0, MinQty: 1} @@ -129,8 +129,21 @@ func (jfbs *JSONFileBomStore) listBomsForUser(user ShortName) ([]BomStub, error) } func (jfbs *JSONFileBomStore) Persist(bs *BomStub, b *Bom, version ShortName) error { + + if err := bs.Validate(); err != nil { + return err + } + if err := b.Validate(); err != nil { + return err + } + b_fpath := jfbs.Rootfpath + "/" + string(bs.Owner) + "/" + string(bs.Name) + "/" + string(version) + ".json" bs_fpath := jfbs.Rootfpath + "/" + string(bs.Owner) + "/" + string(bs.Name) + "/_meta.json" + + if f, err := os.Open(b_fpath); err == nil { + f.Close() + return Error("bom with same owner, name, and version already exists") + } if err := writeJsonBomStub(bs_fpath, bs); err != nil { log.Fatal(err) } @@ -28,5 +28,5 @@ func isShortName(s string) bool { return false } } - return true + return len(s) > 0 } diff --git a/util_test.go b/util_test.go index 7a87bde..32186a0 100644 --- a/util_test.go +++ b/util_test.go @@ -16,6 +16,8 @@ var noShort = []string{ "AS_DF", "2o45", "as.12df", + "5", + "", } func TestIsShortName(t *testing.T) { |