pgweb/pkg/bookmarks/bookmarks.go

142 lines
3.4 KiB
Go
Raw Permalink Normal View History

2015-04-30 11:47:07 -05:00
package bookmarks
2014-12-02 22:19:38 -06:00
import (
"fmt"
2022-11-14 15:47:36 -06:00
"os"
2014-12-02 22:19:38 -06:00
"path/filepath"
"strings"
"github.com/BurntSushi/toml"
"github.com/mitchellh/go-homedir"
"github.com/sosedoff/pgweb/pkg/command"
"github.com/sosedoff/pgweb/pkg/shared"
2014-12-02 22:19:38 -06:00
)
2019-11-02 13:00:23 -05:00
// Bookmark contains information about bookmarked database connection
2014-12-02 22:19:38 -06:00
type Bookmark struct {
2019-11-02 13:00:23 -05:00
URL string `json:"url"` // Postgres connection URL
Host string `json:"host"` // Server hostname
Port int `json:"port"` // Server port
User string `json:"user"` // Database user
Password string `json:"password"` // User password
Database string `json:"database"` // Database name
2022-11-23 16:21:30 -06:00
SSLMode string `json:"ssl"` // Connection SSL mode
2019-11-02 13:00:23 -05:00
SSH *shared.SSHInfo `json:"ssh"` // SSH tunnel config
2014-12-02 22:19:38 -06:00
}
// SSHInfoIsEmpty returns true if ssh configuration is not provided
func (b Bookmark) SSHInfoIsEmpty() bool {
2019-11-02 13:00:23 -05:00
return b.SSH == nil || b.SSH.User == "" && b.SSH.Host == "" && b.SSH.Port == ""
}
2019-11-02 13:00:23 -05:00
// ConvertToOptions returns an options struct from connection details
func (b Bookmark) ConvertToOptions() command.Options {
return command.Options{
2022-11-23 16:21:30 -06:00
URL: b.URL,
Host: b.Host,
Port: b.Port,
User: b.User,
Pass: b.Password,
DbName: b.Database,
SSLMode: b.SSLMode,
}
}
2014-12-02 22:19:38 -06:00
func readServerConfig(path string) (Bookmark, error) {
bookmark := Bookmark{}
2022-11-14 15:47:36 -06:00
buff, err := os.ReadFile(path)
2014-12-02 22:19:38 -06:00
if err != nil {
return bookmark, err
}
_, err = toml.Decode(string(buff), &bookmark)
if bookmark.Port == 0 {
bookmark.Port = 5432
}
2018-01-08 10:55:05 +01:00
// List of all supported postgres modes
modes := []string{"disable", "allow", "prefer", "require", "verify-ca", "verify-full"}
valid := false
for _, mode := range modes {
2022-11-23 16:21:30 -06:00
if bookmark.SSLMode == mode {
valid = true
break
}
}
// Fall back to a default mode if mode is not set or invalid
// Typical typo: ssl mode set to "disabled"
2022-11-23 16:21:30 -06:00
if bookmark.SSLMode == "" || !valid {
bookmark.SSLMode = "disable"
}
2019-11-02 12:44:04 -05:00
// Set default SSH port if it's not provided by user
2019-11-02 13:00:23 -05:00
if bookmark.SSH != nil && bookmark.SSH.Port == "" {
bookmark.SSH.Port = "22"
}
2014-12-02 22:19:38 -06:00
return bookmark, err
}
func fileBasename(path string) string {
filename := filepath.Base(path)
return strings.Replace(filename, filepath.Ext(path), "", 1)
}
2019-11-02 13:00:23 -05:00
// Path returns bookmarks storage path
func Path(overrideDir string) string {
if overrideDir == "" {
path, _ := homedir.Dir()
return fmt.Sprintf("%s/.pgweb/bookmarks", path)
}
return overrideDir
2014-12-02 22:19:38 -06:00
}
2019-11-02 13:00:23 -05:00
// ReadAll returns all available bookmarks
2015-04-30 11:47:07 -05:00
func ReadAll(path string) (map[string]Bookmark, error) {
2014-12-02 22:19:38 -06:00
results := map[string]Bookmark{}
2022-11-14 15:47:36 -06:00
files, err := os.ReadDir(path)
2014-12-02 22:19:38 -06:00
if err != nil {
return results, err
}
for _, file := range files {
if filepath.Ext(file.Name()) != ".toml" {
continue
}
2019-11-02 13:00:23 -05:00
fullPath := filepath.Join(path, file.Name())
2014-12-02 22:19:38 -06:00
key := fileBasename(file.Name())
config, err := readServerConfig(fullPath)
if err != nil {
fmt.Printf("%s parse error: %s\n", fullPath, err)
2014-12-02 22:19:38 -06:00
continue
}
results[key] = config
}
return results, nil
}
2019-11-02 13:00:23 -05:00
// GetBookmark reads an existing bookmark
func GetBookmark(bookmarkPath string, bookmarkName string) (Bookmark, error) {
bookmarks, err := ReadAll(bookmarkPath)
if err != nil {
return Bookmark{}, err
}
bookmark, ok := bookmarks[bookmarkName]
if !ok {
return Bookmark{}, fmt.Errorf("couldn't find a bookmark with name %s", bookmarkName)
}
return bookmark, nil
}