-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from ph4r5h4d/postgresql-support
Postgresql support
- Loading branch information
Showing
9 changed files
with
108 additions
and
4 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package PostgreSQLChecker | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func (pq PostgresSQLConnection) BuildConnectionString() string { | ||
dsl := fmt.Sprintf("host=%s port=%d user=%s password=%s sslmode=%s dbname=%s ", | ||
pq.Host, pq.Port, pq.Username, pq.Password, pq.SSLMode, pq.DatabaseName) | ||
|
||
return dsl | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package PostgreSQLChecker | ||
|
||
import ( | ||
"database/sql" | ||
"errors" | ||
_ "github.com/lib/pq" | ||
"wait4it/model" | ||
) | ||
|
||
func (pq *PostgresSQLConnection) BuildContext(cx model.CheckContext) { | ||
pq.Port = cx.Port | ||
pq.Host = cx.Host | ||
pq.Username = cx.Username | ||
pq.Password = cx.Password | ||
pq.DatabaseName = cx.DatabaseName | ||
if len(cx.DBConf.SSLMode) < 1 { | ||
pq.SSLMode = "disable" | ||
} else { | ||
pq.SSLMode = cx.DBConf.SSLMode | ||
} | ||
} | ||
|
||
func (pq *PostgresSQLConnection) Validate() (bool, error) { | ||
if len(pq.Host) == 0 || len(pq.Username) == 0 { | ||
return false, errors.New("host or username can't be empty") | ||
} | ||
|
||
if pq.Port < 0 || pq.Port > 65535 { | ||
return false, errors.New("invalid port range for mysql") | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
func (pq *PostgresSQLConnection) Check() (bool, bool, error) { | ||
dsl := pq.BuildConnectionString() | ||
|
||
db, err := sql.Open("postgres", dsl) | ||
|
||
// if there is an error opening the connection, handle it | ||
if err != nil { | ||
return false, true, err | ||
} | ||
|
||
err = db.Ping() | ||
if err != nil { | ||
return false, false, nil | ||
} | ||
_ = db.Close() | ||
|
||
return true, true, nil | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package PostgreSQLChecker | ||
|
||
type PostgresSQLConnection struct { | ||
Host string | ||
Port int | ||
Username string | ||
Password string | ||
DatabaseName string | ||
SSLMode string | ||
} |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= | ||
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= | ||
github.com/lib/pq v1.3.0 h1:/qkRGz8zljWiDcFvgpwUpwIAPu3r07TDvs3Rws+o/pU= | ||
github.com/lib/pq v1.3.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= |
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
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