Skip to content

Commit

Permalink
change project structure to comply with golang-standards/project-layout
Browse files Browse the repository at this point in the history
  • Loading branch information
ZelvaMan committed Apr 28, 2024
1 parent 6401475 commit 64f3d74
Show file tree
Hide file tree
Showing 31 changed files with 354 additions and 79 deletions.
1 change: 0 additions & 1 deletion .idea/sqldialects.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion cmd/common.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import "github.com/keenmate/db-gen/common"
import (
"github.com/keenmate/db-gen/private/common"
)

const (
keyDebug = "debug"
Expand Down
38 changes: 19 additions & 19 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ package cmd

import (
"fmt"
"github.com/keenmate/db-gen/common"
dbGen "github.com/keenmate/db-gen/dbGen"
common2 "github.com/keenmate/db-gen/private/common"
dbGen2 "github.com/keenmate/db-gen/private/dbGen"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"log"
)

const keyUseRoutinesFile = "useRoutinesFile"

var generateFlags = []common.FlagArgument{
common.NewBoolFlag(keyUseRoutinesFile, "", false, "Use routines file to generate code"),
var generateFlags = []common2.FlagArgument{
common2.NewBoolFlag(keyUseRoutinesFile, "", false, "Use routines file to generate code"),
}

var generateCmd = &cobra.Command{
Expand All @@ -27,71 +27,71 @@ var generateCmd = &cobra.Command{
`,
Run: func(cmd *cobra.Command, args []string) {
common.BindFlags(cmd, append(commonFlags, generateFlags...))
_, err := dbGen.ReadConfig(viper.GetString(keyConfig))
common2.BindFlags(cmd, append(commonFlags, generateFlags...))
_, err := dbGen2.ReadConfig(viper.GetString(keyConfig))
if err != nil {
common.Exit("configuration error: %s", err)
common2.Exit("configuration error: %s", err)
}

viper.AutomaticEnv() // read in environment variables that match

err = doGenerate()
if err != nil {
common.Exit(err.Error())
common2.Exit(err.Error())
}
},
}

func init() {
rootCmd.AddCommand(generateCmd)

common.DefineFlags(generateCmd, append(commonFlags, generateFlags...))
common2.DefineFlags(generateCmd, append(commonFlags, generateFlags...))
}

func doGenerate() error {
log.Printf("Getting configurations...")

config, err := dbGen.GetAndValidateConfig()
config, err := dbGen2.GetAndValidateConfig()
if err != nil {
return fmt.Errorf("error getting config %s", err)
}

common.LogDebug("Debug logging is enabled")
common2.LogDebug("Debug logging is enabled")

var routines []dbGen.DbRoutine
var routines []dbGen2.DbRoutine

log.Printf("Getting routines...")
routines, err = dbGen.GetRoutines(config)
routines, err = dbGen2.GetRoutines(config)
if err != nil {
return fmt.Errorf("error getting routines: %s", err)
}
log.Printf("Got %d routines", len(routines))

if config.Debug {
common.LogDebug("Saving to debug file...")
err = common.SaveToTempFile(routines, "dbRoutines")
common2.LogDebug("Saving to debug file...")
err = common2.SaveToTempFile(routines, "dbRoutines")
if err != nil {
return fmt.Errorf("error saving debug file: %s", err)
}
}

log.Printf("Preprocessing...")
processedFunctions, err := dbGen.Process(routines, config)
processedFunctions, err := dbGen2.Process(routines, config)
if err != nil {
return fmt.Errorf("error preprocessing: %s", err)
}
log.Printf("After preprocessing %d - %d = %d functions left", len(routines), len(routines)-len(processedFunctions), len(processedFunctions))

if config.Debug {
common.LogDebug("Saving to debug file...")
err = common.SaveToTempFile(processedFunctions, "mapped")
common2.LogDebug("Saving to debug file...")
err = common2.SaveToTempFile(processedFunctions, "mapped")
if err != nil {
return fmt.Errorf("error saving debug file: %s", err)
}
}

log.Printf("Generating...")
err = dbGen.Generate(processedFunctions, config)
err = dbGen2.Generate(processedFunctions, config)
if err != nil {
return fmt.Errorf("error generating: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cmd

import (
"github.com/keenmate/db-gen/dbGen"
"github.com/keenmate/db-gen/private/dbGen"
"github.com/spf13/cobra"
"os"
)
Expand Down
22 changes: 11 additions & 11 deletions cmd/routines.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package cmd

import (
"fmt"
"github.com/keenmate/db-gen/common"
dbGen "github.com/keenmate/db-gen/dbGen"
common2 "github.com/keenmate/db-gen/private/common"
dbGen2 "github.com/keenmate/db-gen/private/dbGen"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"log"
Expand All @@ -14,13 +14,13 @@ var getRoutinesCmd = &cobra.Command{
Short: "Get routines",
Long: "Get routines from database and save them to file to generate later",
Run: func(cmd *cobra.Command, args []string) {
common.BindFlags(cmd, commonFlags)
common2.BindFlags(cmd, commonFlags)

configLocation := viper.GetString("config")

_, err := dbGen.ReadConfig(configLocation)
_, err := dbGen2.ReadConfig(configLocation)
if err != nil {
common.Exit("configuration error: %s", err)
common2.Exit("configuration error: %s", err)
}

log.Printf("arguments: %s", args)
Expand All @@ -34,31 +34,31 @@ var getRoutinesCmd = &cobra.Command{
err = doGetRoutines()

if err != nil {
common.Exit(err.Error())
common2.Exit(err.Error())
}
},
}

func init() {
rootCmd.AddCommand(getRoutinesCmd)

common.DefineFlags(getRoutinesCmd, commonFlags)
common2.DefineFlags(getRoutinesCmd, commonFlags)
}

func doGetRoutines() error {
log.Printf("Getting configurations...")

config, err := dbGen.GetAndValidateConfig()
config, err := dbGen2.GetAndValidateConfig()
if err != nil {
return fmt.Errorf("error getting config %s", err)
}

common.LogDebug("Debug logging is enabled")
common2.LogDebug("Debug logging is enabled")

// because we use shared config, we need to set this
config.UseRoutinesFile = false
log.Printf("Getting routines...")
routines, err := dbGen.GetRoutines(config)
routines, err := dbGen2.GetRoutines(config)
if err != nil {
return fmt.Errorf("error getting routines: %s", err)
}
Expand All @@ -67,7 +67,7 @@ func doGetRoutines() error {

// TODO show what routines changed

err = common.SaveAsJson(config.RoutinesFile, routines)
err = common2.SaveAsJson(config.RoutinesFile, routines)
if err != nil {
return fmt.Errorf("error saving routines: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
_ "embed"
dbGen "github.com/keenmate/db-gen/dbGen"
"github.com/keenmate/db-gen/private/dbGen"
"github.com/spf13/cobra"
)

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
22 changes: 11 additions & 11 deletions dbGen/config.go → private/dbGen/config.go
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package dbGen
import (
"fmt"
"github.com/guregu/null/v5"
"github.com/keenmate/db-gen/common"
common2 "github.com/keenmate/db-gen/private/common"
"github.com/spf13/viper"
"os"
"path/filepath"
Expand Down Expand Up @@ -122,11 +122,11 @@ func GetAndValidateConfig() (*Config, error) {

config.GeneratedFileCase = strings.ToLower(config.GeneratedFileCase)

if !common.Contains(ValidCaseNormalized, config.GeneratedFileCase) {
if !common2.Contains(ValidCaseNormalized, config.GeneratedFileCase) {
return nil, fmt.Errorf(" '%s' is not valid case (maybe GeneratedFileCase is missing)", config.GeneratedFileCase)
}

common.LogDebug("Loaded configuration: \n%+v", config)
common2.LogDebug("Loaded configuration: \n%+v", config)
return config, nil
}

Expand Down Expand Up @@ -162,13 +162,13 @@ func ReadConfig(configLocation string) (string, error) {
}

if localConfigExists {
common.Log("Local config override loaded")
common2.Log("Local config override loaded")
}

return configLocation, nil
}

common.LogDebug("No configuration file set, trying default locations")
common2.LogDebug("No configuration file set, trying default locations")

for _, defaultConfigPath := range defaultConfigPaths {
fileExists, err := TryReadConfigFile(defaultConfigPath)
Expand All @@ -187,7 +187,7 @@ func ReadConfig(configLocation string) (string, error) {
}

if localConfigExists {
common.Log("Local config override loaded")
common2.Log("Local config override loaded")
}

return defaultConfigPath, nil
Expand All @@ -199,13 +199,13 @@ func ReadConfig(configLocation string) (string, error) {
}

func TryReadLocalConfig(configLocation string) (bool, error) {
common.LogDebug("Checking if local config exists")
common2.LogDebug("Checking if local config exists")

for _, path := range getPossibleLocalConfigs(configLocation) {
exists, err := TryReadConfigFile(path)

if exists {
common.LogDebug("Local config at %s loaded", path)
common2.LogDebug("Local config at %s loaded", path)
return exists, err
}
}
Expand All @@ -214,10 +214,10 @@ func TryReadLocalConfig(configLocation string) (bool, error) {
}

func TryReadConfigFile(configPath string) (bool, error) {
common.LogDebug("Trying to read config file: %s", configPath)
common2.LogDebug("Trying to read config file: %s", configPath)

// TODO this could hide some usefull errors, maybe log the reason in debug mode
if !common.FileIsReadable(configPath) {
if !common2.FileIsReadable(configPath) {
return false, nil
}

Expand All @@ -233,7 +233,7 @@ func TryReadConfigFile(configPath string) (bool, error) {
if err != nil {
return true, fmt.Errorf("reading configuration: %s", err)
}
common.LogDebug("Configuration file at %s loaded", configPath)
common2.LogDebug("Configuration file at %s loaded", configPath)

return true, nil
}
Expand Down
File renamed without changes.
12 changes: 6 additions & 6 deletions dbGen/database.go → private/dbGen/database.go
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package dbGen

import (
"fmt"
"github.com/keenmate/db-gen/common"
common2 "github.com/keenmate/db-gen/private/common"
"log"
"slices"
"strings"
Expand Down Expand Up @@ -41,7 +41,7 @@ const (

func GetRoutines(config *Config) ([]DbRoutine, error) {
if config.UseRoutinesFile {
common.LogDebug("Load routines from file %s", config.RoutinesFile)
common2.LogDebug("Load routines from file %s", config.RoutinesFile)
return LoadRoutinesFromFile(config)
}

Expand All @@ -50,7 +50,7 @@ func GetRoutines(config *Config) ([]DbRoutine, error) {

func LoadRoutinesFromFile(config *Config) ([]DbRoutine, error) {
routines := new([]DbRoutine)
err := common.LoadFromJson(config.RoutinesFile, routines)
err := common2.LoadFromJson(config.RoutinesFile, routines)
if err != nil {
return nil, fmt.Errorf("loading routines from file: %s", err)
}
Expand All @@ -60,7 +60,7 @@ func LoadRoutinesFromFile(config *Config) ([]DbRoutine, error) {

func getRoutinesFromDatabase(config *Config) ([]DbRoutine, error) {
log.Printf("Connecting to database...")
conn, err := common.Connect(config.ConnectionString)
conn, err := common2.Connect(config.ConnectionString)
if err != nil {

return nil, fmt.Errorf("error connecting to database: %s", err)
Expand Down Expand Up @@ -103,7 +103,7 @@ func getSchemas(config *Config) []string {
return schemas
}

func getFunctionsInSchema(conn *common.DbConn, schema string) ([]DbRoutine, error) {
func getFunctionsInSchema(conn *common2.DbConn, schema string) ([]DbRoutine, error) {
routines := new([]DbRoutine)

// I am coalescing
Expand Down Expand Up @@ -133,7 +133,7 @@ func getFunctionsInSchema(conn *common.DbConn, schema string) ([]DbRoutine, erro
return *routines, nil
}

func addParamsToRoutine(conn *common.DbConn, routine *DbRoutine) error {
func addParamsToRoutine(conn *common2.DbConn, routine *DbRoutine) error {
q := `
select ordinal_position::int,
parameter_name::text,
Expand Down
2 changes: 1 addition & 1 deletion dbGen/filter.go → private/dbGen/filter.go
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dbGen

import (
"github.com/keenmate/db-gen/common"
"github.com/keenmate/db-gen/private/common"
)

func FilterFunctions(routines *[]DbRoutine, config *Config) ([]DbRoutine, error) {
Expand Down
2 changes: 1 addition & 1 deletion dbGen/generator-functions.go → private/dbGen/generator-functions.go
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dbGen

import (
"github.com/keenmate/db-gen/common"
"github.com/keenmate/db-gen/private/common"
"text/template"
)

Expand Down
Loading

0 comments on commit 64f3d74

Please sign in to comment.