Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tern psql #78

Merged
merged 6 commits into from
Mar 27, 2023
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ var newMigrationText = `-- Write your migrate up statements here

type Config struct {
ConnConfig pgx.ConnConfig
ConnString string
SslMode string
SslRootCert string
VersionTable string
Expand Down Expand Up @@ -254,6 +255,13 @@ The word "last":
}
addConfigFlagsToCommand(cmdStatus)

cmdPrintConnString := &cobra.Command{
Use: "print-connstring",
Short: "Prints a connection string based on the provided config file/arguments",
Run: PrintConnString,
}
addConfigFlagsToCommand(cmdPrintConnString)

cmdNew := &cobra.Command{
Use: "new NAME",
Short: "Generate a new migration",
Expand Down Expand Up @@ -306,6 +314,7 @@ The word "last":
rootCmd.AddCommand(cmdRenumber)
rootCmd.AddCommand(cmdCode)
rootCmd.AddCommand(cmdStatus)
rootCmd.AddCommand(cmdPrintConnString)
rootCmd.AddCommand(cmdNew)
rootCmd.AddCommand(cmdVersion)
rootCmd.Execute()
Expand Down Expand Up @@ -682,6 +691,33 @@ func SnapshotCode(cmd *cobra.Command, args []string) {
}
}

func PrintConnString(cmd *cobra.Command, args []string) {
ctx := context.Background()
config, conn := loadConfigAndConnectToDB(ctx)
defer conn.Close(ctx)

connstring := config.ConnString
if connstring == "" {
options := ""
if ssl_mode := config.SslMode; ssl_mode != "" {
options += fmt.Sprintf("sslmode=%s&", ssl_mode)
}
if ssl_cert := config.SslRootCert; ssl_cert != "" {
options += fmt.Sprintf("sslcert=%s", ssl_cert)
}
connstring = fmt.Sprintf(
"postgres://%s:%s@%s:%d/%s?%s",
config.ConnConfig.User,
config.ConnConfig.Password,
config.ConnConfig.Host,
config.ConnConfig.Port,
config.ConnConfig.Database,
options,
)
}
fmt.Print(connstring)
}

func Status(cmd *cobra.Command, args []string) {
ctx := context.Background()
config, conn := loadConfigAndConnectToDB(ctx)
Expand Down Expand Up @@ -958,6 +994,8 @@ func appendConfigFromFile(config *Config, path string) error {
}

if connString, ok := file.Get("database", "conn_string"); ok {
config.ConnString = connString
fmt.Println(config.ConnString)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this fmt.Println intended?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No thanks for catching that. It was just a test print I forgot to delete. Removed in the newest push.

if connConfig, err := pgx.ParseConfig(connString); err == nil {
config.ConnConfig = *connConfig
} else {
Expand Down Expand Up @@ -1026,7 +1064,6 @@ func appendConfigFromFile(config *Config, path string) error {
if password, ok := file.Get("ssh-tunnel", "password"); ok {
config.SSHConnConfig.Password = password
}

return nil
}

Expand Down