Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added possibility for a different database address than localhost:3306 #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var defaults = Configuration{
DbUser: "db_user",
DbPassword: "db_pw",
DbName: "bd_name",
DbAddress: "db_address",
// PKG_NAME gives name of the package using the stucts
PkgName: "DbStructs",
// TAG_LABEL produces tags commonly used to match database field names with Go struct
Expand All @@ -22,6 +23,7 @@ Or by a JSON file using the json flag `struct-create --json=test.json`
{
"db_user": "db_user",
"db_password": "db_pass",
"db_address": "db_address",
"db_name": "db_name",
"pkg_name": "JsonTest",
"tag_label": "db"
Expand Down Expand Up @@ -97,4 +99,4 @@ type Job struct{
UpdatedAt time.Time `db:"updated_at"`
CreatedAt time.Time `db:"created_at"`
}
```
```
19 changes: 12 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ import (
"errors"
"flag"
"fmt"
_ "github.com/go-sql-driver/mysql"
"log"
"os"
"strings"

_ "github.com/go-sql-driver/mysql"
)

var defaults = Configuration{
DbUser: "db_user",
DbUser: "db_user",
DbPassword: "db_pw",
DbName: "bd_name",
PkgName: "DbStructs",
TagLabel: "db",
DbName: "bd_name",
PkgName: "DbStructs",
TagLabel: "db",
}

var config Configuration
Expand All @@ -26,6 +27,7 @@ type Configuration struct {
DbUser string `json:"db_user"`
DbPassword string `json:"db_password"`
DbName string `json:"db_name"`
DbAddress string `json:"db_address"`
// PkgName gives name of the package using the stucts
PkgName string `json:"pkg_name"`
// TagLabel produces tags commonly used to match database field names with Go struct members
Expand Down Expand Up @@ -102,7 +104,10 @@ func writeStructs(schemas []ColumnSchema) (int, error) {
}

func getSchema() []ColumnSchema {
conn, err := sql.Open("mysql", config.DbUser+":"+config.DbPassword+"@/information_schema")
if config.DbAddress != "" {
config.DbAddress = "tcp(" + config.DbAddress + ")"
}
conn, err := sql.Open("mysql", config.DbUser+":"+config.DbPassword+"@"+config.DbAddress+"/information_schema")
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -184,7 +189,7 @@ var configFile = flag.String("json", "", "Config file")

func main() {
flag.Parse()

if len(*configFile) > 0 {
f, err := os.Open(*configFile)
if err != nil {
Expand Down