-
-
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(postgres): Add list DBs command
- Loading branch information
1 parent
2090689
commit 66a5473
Showing
3 changed files
with
85 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,26 @@ | ||
package drop | ||
|
||
import ( | ||
postgrescmd "github.com/sikalabs/slut/cmd/postgres" | ||
"github.com/sikalabs/slut/utils/postgres" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var PostgresListCmd = &cobra.Command{ | ||
Use: "list", | ||
Short: "list Postgres databases", | ||
Args: cobra.NoArgs, | ||
Run: func(c *cobra.Command, args []string) { | ||
postgres.ListText( | ||
postgrescmd.PostgresCmdFlagHost, | ||
postgrescmd.PostgresCmdFlagPort, | ||
postgrescmd.PostgresCmdFlagUser, | ||
postgrescmd.PostgresCmdFlagPassword, | ||
) | ||
}, | ||
} | ||
|
||
func init() { | ||
postgrescmd.PostgresCmd.AddCommand(PostgresListCmd) | ||
} |
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,58 @@ | ||
package postgres | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
|
||
_ "github.com/lib/pq" | ||
) | ||
|
||
func List( | ||
host string, | ||
port int, | ||
user string, | ||
password string, | ||
) ([]string, error) { | ||
db, err := sql.Open( | ||
"postgres", fmt.Sprintf( | ||
"host=%s port=%d user=%s password=%s sslmode=disable", | ||
host, port, user, password, | ||
)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer db.Close() | ||
|
||
rows, err := db.Query("SELECT datname FROM pg_database;") | ||
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) | ||
} | ||
} |