-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mysql): Add list DBs functionality
- Loading branch information
1 parent
4d680f0
commit 7b9229d
Showing
3 changed files
with
113 additions
and
0 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
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,36 @@ | ||
package drop | ||
|
||
import ( | ||
mysqlcmd "github.com/sikalabs/slut/cmd/mysql" | ||
rootcmd "github.com/sikalabs/slut/cmd/root" | ||
"github.com/sikalabs/slut/utils/mysql" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var MysqlListCmd = &cobra.Command{ | ||
Use: "list", | ||
Short: "list Mysql databases", | ||
Args: cobra.NoArgs, | ||
Run: func(c *cobra.Command, args []string) { | ||
if rootcmd.RootCmdFlagJson { | ||
mysql.ListJSON( | ||
mysqlcmd.MysqlCmdFlagHost, | ||
mysqlcmd.MysqlCmdFlagPort, | ||
mysqlcmd.MysqlCmdFlagUser, | ||
mysqlcmd.MysqlCmdFlagPassword, | ||
) | ||
} else { | ||
mysql.ListText( | ||
mysqlcmd.MysqlCmdFlagHost, | ||
mysqlcmd.MysqlCmdFlagPort, | ||
mysqlcmd.MysqlCmdFlagUser, | ||
mysqlcmd.MysqlCmdFlagPassword, | ||
) | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
mysqlcmd.MysqlCmd.AddCommand(MysqlListCmd) | ||
} |
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,76 @@ | ||
package mysql | ||
|
||
import ( | ||
"database/sql" | ||
"encoding/json" | ||
"fmt" | ||
"strconv" | ||
) | ||
|
||
func List( | ||
host string, | ||
port int, | ||
user string, | ||
password string, | ||
) ([]string, error) { | ||
db, err := sql.Open( | ||
"mysql", | ||
user+":"+password+"@tcp("+host+":"+strconv.Itoa(port)+")/") | ||
if err != nil { | ||
panic(err) | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer db.Close() | ||
|
||
rows, err := db.Query("SHOW DATABASES;") | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer rows.Close() | ||
|
||
var dbNames []string | ||
var dbName string | ||
|
||
for rows.Next() { | ||
err := rows.Scan(&dbName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
dbNames = append(dbNames, dbName) | ||
} | ||
return dbNames, err | ||
} | ||
|
||
func ListText( | ||
host string, | ||
port int, | ||
user string, | ||
password string, | ||
) { | ||
dbNames, err := List(host, port, user, password) | ||
if err != nil { | ||
panic(err) | ||
} | ||
for _, dbName := range dbNames { | ||
fmt.Println(dbName) | ||
} | ||
} | ||
|
||
func ListJSON( | ||
host string, | ||
port int, | ||
user string, | ||
password string, | ||
) { | ||
dbNames, err := List(host, port, user, password) | ||
if err != nil { | ||
panic(err) | ||
} | ||
outJson, err := json.Marshal(dbNames) | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Println(string(outJson)) | ||
} |