From 9c034ab99f8632db38a6c205b848137ebb506d28 Mon Sep 17 00:00:00 2001 From: bnewbold Date: Wed, 18 Apr 2012 23:12:34 -0400 Subject: go fmt update --- bommom.go | 174 +++++++++++++++++++++++++++++------------------------------ core.go | 28 +++++----- formats.go | 28 +++++----- store.go | 24 ++++----- util_test.go | 4 +- 5 files changed, 128 insertions(+), 130 deletions(-) diff --git a/bommom.go b/bommom.go index 83f2610..1feab62 100644 --- a/bommom.go +++ b/bommom.go @@ -8,8 +8,8 @@ import ( "io" "log" "os" - "path" - "time" + "path" + "time" ) // Globals @@ -69,8 +69,8 @@ func main() { } func openBomStore() { - // defaults to JSON file store - var err error + // defaults to JSON file store + var err error bomstore, err = OpenJSONFileBomStore(*fileStorePath) if err != nil { log.Fatal(err) @@ -79,9 +79,9 @@ func openBomStore() { func dumpOut(fname string, bs *BomStub, b *Bom) { var outFile io.Writer - if fname == "" { - outFile = os.Stdout - } else { + if fname == "" { + outFile = os.Stdout + } else { // if no outFormat defined, infer from file extension if *outFormat == "" { switch ext := path.Ext(fname); ext { @@ -122,24 +122,24 @@ func dumpOut(fname string, bs *BomStub, b *Bom) { func loadIn(fname string) (bs *BomStub, b *Bom) { - if inFormat == "" { - switch ext := path.Ext(fname); ext { - case ".json", ".JSON": - inFormat = "json" - case ".csv", ".CSV": - inFormat = "csv" - case ".xml", ".XML": - inFormat = "xml" - default: - log.Fatal("Unknown file extention (use -format): " + ext) - } - } - - infile, err := os.Open(fname) - if err != nil { - log.Fatal(err) - } - defer infile.Close() + if inFormat == "" { + switch ext := path.Ext(fname); ext { + case ".json", ".JSON": + inFormat = "json" + case ".csv", ".CSV": + inFormat = "csv" + case ".xml", ".XML": + inFormat = "xml" + default: + log.Fatal("Unknown file extention (use -format): " + ext) + } + } + + infile, err := os.Open(fname) + if err != nil { + log.Fatal(err) + } + defer infile.Close() switch inFormat { case "json": @@ -151,15 +151,15 @@ func loadIn(fname string) (bs *BomStub, b *Bom) { default: log.Fatal("Error: unknown/unimplemented format: " + *outFormat) } - if err != nil { - log.Fatal(err) - } - return bs, b + if err != nil { + log.Fatal(err) + } + return bs, b } func initCmd() { - - openBomStore() + + openBomStore() bs, err := bomstore.GetStub(ShortName("common"), ShortName("gizmo")) if err == nil { // dummy BomStub already exists? @@ -189,14 +189,14 @@ func dumpCmd() { " and/or " + nameStr) } - var fname string - if flag.NArg() == 4 { - fname = flag.Arg(3) - } else { - fname = "" - } + var fname string + if flag.NArg() == 4 { + fname = flag.Arg(3) + } else { + fname = "" + } - openBomStore() + openBomStore() if auth == nil { auth = DummyAuth(true) @@ -206,44 +206,43 @@ func dumpCmd() { log.Fatal(err) } - dumpOut(fname, bs, b) + 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 { + 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") - } - + 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, 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() + bs.Owner = userName + bs.Name = bomName + b.Progeny = "File import from " + inFname + " (" + inFormat + ")" + b.Created = time.Now() - openBomStore() + openBomStore() - bomstore.Persist(bs, b, ShortName(version)) + bomstore.Persist(bs, b, ShortName(version)) } func convertCmd() { @@ -251,37 +250,37 @@ func convertCmd() { log.Fatal("Error: wrong number of arguments (expected input and output files)") } - // should refactor this to open both files first, then do processing? not - // sure what best practice is. + // should refactor this to open both files first, then do processing? not + // sure what best practice is. inFname := flag.Arg(1) outFname := flag.Arg(2) - bs, b := loadIn(inFname) - - if b == nil { - log.Fatal("null bom") - } - if inFormat == "csv" && bs == nil { - // TODO: from inname? if ShortName? - bs = &BomStub{Name: "untitled", Owner: anonUser.name} - } - - if err := bs.Validate(); err != nil { - log.Fatal("loaded bomstub not valid: " + err.Error()) - } - if err := b.Validate(); err != nil { - log.Fatal("loaded bom not valid: " + err.Error()) - } - - dumpOut(outFname, bs, b) + bs, b := loadIn(inFname) + + if b == nil { + log.Fatal("null bom") + } + if inFormat == "csv" && bs == nil { + // TODO: from inname? if ShortName? + bs = &BomStub{Name: "untitled", Owner: anonUser.name} + } + + if err := bs.Validate(); err != nil { + log.Fatal("loaded bomstub not valid: " + err.Error()) + } + if err := b.Validate(); err != nil { + log.Fatal("loaded bom not valid: " + err.Error()) + } + + dumpOut(outFname, bs, b) } func listCmd() { - openBomStore() + openBomStore() var bomStubs []BomStub - var err error + var err error if flag.NArg() > 2 { log.Fatal("Error: too many arguments...") } @@ -326,4 +325,3 @@ func printUsage() { fmt.Println("") flag.PrintDefaults() } - diff --git a/core.go b/core.go index 6a79896..0ae6da1 100644 --- a/core.go +++ b/core.go @@ -78,23 +78,23 @@ func (b *Bom) AddLineItem(li *LineItem) error { } func (b *Bom) Validate() error { - if !isShortName(b.Version) { - return Error("version not a ShortName: \"" + b.Version + "\"") - } - if &b.Created == nil { - return Error("created timestamp not defined") - } - return nil + if !isShortName(b.Version) { + return Error("version not a ShortName: \"" + b.Version + "\"") + } + if &b.Created == nil { + 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 + 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 diff --git a/formats.go b/formats.go index 5d93f80..fb7f573 100644 --- a/formats.go +++ b/formats.go @@ -74,9 +74,9 @@ func DumpBomAsCSV(b *Bom, out io.Writer) { func LoadBomFromCSV(out io.Writer) (*Bom, error) { - b := Bom{} + b := Bom{} - return &b, nil + return &b, nil } // --------------------- JSON ----------------------- @@ -96,26 +96,26 @@ func DumpBomAsJSON(bs *BomStub, b *Bom, out io.Writer) { func LoadBomFromJSON(input io.Reader) (*BomStub, *Bom, error) { - bs := &BomStub{} - b := &Bom{} + bs := &BomStub{} + b := &Bom{} obj := map[string]interface{}{ "bom_meta": &bs, "bom": &b, } - fmt.Println(obj) + fmt.Println(obj) enc := json.NewDecoder(input) if err := enc.Decode(&obj); 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 + if &bs == nil || &b == nil { + log.Fatal("didn't load successfully") + } + fmt.Println(bs) + fmt.Println(b) + return bs, b, nil } // --------------------- XML ----------------------- @@ -133,8 +133,8 @@ func DumpBomAsXML(bs *BomStub, b *Bom, out io.Writer) { func LoadBomFromXML(input io.Reader) (*BomStub, *Bom, error) { - bs := BomStub{} - b := Bom{} + bs := BomStub{} + b := Bom{} enc := xml.NewDecoder(input) if err := enc.Decode(&bs); err != nil { @@ -143,5 +143,5 @@ func LoadBomFromXML(input io.Reader) (*BomStub, *Bom, error) { if err := enc.Decode(&b); err != nil { log.Fatal(err) } - return &bs, &b, nil + return &bs, &b, nil } diff --git a/store.go b/store.go index 343cd9e..7cbf360 100644 --- a/store.go +++ b/store.go @@ -130,20 +130,20 @@ 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 - } - + 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 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) } diff --git a/util_test.go b/util_test.go index 32186a0..868a452 100644 --- a/util_test.go +++ b/util_test.go @@ -16,8 +16,8 @@ var noShort = []string{ "AS_DF", "2o45", "as.12df", - "5", - "", + "5", + "", } func TestIsShortName(t *testing.T) { -- cgit v1.2.3