config

package
v0.0.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 24, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FindConfigPath added in v0.0.10

func FindConfigPath(opts Options) string

FindConfigPath returns the path to the config file being used (or default path).

func InitConfig added in v0.0.10

func InitConfig(path string) (string, *errors.XError)

InitConfig creates a template config file at the given path. If path is empty, uses the default path ($HOME/.config/xsql/xsql.yaml).

func SetConfigValue added in v0.0.10

func SetConfigValue(configPath, key, value string) *errors.XError

SetConfigValue sets a value in the config file using dot-notation key. Supported key patterns:

  • profile.<name>.<field>
  • ssh_proxy.<name>.<field>

Types

type File

type File struct {
	SSHProxies map[string]SSHProxy `yaml:"ssh_proxies"`
	Profiles   map[string]Profile  `yaml:"profiles"`
	MCP        MCPConfig           `yaml:"mcp"`
}

File 表示 xsql.yaml 的配置结构。 约束:配置优先级为 CLI > ENV > Config。

func LoadConfig

func LoadConfig(opts Options) (File, string, *errors.XError)

LoadConfig 加载配置文件,返回完整配置和配置文件路径。

type MCPConfig added in v0.0.6

type MCPConfig struct {
	Transport string        `yaml:"transport"` // stdio | streamable_http
	HTTP      MCPHTTPConfig `yaml:"http"`
}

MCPConfig 定义 MCP server 配置。

type MCPHTTPConfig added in v0.0.6

type MCPHTTPConfig struct {
	Addr                string `yaml:"addr"`
	AuthToken           string `yaml:"auth_token"`            // 支持 keyring:xxx 引用
	AllowPlaintextToken bool   `yaml:"allow_plaintext_token"` // 允许明文 token
}

MCPHTTPConfig 定义 MCP Streamable HTTP 传输配置。

type Options

type Options struct {
	// ConfigPath: 若非空,则只读取该文件(不存在报错)。
	ConfigPath string

	// CLI
	CLIProfile    string
	CLIProfileSet bool
	CLIFormat     string
	CLIFormatSet  bool

	// ENV(由调用方注入,便于测试)
	EnvProfile string
	EnvFormat  string

	// HomeDir 用于默认路径计算(为空则自动探测)。
	HomeDir string

	// WorkDir 用于默认路径(为空则使用进程当前工作目录)。
	WorkDir string
}

type Profile

type Profile struct {
	Description string `yaml:"description"` // 描述,用于区分不同数据库
	Format      string `yaml:"format"`

	// DB 连接
	DB       string `yaml:"db"`  // mysql | pg
	DSN      string `yaml:"dsn"` // 原生 DSN(优先)
	Host     string `yaml:"host"`
	Port     int    `yaml:"port"`
	User     string `yaml:"user"`
	Password string `yaml:"password"` // 支持 keyring:xxx 引用
	Database string `yaml:"database"`

	// 安全选项
	AllowPlaintext   bool `yaml:"allow_plaintext"`    // 允许明文密码
	UnsafeAllowWrite bool `yaml:"unsafe_allow_write"` // 允许写操作(绕过只读保护)

	// 超时配置(秒)
	QueryTimeout  int `yaml:"query_timeout"`  // 查询超时,默认 30 秒
	SchemaTimeout int `yaml:"schema_timeout"` // Schema 导出超时,默认 60 秒

	// SSH proxy 引用(引用 ssh_proxies 中定义的名称)
	SSHProxy string `yaml:"ssh_proxy"`

	// Proxy 本地端口(用于 xsql proxy 命令)
	LocalPort int `yaml:"local_port"`

	// 解析后的 SSH 配置(由 Resolve 填充,不从 YAML 读取)
	SSHConfig *SSHProxy `yaml:"-"`
}

type ProfileInfo added in v0.0.10

type ProfileInfo struct {
	Name        string `json:"name"`
	Description string `json:"description,omitempty"`
	DB          string `json:"db"`
	Mode        string `json:"mode"` // "read-only" or "read-write"
}

func ProfileToInfo added in v0.0.10

func ProfileToInfo(name string, p Profile) ProfileInfo

type Resolved

type Resolved struct {
	ConfigPath  string
	ProfileName string
	Format      string
	Profile     Profile // 完整 profile 供 query 使用
}

func Resolve

func Resolve(opts Options) (Resolved, *errors.XError)

Resolve 实现第一阶段 config/profile/format 合并:CLI > ENV > Config。

type SSHProxy

type SSHProxy struct {
	Host           string `yaml:"host"`
	Port           int    `yaml:"port"`
	User           string `yaml:"user"`
	IdentityFile   string `yaml:"identity_file"`
	Passphrase     string `yaml:"passphrase"` // 支持 keyring:xxx 引用
	KnownHostsFile string `yaml:"known_hosts_file"`
	SkipHostKey    bool   `yaml:"skip_host_key"` // 极不推荐
}

SSHProxy 定义可复用的 SSH 代理配置。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL