Last active
June 7, 2020 06:37
-
-
Save leejh3224/39973f822aac66cc9ce205d081ea1d80 to your computer and use it in GitHub Desktop.
generate go struct from database and add json tag for each struct
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"log" | |
"os" | |
"os/exec" | |
"path" | |
"path/filepath" | |
"strings" | |
"github.com/iancoleman/strcase" | |
"github.com/joho/godotenv" | |
) | |
// Command to add models from database and add json tag for model definitions. | |
func main() { | |
err := godotenv.Load() | |
if err != nil { | |
log.Fatalf("Error loading .env file: %v", err) | |
} | |
cwd, err := os.Getwd() | |
if err != nil { | |
log.Fatalf("failed to get working directory: %v", err) | |
} | |
modelPath := path.Join(cwd, "internal/model") | |
clean(modelPath) | |
execTablesToGo(modelPath) | |
execGoModifyTags(modelPath) | |
} | |
func clean(path string) { | |
fmt.Println("removing previous model files...") | |
_files, err := filepath.Glob(path + "/*.go") | |
if err != nil { | |
fmt.Printf("failed to find file with pattern: %v", err) | |
} | |
for _, file := range _files { | |
if err = os.Remove(file); err != nil { | |
fmt.Printf("failed to delete file: %v", err) | |
} | |
} | |
if err != nil { | |
fmt.Printf("failed to remove files: %v\n", err) | |
} | |
} | |
func execTablesToGo(path string) { | |
cwd, err := os.Getwd() | |
if err != nil { | |
log.Fatalf("failed to get working directory: %v", err) | |
} | |
host := os.Getenv("DB_HOST") | |
username := os.Getenv("DB_USERNAME") | |
password := os.Getenv("DB_PASSWORD") | |
database := os.Getenv("DB_DATABASE") | |
if host == "" || username == "" || password == "" || database == "" { | |
log.Fatalf(`check database options! | |
host: %s | |
username: %s | |
password: %s | |
database: %s`, host, username, password, database) | |
} | |
cmd := exec.Command("go", "run", "github.com/fraenky8/tables-to-go", "-v", "-t", "mysql", "-h", host, "-d", database, "-u", username, "-p", password, "-of", path, "-pn", "model", "-fn-format", "s") | |
cmd.Dir = cwd | |
cmd.Stdout = os.Stdout | |
fmt.Printf("> executing %s\n", cmd.String()) | |
if err := cmd.Run(); err != nil { | |
log.Fatalf("failed to run command: %v", err) | |
} | |
} | |
func execGoModifyTags(path string) { | |
cwd, err := os.Getwd() | |
if err != nil { | |
log.Fatalf("failed to get working directory: %v", err) | |
} | |
files, err := ioutil.ReadDir(path) | |
if err != nil { | |
log.Fatalf("failed to read directory: %v", err) | |
} | |
for _, file := range files { | |
structName := strings.TrimSuffix(file.Name(), filepath.Ext(file.Name())) | |
structName = strcase.ToCamel(structName) | |
fileName := fmt.Sprintf("internal/model/%s", file.Name()) | |
cmd := exec.Command("gomodifytags", "-file", fileName, "-struct", structName, "-add-tags", "json", "-w", "--skip-unexported") | |
cmd.Dir = cwd | |
cmd.Stdout = os.Stdout | |
fmt.Printf("> executing %s\n", cmd.String()) | |
if err := cmd.Run(); err != nil { | |
log.Fatalf("failed to run command: %v", err) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment