Skip to content

Commit

Permalink
Add host flag
Browse files Browse the repository at this point in the history
Fix port flag
Fix psql path

Co-authored-by: Thomas Krampl <thomas.siegfried.krampl@nav.no>
  • Loading branch information
erikvatt and thokra-nav committed Jan 31, 2022
1 parent 5c3cb4d commit 76d3da8
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ const (
UsernameFlag = "username"
VerboseFlag = "verbose"
PortFlag = "port"
HostFlag = "host"
)
4 changes: 3 additions & 1 deletion cmd/root/postgres/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ func (c Config) InitCmds(root *cobra.Command) {
viper.BindPFlag(cmd.ContextFlag, c.postgres.PersistentFlags().Lookup(cmd.ContextFlag))

c.proxy.Flags().StringP(cmd.PortFlag, "p", "5432", "Local port for the proxy to listen on")
viper.BindPFlag(cmd.ContextFlag, c.proxy.Flags().Lookup(cmd.ContextFlag))
viper.BindPFlag(cmd.PortFlag, c.proxy.Flags().Lookup(cmd.PortFlag))
c.proxy.Flags().StringP(cmd.HostFlag, "H", "localhost", "Host for the proxy")
viper.BindPFlag(cmd.HostFlag, c.proxy.Flags().Lookup(cmd.HostFlag))

c.psql.Flags().BoolP(cmd.VerboseFlag, "V", false, "Verbose will also print the proxy logs")
viper.BindPFlag(cmd.VerboseFlag, c.psql.Flags().Lookup(cmd.VerboseFlag))
Expand Down
5 changes: 4 additions & 1 deletion cmd/root/postgres/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var proxyCmd = &cobra.Command{
namespace := viper.GetString(cmd.NamespaceFlag)
context := viper.GetString(cmd.ContextFlag)
port := viper.GetString(cmd.PortFlag)
host := viper.GetString(cmd.HostFlag)

dbInfo, err := NewDBInfo(appName, namespace, context)
if err != nil {
Expand All @@ -46,7 +47,9 @@ var proxyCmd = &cobra.Command{
return err
}

return runProxy(ctx, projectID, connectionName, "localhost:"+port, make(chan int, 1))
fmt.Printf("Starting proxy on %v:%v\n", host, port)

return runProxy(ctx, projectID, connectionName, fmt.Sprintf("%v:%v", host, port), make(chan int, 1))
},
}

Expand Down
8 changes: 7 additions & 1 deletion cmd/root/postgres/psql.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ var psqlCmd = &cobra.Command{
k8sContext := viper.GetString(cmd.ContextFlag)
verbose := viper.GetBool(cmd.VerboseFlag)

psqlPath, err := exec.LookPath("psql")
if err != nil {
return err
}

dbInfo, err := NewDBInfo(appName, namespace, k8sContext)
if err != nil {
return err
Expand Down Expand Up @@ -77,7 +82,8 @@ var psqlCmd = &cobra.Command{
"--username", email,
"--dbname", connectionInfo.dbName,
}
cmd := exec.CommandContext(ctx, "psql", arguments...)

cmd := exec.CommandContext(ctx, psqlPath, arguments...)
cmd.Env = append(cmd.Env, fmt.Sprintf("PGPASSWORD=%s", token))

cmd.Stderr = os.Stderr
Expand Down

0 comments on commit 76d3da8

Please sign in to comment.