diff --git a/cmd/cc-boltinit/main.go b/cmd/cc-boltinit/main.go index 19de79c..a5a2c58 100644 --- a/cmd/cc-boltinit/main.go +++ b/cmd/cc-boltinit/main.go @@ -36,4 +36,9 @@ func main() { ct3, _ := db.Get(query) fmt.Printf("Found config item: %s value: %s \n", ct3.Name, ct3.Value) + // Get all config items: + cis, _ := db.GetAll("Formbuilder") + for _, configItem := range cis { + fmt.Printf("%s - %s:%s\n", configItem.Application, configItem.Name, configItem.Value) + } } diff --git a/datastores/boltdb.go b/datastores/boltdb.go index a0ce53f..ec33022 100644 --- a/datastores/boltdb.go +++ b/datastores/boltdb.go @@ -32,9 +32,8 @@ func (store BoltDB) Get(configItem ConfigItem) (ConfigItem, error) { // Our return item: retval := ConfigItem{} - // Need to decide on appropriate nesting structure for BoltDB... - // Perhaps {appname} or {} -> Configitems? - // ^ this means 'global' + // TODO: Get global first, then get application, then get application + machine + // Get the key based on the application err = db.View(func(tx *bolt.Tx) error { // Get the item from the bucket with the app name b := tx.Bucket([]byte(configItem.Application)) @@ -55,6 +54,44 @@ func (store BoltDB) Get(configItem ConfigItem) (ConfigItem, error) { return retval, err } +func (store BoltDB) GetAll(application string) ([]ConfigItem, error) { + // Open the database: + db, err := bolt.Open(store.Database, 0600, nil) + checkErr(err) + defer db.Close() + + // Our return items: + retval := []ConfigItem{} + + // TODO: Get global first, then get application, then get application + machine + // Get the key based on the application + err = db.View(func(tx *bolt.Tx) error { + + // Get the items from the bucket with the app name + b := tx.Bucket([]byte(application)) + + c := b.Cursor() + + for k, v := c.First(); k != nil; k, v = c.Next() { + + // Unmarshal data into our config item + ci := ConfigItem{} + if err := json.Unmarshal(v, &ci); err != nil { + return err + } + + // Add the item to our list of config items + retval = append(retval, ci) + } + + return nil + }) + + checkErr(err) + + return retval, err +} + func (store BoltDB) Set(configItem ConfigItem) error { // Open the database: