func main() { // preset base path of app config := cfg.NewConfig() basepath, _ := os.Getwd() config.BasePath = basepath if len(os.Args) == 1 || os.Args[1] == "help" || os.Args[1] == "-h" { usage() os.Exit(0) } programmName = os.Args[0] commandName = os.Args[1] os.Args = os.Args[1:] attributes := commands.Attributes{} attributes.Username = flag.String("username", "", "") attributes.Email = flag.String("email", "", "") attributes.Id = flag.Int("id", 0, "") attributes.Title = flag.String("title", "", "") attributes.Slug = flag.String("slug", "", "") attributes.Amount = flag.Float64("amount", 0.0, "") attributes.ItemType = flag.String("type", "", "") attributes.CategoryTitle = flag.String("category", "", "") attributes.Comment = flag.String("comment", "", "") flag.Parse() commands := map[string]func() commands.Command{ "init": func() commands.Command { return commands.NewInitCommand(attributes) }, "balance": func() commands.Command { return commands.NewBalanceCommand(attributes) }, "add-item": func() commands.Command { return commands.NewAddItemCommand(attributes) }, "edit-item": func() commands.Command { return commands.NewEditItemCommand(attributes) }, "delete-item": func() commands.Command { return commands.NewDeleteItemCommand(attributes) }, "list-item": func() commands.Command { return commands.NewListItemCommand(attributes) }, "add-category": func() commands.Command { return commands.NewAddCategoryCommand(attributes) }, "edit-category": func() commands.Command { return commands.NewEditCategoryCommand(attributes) }, "delete-category": func() commands.Command { return commands.NewDeleteCategoryCommand(attributes) }, } _, ok := commands[commandName] if ok { command := commands[commandName]() err := command.Validate() if err != nil { command.Usage(err) os.Exit(0) } err = command.Run() if err != nil { command.Usage(err) os.Exit(0) } } else { usage() } }
// Return connection object func Connection() *gorm.DB { config := cfg.NewConfig() database, err := config.GetConfig().Get("development.open") if err != nil { log.Fatal(err) } if db == nil { // @todo: Get connect data from app/dbconf.yml DB, err := gorm.Open("sqlite3", filepath.Join(config.BasePath, database)) if err != nil { log.Fatal(err) } db = &DB } return db }
func prepareConfig() { config := cfg.NewConfig() basepath, _ := os.Getwd() config.BasePath = filepath.Join(basepath, "../../../") }