Skip to content

Commit

Permalink
Added boltdb
Browse files Browse the repository at this point in the history
Introduced initdb command. Currently only loads in categories.
Changed list categories to use boltdb
  • Loading branch information
HakShak committed Oct 7, 2016
1 parent fad094c commit 5df54e6
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ _testmain.go

mame*.xml
mame*lx.zip
sanemame.db
27 changes: 11 additions & 16 deletions cmd/categories.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package cmd

import (
"fmt"
"github.com/HakShak/sanemame/db"
"github.com/HakShak/sanemame/mamexml"
"github.com/boltdb/bolt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"log"
"os"
"text/tabwriter"
Expand All @@ -15,32 +18,24 @@ var categoriesCmd = &cobra.Command{
Short: "List categories from Catver.ini",
Long: `Load and list the set of categories from Catver.ini`,
Run: func(cmd *cobra.Command, args []string) {
categories, err := mamexml.LoadCatverIni("Catver.ini")
dbPath := viper.GetString(DatabaseLocation)
boltDb, err := bolt.Open(dbPath, 0600, nil)
if err != nil {
log.Fatal(err)
}
defer boltDb.Close()

categories := db.GetCategories(boltDb)

tw := new(tabwriter.Writer)
tw.Init(os.Stdout, 0, 0, 2, ' ', 0)
fmt.Fprintln(tw, "Primary\tSecondary")
fmt.Fprintln(tw, "-------\t---------")

categorySet := make(map[string]map[string]bool)
fmt.Fprintln(tw, "Category")
fmt.Fprintln(tw, "--------")

for _, category := range categories {
if _, ok := categorySet[category.Primary]; !ok {
categorySet[category.Primary] = make(map[string]bool)
}
categorySet[category.Primary][category.Secondary] = category.Mature
fmt.Fprintf(tw, "%s\n", category)
}

for primary, secondarySet := range categorySet {
for secondary, _ := range secondarySet {
if secondary != "" {
fmt.Fprintf(tw, "%s\t%s\n", primary, secondary)
}
}
}
fmt.Fprintln(tw)
tw.Flush()
},
Expand Down
47 changes: 47 additions & 0 deletions cmd/initdb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cmd

import (
"log"

"github.com/HakShak/sanemame/db"
"github.com/boltdb/bolt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// initdbCmd represents the initdb command
var initdbCmd = &cobra.Command{
Use: "initdb",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
dbPath := viper.GetString(DatabaseLocation)
boltDb, err := bolt.Open(dbPath, 0600, nil)
if err != nil {
log.Fatal(err)
}
defer boltDb.Close()

db.UpdateCategories(boltDb, "Catver.ini")
},
}

func init() {
RootCmd.AddCommand(initdbCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// initdbCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// initdbCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")

}
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func Execute() {

const MameRepo = "mame.repo"
const GithubReleasesApi = "github.api.releases"
const DatabaseLocation = "db.path"

func init() {
cobra.OnInitialize(initConfig)
Expand All @@ -47,6 +48,7 @@ func init() {

viper.SetDefault(MameRepo, "mamedev/mame")
viper.SetDefault(GithubReleasesApi, "https://api.github.com/repos/%s/releases")
viper.SetDefault(DatabaseLocation, "sanemame.db")
log.SetFlags(0)
}

Expand Down
69 changes: 69 additions & 0 deletions db/categories.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package db

import (
"encoding/json"
"github.com/HakShak/sanemame/mamexml"
"github.com/boltdb/bolt"
"log"
)

const CategoryMachines = "category-machines"

func UpdateCategories(db *bolt.DB, fileName string) {
categories, err := mamexml.LoadCatverIni(fileName)
if err != nil {
log.Fatal(err)
}

categorySet := make(map[string][]string)

for machine, category := range categories {
categoryKey := category.Primary
if len(category.Secondary) > 0 {
categoryKey += "-" + category.Secondary
}
categorySet[categoryKey] = append(categorySet[categoryKey], machine)
}

err = db.Update(func(tx *bolt.Tx) error {
bucket, err := tx.CreateBucketIfNotExists([]byte(CategoryMachines))
if err != nil {
return err
}

for categoryKey, machineList := range categorySet {
machineListBytes, err := json.Marshal(machineList)
if err != nil {
return err
}
err = bucket.Put([]byte(categoryKey), machineListBytes)
if err != nil {
return err
}
}
return nil
})

if err != nil {
log.Fatal(err)
}
}

func GetCategories(db *bolt.DB) []string {
var result []string
err := db.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte(CategoryMachines))

bucket.ForEach(func(k, v []byte) error {
result = append(result, string(k))
return nil
})

return nil
})
if err != nil {
log.Fatal(err)
}

return result
}

0 comments on commit 5df54e6

Please sign in to comment.