Added changes to autoapi main to main generator so application have the same parameter syntax as the generator, changed parameter name to match as well, added .project ide ignore to .gitignore

Colin Gagnon 3 years ago
parent
commit
db3adfef63
4 changed files with 59 additions and 12 deletions
  1. 4 1
      .gitignore
  2. 1 1
      README.md
  3. 52 8
      lib/maingenerator.go
  4. 2 2
      main.go

+ 4 - 1
.gitignore

@@ -19,4 +19,7 @@ _cgo_export.*
19 19
 
20 20
 _testmain.go
21 21
 
22
-*.exe
22
+*.exe
23
+
24
+# IDE Settings
25
+.project

+ 1 - 1
README.md

@@ -75,4 +75,4 @@ Your api will now be runnning on: http://localhost:8080 (by default)
75 75
 
76 76
 ## Contributors
77 77
 * [Olivier Tremblay](https://git.is-a-dev.com/otremblay)
78
-* [Colin Gagnon](ttps://github.com/colingagnon)
78
+* [Colin Gagnon](https://github.com/colingagnon)

+ 52 - 8
lib/maingenerator.go

@@ -36,20 +36,64 @@ import({{$rootHandlersPackageName := .rootHandlersPackageName}}{{$rootdbpackagen
36 36
 `))
37 37
 	importstmpl = importstmpl
38 38
 	routestmpl := template.Must(template.New("mainRoutes").Parse(`
39
+const (
40
+	// Ugly way to check to see if they passed in a password
41
+	// chance of collision with a GUID is very low
42
+	defaultPassValue = "5e7dc6f6a1a94c39be95b88a47c2458b"
43
+)
44
+
45
+var (
46
+	dbPort  string
47
+	dbHost  string
48
+	dbName  string
49
+	dbUname string
50
+	dbPass  string
51
+)
52
+
53
+func init() {
54
+	flag.StringVar(&dbPort, "P", "3306", "port")
55
+	flag.StringVar(&dbPass, "p", defaultPassValue, "password")
56
+	flag.StringVar(&dbHost, "h", "localhost", "host")
57
+	flag.StringVar(&dbName, "d", "", "database name")
58
+	flag.StringVar(&dbUname, "u", "root", "username")
59
+	flag.Parse()
60
+}
61
+
39 62
 func main(){
40
-	dbHost := os.Args[1]
41
-	dbName := os.Args[2]
42
-	dbUname := os.Args[3]
63
+	pass := dbPass
64
+	if pass == defaultPassValue {
65
+		fmt.Print("Password:")
66
+		pass = strings.TrimSpace(string(gopass.GetPasswdMasked()))
67
+	}
43 68
 
44
-	fmt.Print("Password:")
45
-	pass := strings.TrimSpace(string(gopass.GetPasswdMasked()))
46
-	dbconn, err := sql.Open("mymysql", fmt.Sprintf("tcp:%s:3306*%s/%s/%s", dbHost, dbName, dbUname, pass))
69
+	if strings.TrimSpace(dbPort) == "" {
70
+		fmt.Println("Missing port")
71
+		flag.PrintDefaults()
72
+		os.Exit(1)
73
+	}
74
+	if strings.TrimSpace(dbHost) == "" {
75
+		fmt.Println("Missing host")
76
+		flag.PrintDefaults()
77
+		os.Exit(1)
78
+	}
79
+	if strings.TrimSpace(dbName) == "" {
80
+		fmt.Println("Missing database name")
81
+		flag.PrintDefaults()
82
+		os.Exit(1)
83
+	}
84
+	if strings.TrimSpace(dbUname) == "" {
85
+		fmt.Println("Missing username")
86
+		flag.PrintDefaults()
87
+		os.Exit(1)
88
+	}
89
+	
90
+	dbConn, err := sql.Open("mymysql", fmt.Sprintf("tcp:%s:3306*%s/%s/%s", dbHost, dbName, dbUname, pass))
47 91
 	if err != nil {
48 92
 		panic(err)
49 93
 	}
50
-    db.MustValidateChecksum(dbconn, os.Args[2])
94
+    db.MustValidateChecksum(dbConn, dbName)
51 95
     {{range .Tables}}
52
-    {{.TableName}}db.DB = dbconn
96
+    {{.TableName}}db.DB = dbConn
53 97
     {{end}}
54 98
     r := mux.NewRouter()
55 99
     g := r.Methods("GET").Subrouter()

+ 2 - 2
main.go

@@ -65,12 +65,12 @@ func main() {
65 65
 		os.Exit(1)
66 66
 	}
67 67
 
68
-	db, err := sql.Open("mymysql", fmt.Sprintf("tcp:%s:%s*%s/%s/%s", dbHost, dbPort, dbName, dbUname, pass))
68
+	dbConn, err := sql.Open("mymysql", fmt.Sprintf("tcp:%s:%s*%s/%s/%s", dbHost, dbPort, dbName, dbUname, pass))
69 69
 	if err != nil {
70 70
 		flag.PrintDefaults()
71 71
 		log.Panic(err)
72 72
 	}
73
-	err = lib.Generate(db, dbName)
73
+	err = lib.Generate(dbConn, dbName)
74 74
 	if err != nil {
75 75
 		log.Panic(err)
76 76
 	}