2015-04-30 19:47:07 +03:00
package command
import (
2017-09-16 02:54:14 +03:00
"errors"
2015-04-30 19:47:07 +03:00
"os"
2016-02-20 06:14:56 +03:00
"strings"
2015-04-30 19:47:07 +03:00
"github.com/jessevdk/go-flags"
)
type Options struct {
2018-02-22 23:20:18 +03:00
Version bool ` short:"v" long:"version" description:"Print version" `
2018-09-01 05:49:24 +03:00
Debug bool ` short:"d" long:"debug" description:"Enable debugging mode" `
2018-02-22 23:20:18 +03:00
Url string ` long:"url" description:"Database connection string" `
2018-09-14 08:04:02 +03:00
Host string ` long:"host" description:"Server hostname or IP" default:"localhost" `
2018-02-22 23:20:18 +03:00
Port int ` long:"port" description:"Server port" default:"5432" `
User string ` long:"user" description:"Database user" `
Pass string ` long:"pass" description:"Password for user" `
DbName string ` long:"db" description:"Database name" `
Ssl string ` long:"ssl" description:"SSL option" `
HttpHost string ` long:"bind" description:"HTTP server host" default:"localhost" `
HttpPort uint ` long:"listen" description:"HTTP server listen port" default:"8081" `
AuthUser string ` long:"auth-user" description:"HTTP basic auth user" `
AuthPass string ` long:"auth-pass" description:"HTTP basic auth password" `
SkipOpen bool ` short:"s" long:"skip-open" description:"Skip browser open on start" `
2018-09-01 05:49:24 +03:00
Sessions bool ` long:"sessions" description:"Enable multiple database sessions" `
2018-02-22 23:20:18 +03:00
Prefix string ` long:"prefix" description:"Add a url prefix" `
ReadOnly bool ` long:"readonly" description:"Run database connection in readonly mode" `
2018-09-01 05:49:24 +03:00
LockSession bool ` long:"lock-session" description:"Lock session to a single database connection" `
2018-02-22 23:20:18 +03:00
Bookmark string ` short:"b" long:"bookmark" description:"Bookmark to use for connection. Bookmark files are stored under $HOME/.pgweb/bookmarks/*.toml" default:"" `
BookmarksDir string ` long:"bookmarks-dir" description:"Overrides default directory for bookmark files to search" default:"" `
2018-09-01 05:49:24 +03:00
DisablePrettyJson bool ` long:"no-pretty-json" description:"Disable JSON formatting feature for result export" `
DisableSSH bool ` long:"no-ssh" description:"Disable database connections via SSH" `
2018-02-22 23:20:18 +03:00
ConnectBackend string ` long:"connect-backend" description:"Enable database authentication through a third party backend" `
ConnectToken string ` long:"connect-token" description:"Authentication token for the third-party connect backend" `
ConnectHeaders string ` long:"connect-headers" description:"List of headers to pass to the connect backend" `
2018-09-01 05:49:24 +03:00
DisableConnectionIdleTimeout bool ` long:"no-idle-timeout" description:"Disable connection idle timeout" `
2018-02-22 23:20:18 +03:00
ConnectionIdleTimeout int ` long:"idle-timeout" description:"Set connection idle timeout in minutes" default:"180" `
2018-09-01 05:49:24 +03:00
Cors bool ` long:"cors" description:"Enable Cross-Origin Resource Sharing (CORS)" `
2018-02-22 23:20:18 +03:00
CorsOrigin string ` long:"cors-origin" description:"Allowed CORS origins" default:"*" `
2015-04-30 19:47:07 +03:00
}
var Opts Options
2018-02-22 23:20:18 +03:00
func ParseOptions ( args [ ] string ) ( Options , error ) {
var opts = Options { }
_ , err := flags . ParseArgs ( & opts , args )
2015-04-30 19:47:07 +03:00
if err != nil {
2018-02-22 23:20:18 +03:00
return opts , err
2015-04-30 19:47:07 +03:00
}
2018-02-22 23:20:18 +03:00
if opts . Url == "" {
opts . Url = os . Getenv ( "DATABASE_URL" )
2015-04-30 19:47:07 +03:00
}
2018-11-27 23:58:50 +03:00
if opts . Prefix == "" {
opts . Prefix = os . Getenv ( "URL_PREFIX" )
}
2016-01-11 00:22:30 +03:00
if os . Getenv ( "SESSIONS" ) != "" {
2018-02-22 23:20:18 +03:00
opts . Sessions = true
2016-01-11 00:22:30 +03:00
}
2016-11-06 05:23:37 +03:00
if os . Getenv ( "LOCK_SESSION" ) != "" {
2018-02-22 23:20:18 +03:00
opts . LockSession = true
opts . Sessions = false
2016-11-06 05:23:37 +03:00
}
2018-09-14 08:54:42 +03:00
if opts . Sessions || opts . ConnectBackend != "" {
2018-09-14 08:56:16 +03:00
opts . Bookmark = ""
2018-09-14 08:54:42 +03:00
opts . Url = ""
opts . Host = ""
opts . User = ""
opts . Pass = ""
opts . DbName = ""
opts . Ssl = ""
}
2018-02-22 23:20:18 +03:00
if opts . Prefix != "" && ! strings . Contains ( opts . Prefix , "/" ) {
opts . Prefix = opts . Prefix + "/"
2016-02-20 06:14:56 +03:00
}
2018-02-22 23:20:18 +03:00
if opts . AuthUser == "" && os . Getenv ( "AUTH_USER" ) != "" {
opts . AuthUser = os . Getenv ( "AUTH_USER" )
2016-09-29 04:37:07 +03:00
}
2018-02-22 23:20:18 +03:00
if opts . AuthPass == "" && os . Getenv ( "AUTH_PASS" ) != "" {
opts . AuthPass = os . Getenv ( "AUTH_PASS" )
2016-09-29 04:37:07 +03:00
}
2018-02-22 23:20:18 +03:00
if opts . ConnectBackend != "" {
if ! opts . Sessions {
return opts , errors . New ( "--sessions flag must be set" )
2017-09-16 02:54:14 +03:00
}
2018-02-22 23:20:18 +03:00
if opts . ConnectToken == "" {
return opts , errors . New ( "--connect-token flag must be set" )
2017-09-16 02:54:14 +03:00
}
2017-09-21 10:06:14 +03:00
} else {
2018-02-22 23:20:18 +03:00
if opts . ConnectToken != "" || opts . ConnectHeaders != "" {
return opts , errors . New ( "--connect-backend flag must be set" )
2017-09-21 10:06:14 +03:00
}
2017-09-16 02:54:14 +03:00
}
2018-02-22 23:20:18 +03:00
return opts , nil
}
func SetDefaultOptions ( ) error {
opts , err := ParseOptions ( [ ] string { } )
if err != nil {
return err
}
Opts = opts
2015-04-30 19:47:07 +03:00
return nil
}