Skip to content

Instantly share code, notes, and snippets.

@redhajuanda
Created September 11, 2020 15:03
Show Gist options
  • Save redhajuanda/a802ed3b59d15da39609ce4e3c6ef999 to your computer and use it in GitHub Desktop.
Save redhajuanda/a802ed3b59d15da39609ce4e3c6ef999 to your computer and use it in GitHub Desktop.
package main
import (
"database/sql"
"flag"
"fmt"
"log"
"os"
"github.com/redhajuanda/goseeder/seeds"
_ "github.com/go-sql-driver/mysql"
"github.com/joho/godotenv"
)
func main() {
godotenv.Load()
handleArgs()
}
func handleArgs() {
flag.Parse()
args := flag.Args()
if len(args) >= 1 {
switch args[0] {
case "seed":
connString := fmt.Sprintf("%v:%v@tcp(%v:%v)/%v?charset=utf8&parseTime=True&loc=Local&multiStatements=true", os.Getenv("DB_USERNAME"), os.Getenv("DB_PASSWORD"), os.Getenv("DB_HOST"), os.Getenv("DB_PORT"), os.Getenv("DB_NAME"))
// connect DB
db, err := sql.Open("mysql", connString)
if err != nil {
log.Fatalf("Error opening DB: %v", err)
}
seeds.Execute(db, args[1:]...)
os.Exit(0)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment