restructure
This commit is contained in:
@@ -4,12 +4,8 @@ import (
|
||||
"log"
|
||||
"sort"
|
||||
|
||||
// init dependencies
|
||||
_ "atom/database/migrations"
|
||||
_ "atom/providers"
|
||||
|
||||
"atom/container"
|
||||
"atom/contracts"
|
||||
"github.com/rogeecn/atom/container"
|
||||
"github.com/rogeecn/atom/contracts"
|
||||
|
||||
"github.com/go-gormigrate/gormigrate/v2"
|
||||
"github.com/spf13/cobra"
|
||||
@@ -17,19 +13,21 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// migrateCmd represents the migrate command
|
||||
var migrateCmd = &cobra.Command{
|
||||
Use: "migrate",
|
||||
Short: "migrate database tables",
|
||||
Long: `migrate database tables`,
|
||||
}
|
||||
|
||||
func init() {
|
||||
func WithMigration(rootCmd *cobra.Command) *cobra.Command {
|
||||
rootCmd.AddCommand(migrateCmd)
|
||||
migrateCmd.AddCommand(migrateUpCmd)
|
||||
migrateCmd.AddCommand(migrateDownCmd)
|
||||
|
||||
migrateCmd.PersistentFlags().StringVar(&migrateToId, "to", "", "migration to id")
|
||||
|
||||
return rootCmd
|
||||
}
|
||||
|
||||
// migrateCmd represents the migrate command
|
||||
var migrateCmd = &cobra.Command{
|
||||
Use: "migrate",
|
||||
Short: "migrate database tables",
|
||||
Long: `migrate database tables`,
|
||||
}
|
||||
|
||||
var migrateToId string
|
||||
@@ -50,7 +48,6 @@ var migrateUpCmd = &cobra.Command{
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return container.Container.Invoke(func(mi MigrationInfo) error {
|
||||
m := gormigrate.New(mi.DB, gormigrate.DefaultOptions, sortedMigrations(mi.Migrations))
|
||||
|
||||
if len(migrateToId) > 0 {
|
||||
log.Printf("migrate up to [%s]\n", migrateToId)
|
||||
return m.MigrateTo(migrateToId)
|
||||
|
||||
27
cmd/model.go
27
cmd/model.go
@@ -4,24 +4,29 @@ Copyright © 2023 NAME HERE <EMAIL ADDRESS>
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"atom/container"
|
||||
"atom/providers/config"
|
||||
"atom/providers/database"
|
||||
"errors"
|
||||
"log"
|
||||
|
||||
"github.com/glebarez/sqlite"
|
||||
"github.com/rogeecn/atom/container"
|
||||
"github.com/spf13/cobra"
|
||||
"go.uber.org/dig"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gen"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func WithModel(rootCmd *cobra.Command) *cobra.Command {
|
||||
rootCmd.AddCommand(modelCmd)
|
||||
return rootCmd
|
||||
}
|
||||
|
||||
// MigrationInfo http service container
|
||||
type GenQueryGenerator struct {
|
||||
dig.In
|
||||
|
||||
Config *config.Config
|
||||
DB *gorm.DB
|
||||
DB *gorm.DB
|
||||
}
|
||||
|
||||
// // Dynamic SQL
|
||||
@@ -39,18 +44,18 @@ var modelCmd = &cobra.Command{
|
||||
return container.Container.Invoke(func(gq GenQueryGenerator) error {
|
||||
var tables []string
|
||||
|
||||
switch gq.Config.Database.Driver {
|
||||
case database.DriverMySQL:
|
||||
switch gq.DB.Dialector.Name() {
|
||||
case mysql.Dialector{}.Name():
|
||||
err := gq.DB.Raw("show tables").Scan(&tables).Error
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
case database.DriverPostgres:
|
||||
case postgres.Dialector{}.Name():
|
||||
err := gq.DB.Raw("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'").Scan(&tables).Error
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
case database.DriverSQLite:
|
||||
case sqlite.DriverName:
|
||||
err := gq.DB.Raw("SELECT name FROM sqlite_master WHERE type='table'").Scan(&tables).Error
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
@@ -89,7 +94,3 @@ var modelCmd = &cobra.Command{
|
||||
})
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(modelCmd)
|
||||
}
|
||||
|
||||
39
cmd/root.go
39
cmd/root.go
@@ -1,39 +0,0 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
_ "atom/providers"
|
||||
"log"
|
||||
|
||||
"atom/container"
|
||||
"atom/services/http"
|
||||
"atom/utils"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "atom",
|
||||
Short: "atom",
|
||||
Long: `the app long description`,
|
||||
Version: fmt.Sprintf("\nVersion: %s\nGitHash: %s\nBuildAt: %s\n", utils.Version, utils.GitHash, utils.BuildAt),
|
||||
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
||||
log.Println("using config file: ", utils.ShareConfigFile)
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return container.Container.Invoke(http.Serve)
|
||||
},
|
||||
}
|
||||
|
||||
func Execute() {
|
||||
err := rootCmd.Execute()
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
rootCmd.PersistentFlags().StringVarP(&utils.ShareConfigFile, "config", "c", "config.toml", "config file")
|
||||
}
|
||||
20
cmd/seed.go
20
cmd/seed.go
@@ -1,22 +1,22 @@
|
||||
/*
|
||||
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
_ "atom/database/seeders"
|
||||
_ "atom/providers"
|
||||
|
||||
"atom/container"
|
||||
"atom/contracts"
|
||||
"log"
|
||||
|
||||
"github.com/rogeecn/atom/container"
|
||||
"github.com/rogeecn/atom/contracts"
|
||||
|
||||
"github.com/brianvoe/gofakeit/v6"
|
||||
"github.com/spf13/cobra"
|
||||
"go.uber.org/dig"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func WithSeeder(rootCmd *cobra.Command) *cobra.Command {
|
||||
rootCmd.AddCommand(seedCmd)
|
||||
return rootCmd
|
||||
}
|
||||
|
||||
// seedCmd represents the seed command
|
||||
var seedCmd = &cobra.Command{
|
||||
Use: "seed",
|
||||
@@ -40,10 +40,6 @@ var seedCmd = &cobra.Command{
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(seedCmd)
|
||||
}
|
||||
|
||||
type SeedersContainer struct {
|
||||
dig.In
|
||||
|
||||
|
||||
Reference in New Issue
Block a user