Skip to content

Commit

Permalink
code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mayswind committed Aug 18, 2024
1 parent 6fcb0a2 commit e86d4e0
Show file tree
Hide file tree
Showing 73 changed files with 1,404 additions and 1,344 deletions.
14 changes: 14 additions & 0 deletions cmd/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cmd

import (
"github.com/urfave/cli/v2"

"github.com/mayswind/ezbookkeeping/pkg/core"
)

func bindAction(fn core.CliHandlerFunc) cli.ActionFunc {
return func(cliCtx *cli.Context) error {
c := core.WrapCilContext(cliCtx)
return fn(c)
}
}
21 changes: 11 additions & 10 deletions cmd/cron_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/urfave/cli/v2"

"github.com/mayswind/ezbookkeeping/pkg/core"
"github.com/mayswind/ezbookkeeping/pkg/cron"
"github.com/mayswind/ezbookkeeping/pkg/log"
)
Expand All @@ -17,12 +18,12 @@ var CronJobs = &cli.Command{
{
Name: "list",
Usage: "List all enabled cron jobs",
Action: listAllCronJobs,
Action: bindAction(listAllCronJobs),
},
{
Name: "run",
Usage: "Run specified cron job",
Action: runCronJob,
Action: bindAction(runCronJob),
Flags: []cli.Flag{
&cli.StringFlag{
Name: "name",
Expand All @@ -35,24 +36,24 @@ var CronJobs = &cli.Command{
},
}

func listAllCronJobs(c *cli.Context) error {
func listAllCronJobs(c *core.CliContext) error {
config, err := initializeSystem(c)

if err != nil {
return err
}

err = cron.InitializeCronJobSchedulerContainer(config, false)
err = cron.InitializeCronJobSchedulerContainer(c, config, false)

if err != nil {
log.BootErrorf("[cron_jobs.listAllCronJobs] initializes cron job scheduler failed, because %s", err.Error())
log.BootErrorf(c, "[cron_jobs.listAllCronJobs] initializes cron job scheduler failed, because %s", err.Error())
return err
}

cronJobs := cron.Container.GetAllJobs()

if len(cronJobs) < 1 {
log.BootErrorf("[cron_jobs.listAllCronJobs] there are no enabled cron jobs")
log.BootErrorf(c, "[cron_jobs.listAllCronJobs] there are no enabled cron jobs")
return err
}

Expand All @@ -71,25 +72,25 @@ func listAllCronJobs(c *cli.Context) error {
return nil
}

func runCronJob(c *cli.Context) error {
func runCronJob(c *core.CliContext) error {
config, err := initializeSystem(c)

if err != nil {
return err
}

err = cron.InitializeCronJobSchedulerContainer(config, false)
err = cron.InitializeCronJobSchedulerContainer(c, config, false)

if err != nil {
log.BootErrorf("[cron_jobs.runCronJob] initializes cron job scheduler failed, because %s", err.Error())
log.BootErrorf(c, "[cron_jobs.runCronJob] initializes cron job scheduler failed, because %s", err.Error())
return err
}

jobName := c.String("name")
err = cron.Container.SyncRunJobNow(jobName)

if err != nil {
log.BootErrorf("[cron_jobs.runCronJob] failed to run cron job \"%s\", because %s", jobName, err.Error())
log.BootErrorf(c, "[cron_jobs.runCronJob] failed to run cron job \"%s\", because %s", jobName, err.Error())
return err
}

Expand Down
35 changes: 18 additions & 17 deletions cmd/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"github.com/urfave/cli/v2"

"github.com/mayswind/ezbookkeeping/pkg/core"
"github.com/mayswind/ezbookkeeping/pkg/datastore"
"github.com/mayswind/ezbookkeeping/pkg/log"
"github.com/mayswind/ezbookkeeping/pkg/models"
Expand All @@ -16,32 +17,32 @@ var Database = &cli.Command{
{
Name: "update",
Usage: "Update database structure",
Action: updateDatabaseStructure,
Action: bindAction(updateDatabaseStructure),
},
},
}

func updateDatabaseStructure(c *cli.Context) error {
func updateDatabaseStructure(c *core.CliContext) error {
_, err := initializeSystem(c)

if err != nil {
return err
}

log.BootInfof("[database.updateDatabaseStructure] starting maintaining")
log.BootInfof(c, "[database.updateDatabaseStructure] starting maintaining")

err = updateAllDatabaseTablesStructure()
err = updateAllDatabaseTablesStructure(c)

if err != nil {
log.BootErrorf("[database.updateDatabaseStructure] update database table structure failed, because %s", err.Error())
log.BootErrorf(c, "[database.updateDatabaseStructure] update database table structure failed, because %s", err.Error())
return err
}

log.BootInfof("[database.updateDatabaseStructure] all tables maintained successfully")
log.BootInfof(c, "[database.updateDatabaseStructure] all tables maintained successfully")
return nil
}

func updateAllDatabaseTablesStructure() error {
func updateAllDatabaseTablesStructure(c *core.CliContext) error {
var err error

err = datastore.Container.UserStore.SyncStructs(new(models.User))
Expand All @@ -50,79 +51,79 @@ func updateAllDatabaseTablesStructure() error {
return err
}

log.BootInfof("[database.updateAllDatabaseTablesStructure] user table maintained successfully")
log.BootInfof(c, "[database.updateAllDatabaseTablesStructure] user table maintained successfully")

err = datastore.Container.UserStore.SyncStructs(new(models.TwoFactor))

if err != nil {
return err
}

log.BootInfof("[database.updateAllDatabaseTablesStructure] two-factor table maintained successfully")
log.BootInfof(c, "[database.updateAllDatabaseTablesStructure] two-factor table maintained successfully")

err = datastore.Container.UserStore.SyncStructs(new(models.TwoFactorRecoveryCode))

if err != nil {
return err
}

log.BootInfof("[database.updateAllDatabaseTablesStructure] two-factor recovery code table maintained successfully")
log.BootInfof(c, "[database.updateAllDatabaseTablesStructure] two-factor recovery code table maintained successfully")

err = datastore.Container.TokenStore.SyncStructs(new(models.TokenRecord))

if err != nil {
return err
}

log.BootInfof("[database.updateAllDatabaseTablesStructure] token record table maintained successfully")
log.BootInfof(c, "[database.updateAllDatabaseTablesStructure] token record table maintained successfully")

err = datastore.Container.UserDataStore.SyncStructs(new(models.Account))

if err != nil {
return err
}

log.BootInfof("[database.updateAllDatabaseTablesStructure] account table maintained successfully")
log.BootInfof(c, "[database.updateAllDatabaseTablesStructure] account table maintained successfully")

err = datastore.Container.UserDataStore.SyncStructs(new(models.Transaction))

if err != nil {
return err
}

log.BootInfof("[database.updateAllDatabaseTablesStructure] transaction table maintained successfully")
log.BootInfof(c, "[database.updateAllDatabaseTablesStructure] transaction table maintained successfully")

err = datastore.Container.UserDataStore.SyncStructs(new(models.TransactionCategory))

if err != nil {
return err
}

log.BootInfof("[database.updateAllDatabaseTablesStructure] transaction category table maintained successfully")
log.BootInfof(c, "[database.updateAllDatabaseTablesStructure] transaction category table maintained successfully")

err = datastore.Container.UserDataStore.SyncStructs(new(models.TransactionTag))

if err != nil {
return err
}

log.BootInfof("[database.updateAllDatabaseTablesStructure] transaction tag table maintained successfully")
log.BootInfof(c, "[database.updateAllDatabaseTablesStructure] transaction tag table maintained successfully")

err = datastore.Container.UserDataStore.SyncStructs(new(models.TransactionTagIndex))

if err != nil {
return err
}

log.BootInfof("[database.updateAllDatabaseTablesStructure] transaction tag index table maintained successfully")
log.BootInfof(c, "[database.updateAllDatabaseTablesStructure] transaction tag index table maintained successfully")

err = datastore.Container.UserDataStore.SyncStructs(new(models.TransactionTemplate))

if err != nil {
return err
}

log.BootInfof("[database.updateAllDatabaseTablesStructure] transaction template table maintained successfully")
log.BootInfof(c, "[database.updateAllDatabaseTablesStructure] transaction template table maintained successfully")

return nil
}
33 changes: 16 additions & 17 deletions cmd/initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import (
"encoding/json"
"os"

"github.com/urfave/cli/v2"

"github.com/mayswind/ezbookkeeping/pkg/core"
"github.com/mayswind/ezbookkeeping/pkg/datastore"
"github.com/mayswind/ezbookkeeping/pkg/duplicatechecker"
"github.com/mayswind/ezbookkeeping/pkg/exchangerates"
Expand All @@ -17,48 +16,48 @@ import (
"github.com/mayswind/ezbookkeeping/pkg/uuid"
)

func initializeSystem(c *cli.Context) (*settings.Config, error) {
func initializeSystem(c *core.CliContext) (*settings.Config, error) {
var err error
configFilePath := c.String("conf-path")
isDisableBootLog := c.Bool("no-boot-log")

if configFilePath != "" {
if _, err = os.Stat(configFilePath); err != nil {
if !isDisableBootLog {
log.BootErrorf("[initializer.initializeSystem] cannot load configuration from custom config path %s, because file not exists", configFilePath)
log.BootErrorf(c, "[initializer.initializeSystem] cannot load configuration from custom config path %s, because file not exists", configFilePath)
}
return nil, err
}

if !isDisableBootLog {
log.BootInfof("[initializer.initializeSystem] will loading configuration from custom config path %s", configFilePath)
log.BootInfof(c, "[initializer.initializeSystem] will loading configuration from custom config path %s", configFilePath)
}
} else {
configFilePath, err = settings.GetDefaultConfigFilePath()

if err != nil {
if !isDisableBootLog {
log.BootErrorf("[initializer.initializeSystem] cannot get default configuration path, because %s", err.Error())
log.BootErrorf(c, "[initializer.initializeSystem] cannot get default configuration path, because %s", err.Error())
}
return nil, err
}

if !isDisableBootLog {
log.BootInfof("[initializer.initializeSystem] will load configuration from default config path %s", configFilePath)
log.BootInfof(c, "[initializer.initializeSystem] will load configuration from default config path %s", configFilePath)
}
}

config, err := settings.LoadConfiguration(configFilePath)

if err != nil {
if !isDisableBootLog {
log.BootErrorf("[initializer.initializeSystem] cannot load configuration, because %s", err.Error())
log.BootErrorf(c, "[initializer.initializeSystem] cannot load configuration, because %s", err.Error())
}
return nil, err
}

if config.SecretKeyNoSet {
log.BootWarnf("[initializer.initializeSystem] \"secret_key\" in config file is not set, please change it to keep your user data safe")
log.BootWarnf(c, "[initializer.initializeSystem] \"secret_key\" in config file is not set, please change it to keep your user data safe")
}

settings.SetCurrentConfig(config)
Expand All @@ -67,7 +66,7 @@ func initializeSystem(c *cli.Context) (*settings.Config, error) {

if err != nil {
if !isDisableBootLog {
log.BootErrorf("[initializer.initializeSystem] initializes data store failed, because %s", err.Error())
log.BootErrorf(c, "[initializer.initializeSystem] initializes data store failed, because %s", err.Error())
}
return nil, err
}
Expand All @@ -76,7 +75,7 @@ func initializeSystem(c *cli.Context) (*settings.Config, error) {

if err != nil {
if !isDisableBootLog {
log.BootErrorf("[initializer.initializeSystem] sets logger configuration failed, because %s", err.Error())
log.BootErrorf(c, "[initializer.initializeSystem] sets logger configuration failed, because %s", err.Error())
}
return nil, err
}
Expand All @@ -85,7 +84,7 @@ func initializeSystem(c *cli.Context) (*settings.Config, error) {

if err != nil {
if !isDisableBootLog {
log.BootErrorf("[initializer.initializeSystem] initializes object storage failed, because %s", err.Error())
log.BootErrorf(c, "[initializer.initializeSystem] initializes object storage failed, because %s", err.Error())
}
return nil, err
}
Expand All @@ -94,7 +93,7 @@ func initializeSystem(c *cli.Context) (*settings.Config, error) {

if err != nil {
if !isDisableBootLog {
log.BootErrorf("[initializer.initializeSystem] initializes uuid generator failed, because %s", err.Error())
log.BootErrorf(c, "[initializer.initializeSystem] initializes uuid generator failed, because %s", err.Error())
}
return nil, err
}
Expand All @@ -103,7 +102,7 @@ func initializeSystem(c *cli.Context) (*settings.Config, error) {

if err != nil {
if !isDisableBootLog {
log.BootErrorf("[initializer.initializeSystem] initializes duplicate checker failed, because %s", err.Error())
log.BootErrorf(c, "[initializer.initializeSystem] initializes duplicate checker failed, because %s", err.Error())
}
return nil, err
}
Expand All @@ -112,7 +111,7 @@ func initializeSystem(c *cli.Context) (*settings.Config, error) {

if err != nil {
if !isDisableBootLog {
log.BootErrorf("[initializer.initializeSystem] initializes mailer failed, because %s", err.Error())
log.BootErrorf(c, "[initializer.initializeSystem] initializes mailer failed, because %s", err.Error())
}
return nil, err
}
Expand All @@ -121,15 +120,15 @@ func initializeSystem(c *cli.Context) (*settings.Config, error) {

if err != nil {
if !isDisableBootLog {
log.BootErrorf("[initializer.initializeSystem] initializes exchange rates data source failed, because %s", err.Error())
log.BootErrorf(c, "[initializer.initializeSystem] initializes exchange rates data source failed, because %s", err.Error())
}
return nil, err
}

cfgJson, _ := json.Marshal(getConfigWithoutSensitiveData(config))

if !isDisableBootLog {
log.BootInfof("[initializer.initializeSystem] has loaded configuration %s", cfgJson)
log.BootInfof(c, "[initializer.initializeSystem] has loaded configuration %s", cfgJson)
}

return config, nil
Expand Down
5 changes: 3 additions & 2 deletions cmd/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/urfave/cli/v2"

"github.com/mayswind/ezbookkeeping/pkg/core"
"github.com/mayswind/ezbookkeeping/pkg/utils"
)

Expand All @@ -16,7 +17,7 @@ var SecurityUtils = &cli.Command{
{
Name: "gen-secret-key",
Usage: "Generate a random secret key",
Action: genSecretKey,
Action: bindAction(genSecretKey),
Flags: []cli.Flag{
&cli.IntFlag{
Name: "length",
Expand All @@ -30,7 +31,7 @@ var SecurityUtils = &cli.Command{
},
}

func genSecretKey(c *cli.Context) error {
func genSecretKey(c *core.CliContext) error {
length := c.Int("length")

if length <= 0 {
Expand Down
Loading

0 comments on commit e86d4e0

Please sign in to comment.